diff options
author | Zhaoxing <meng.zhaoxing1@zte.com.cn> | 2017-08-08 14:02:53 +0800 |
---|---|---|
committer | Zhaoxing <meng.zhaoxing1@zte.com.cn> | 2017-08-08 14:02:53 +0800 |
commit | c0604184b2aa8cff924ca783ec6b36f1f5988775 (patch) | |
tree | 1c70ffe1ad90c915b3382a37eb2fa80901519c7b /baseservice-i18n/src/main/java/org/openo/baseservice/i18n/I18nImpl.java | |
parent | 706fc9fed496972968fde136c3e4e10f9578a5b3 (diff) |
init code
Change-Id: Icd0948118397b256da70dfbcbbec5520dc5eafd4
Signed-off-by: Zhaoxing <meng.zhaoxing1@zte.com.cn>
Diffstat (limited to 'baseservice-i18n/src/main/java/org/openo/baseservice/i18n/I18nImpl.java')
-rw-r--r-- | baseservice-i18n/src/main/java/org/openo/baseservice/i18n/I18nImpl.java | 137 |
1 files changed, 0 insertions, 137 deletions
diff --git a/baseservice-i18n/src/main/java/org/openo/baseservice/i18n/I18nImpl.java b/baseservice-i18n/src/main/java/org/openo/baseservice/i18n/I18nImpl.java deleted file mode 100644 index bda6f2d..0000000 --- a/baseservice-i18n/src/main/java/org/openo/baseservice/i18n/I18nImpl.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * 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*\\}", ""); - } - -} |