2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.openo.baseservice.i18n;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Locale;
25 import java.util.Map.Entry;
27 public class I18nImpl implements I18n {
28 private static Logger logger = LoggerFactory.getLogger(I18nImpl.class);
32 private Map<String, I18nItem> items;
34 private Map<String, String> keyToValueMap;
36 private Map<String, String> valueToKeyMap;
39 public I18nImpl(String name, Map<String, I18nItem> items) {
41 this.items = Collections.unmodifiableMap(items);
43 keyToValueMap = new HashMap<>(items.size());
44 valueToKeyMap = new HashMap<>(items.size() * 3);
47 for (Entry<String, I18nItem> entry : items.entrySet()) {
48 String key = entry.getKey();
49 I18nItem item = entry.getValue();
50 String value = I18nJsonUtil.getInstance().writeToJson(item.getValues());
52 keyToValueMap.put(key, value);
53 for (Entry<String, String> valueEntry : item.getValues().entrySet()) {
54 valueToKeyMap.put(valueEntry.getValue(), key);
57 } catch (Exception e) {
58 logger.error("new I18nImpl failed:" + name, e);
61 keyToValueMap = Collections.unmodifiableMap(keyToValueMap);
62 valueToKeyMap = Collections.unmodifiableMap(valueToKeyMap);
66 public Map<String, String> getLabelValues(String labelKey) {
67 I18nItem item = items.get(labelKey);
69 return item.getValues();
75 public String getLabelValue(String labelKey, Locale theLocale) {
76 I18nItem item = items.get(labelKey);
78 Map<String, String> values = item.getValues();
79 return values.get(I18nLocaleTransfer.transfer(theLocale, values.keySet()));
85 public String getLabelValue(String labelKey, Locale theLocale, String[] arguments) {
86 I18nItem item = items.get(labelKey);
88 Map<String, String> values = item.getValues();
89 String value = values.get(I18nLocaleTransfer.transfer(theLocale, values.keySet()));
90 return replaceArguments(value, arguments);
96 public Map<String, String> getLabelValues(String labelKey, String[] arguments) {
97 I18nItem item = items.get(labelKey);
99 Map<String, String> map = new HashMap<String, String>();
100 for (Entry<String, String> entry : item.getValues().entrySet()) {
101 String value = entry.getValue();
102 map.put(entry.getKey(), replaceArguments(value, arguments));
110 public String getCanonicalLabelValues(String labelKey) {
111 return keyToValueMap.get(labelKey);
115 public String getKeyFromValue(String aValue) {
116 return valueToKeyMap.get(aValue);
119 private String replaceArguments(String value, String[] arguments) {
123 if (arguments != null) {
125 for (String argument : arguments) {
126 if (argument == null) {
127 value = value.replaceAll("\\{\\s*" + i + "\\s*\\}", "");
129 value = value.replaceAll("\\{\\s*" + i + "\\s*\\}", argument);
134 return value.replaceAll("\\{\\s*\\d+\\s*\\}", "");