summaryrefslogtreecommitdiffstats
path: root/wso2/baseservice-i18n/src/main/java/org/openo/baseservice/i18n/I18nImpl.java
blob: bda6f2d3715c32f823ef7936b4b6834b3db99eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * Copyright 2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openo.baseservice.i18n;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

public class I18nImpl implements I18n {
  private static Logger logger = LoggerFactory.getLogger(I18nImpl.class);

  private String name;

  private Map<String, I18nItem> items;

  private Map<String, String> keyToValueMap;

  private Map<String, String> valueToKeyMap;


  public I18nImpl(String name, Map<String, I18nItem> items) {
    this.name = name;
    this.items = Collections.unmodifiableMap(items);

    keyToValueMap = new HashMap<>(items.size());
    valueToKeyMap = new HashMap<>(items.size() * 3);

    try {
      for (Entry<String, I18nItem> entry : items.entrySet()) {
        String key = entry.getKey();
        I18nItem item = entry.getValue();
        String value = I18nJsonUtil.getInstance().writeToJson(item.getValues());

        keyToValueMap.put(key, value);
        for (Entry<String, String> valueEntry : item.getValues().entrySet()) {
          valueToKeyMap.put(valueEntry.getValue(), key);
        }
      }
    } catch (Exception e) {
      logger.error("new I18nImpl failed:" + name, e);
    }

    keyToValueMap = Collections.unmodifiableMap(keyToValueMap);
    valueToKeyMap = Collections.unmodifiableMap(valueToKeyMap);
  }

  @Override
  public Map<String, String> getLabelValues(String labelKey) {
    I18nItem item = items.get(labelKey);
    if (item != null) {
      return item.getValues();
    }
    return null;
  }

  @Override
  public String getLabelValue(String labelKey, Locale theLocale) {
    I18nItem item = items.get(labelKey);
    if (item != null) {
      Map<String, String> values = item.getValues();
      return values.get(I18nLocaleTransfer.transfer(theLocale, values.keySet()));
    }
    return null;
  }

  @Override
  public String getLabelValue(String labelKey, Locale theLocale, String[] arguments) {
    I18nItem item = items.get(labelKey);
    if (item != null) {
      Map<String, String> values = item.getValues();
      String value = values.get(I18nLocaleTransfer.transfer(theLocale, values.keySet()));
      return replaceArguments(value, arguments);
    }
    return null;
  }

  @Override
  public Map<String, String> getLabelValues(String labelKey, String[] arguments) {
    I18nItem item = items.get(labelKey);
    if (item != null) {
      Map<String, String> map = new HashMap<String, String>();
      for (Entry<String, String> entry : item.getValues().entrySet()) {
        String value = entry.getValue();
        map.put(entry.getKey(), replaceArguments(value, arguments));
      }
      return map;
    }
    return null;
  }

  @Override
  public String getCanonicalLabelValues(String labelKey) {
    return keyToValueMap.get(labelKey);
  }

  @Override
  public String getKeyFromValue(String aValue) {
    return valueToKeyMap.get(aValue);
  }

  private String replaceArguments(String value, String[] arguments) {
    if (value == null) {
      return null;
    }
    if (arguments != null) {
      int i = 0;
      for (String argument : arguments) {
        if (argument == null) {
          value = value.replaceAll("\\{\\s*" + i + "\\s*\\}", "");
        } else {
          value = value.replaceAll("\\{\\s*" + i + "\\s*\\}", argument);
        }
        i++;
      }
    }
    return value.replaceAll("\\{\\s*\\d+\\s*\\}", "");
  }

}