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.io.IOException;
22 import java.io.InputStream;
23 import java.util.HashMap;
24 import java.util.Locale;
26 import java.util.Map.Entry;
27 import java.util.Optional;
29 final class I18nContainer {
31 private static Logger logger = LoggerFactory.getLogger(I18nContainer.class);
33 private Map<String, I18n> i18ns;
35 private I18nContainer() {
41 Map<String, Map<String, I18nItem>> i18nTemps = generateI18ns();
43 i18ns = new HashMap<String, I18n>();
44 for (Entry<String, Map<String, I18nItem>> entry : i18nTemps.entrySet()) {
45 String name = entry.getKey();
46 Map<String, I18nItem> items = entry.getValue();
47 for (Entry<String, I18nItem> i18nItemEntry : items.entrySet()) {
48 i18nItemEntry.getValue().unmodifiable();
51 I18n i18n = new I18nImpl(name, items);
52 i18ns.put(name, i18n);
56 @SuppressWarnings("unchecked")
57 private Map<String, Map<String, I18nItem>> generateI18ns() {
58 Map<String, Map<String, I18nItem>> i18nTemps = new HashMap<String, Map<String, I18nItem>>();
60 ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
61 JsonResourceScanner.findI18nPaths().forEach((path) -> {
62 HashMap<String, String> fileValues = null;
63 try (InputStream ins = systemClassLoader.getResourceAsStream(path)) {
64 fileValues = I18nJsonUtil.getInstance().readFromJson(ins, HashMap.class);
65 logger.info("load i18n file success: " + path);
66 } catch (IOException ex) {
67 logger.info("load i18n file failed: " + path);
68 logger.info("load i18n file failed: " + systemClassLoader.getResource(path).toString(), ex);
72 if (!validateI18nFileValues(fileValues, path)) {
76 String fileName = null;
77 int i = path.lastIndexOf("/");
79 fileName = path.substring(i + 1);
83 i = fileName.indexOf("-i18n-");
84 String name = fileName.substring(0, i);
85 String localeSrc = fileName.substring(i + 6, fileName.lastIndexOf("."));
87 logger.info("parse i18n file failed: name is null");
89 } else if (localeSrc.isEmpty()) {
90 logger.info("parse i18n file failed: locale is null");
94 String[] ss = localeSrc.replace("-", "_").split("_");
97 locale = new Locale(ss[0]).toString();
98 } else if (ss.length == 2) {
99 locale = new Locale(ss[0], ss[1]).toString();
101 logger.info("parse i18n file failed: locale is error \"" + localeSrc + "\"");
105 Map<String, I18nItem> i18nItems = i18nTemps.get(name);
106 if (i18nItems == null) {
107 i18nItems = new HashMap<String, I18nItem>();
108 i18nTemps.put(name, i18nItems);
111 for (Entry<String, String> entry : fileValues.entrySet()) {
112 String label = entry.getKey();
114 I18nItem i18nItem = i18nItems.get(label);
115 if (i18nItem == null) {
116 i18nItem = new I18nItem(label);
117 i18nItems.put(label, i18nItem);
120 i18nItem.addValue(locale, entry.getValue());
126 private boolean validateI18nFileValues(HashMap<String, String> fileValues, String path) {
127 for (Entry<String, String> entry : fileValues.entrySet()) {
128 if (entry.getValue() != null && !String.class.isInstance(entry.getValue())) {
129 logger.info("parse i18n file failed: " + path + " field's[" + entry.getKey()
130 + "] value is not string type");
137 protected static I18nContainer getInstance() {
138 return I18nContainerSingleton.singleton;
141 Optional<I18n> getI18n(String name) {
142 return Optional.ofNullable(this.i18ns.get(name));
145 static class I18nContainerSingleton {
146 private static final I18nContainer singleton = new I18nContainer();