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 com.fasterxml.jackson.core.JsonParseException;
19 import com.fasterxml.jackson.core.JsonProcessingException;
20 import com.fasterxml.jackson.databind.JsonMappingException;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.Locale;
27 import java.util.concurrent.locks.Lock;
28 import java.util.concurrent.locks.ReentrantLock;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 final class I18nJsonUtil {
34 private static I18nJsonUtil util;
35 private static Lock lock = new ReentrantLock();
36 private ObjectMapper objectMapper;
38 public I18nJsonUtil(ObjectMapper objectMapper) {
39 this.objectMapper = objectMapper;
42 <T extends Object> T readFromJson(InputStream ins, Class<T> clazz)
43 throws JsonParseException, JsonMappingException, IOException {
44 ByteArrayOutputStream baos = new ByteArrayOutputStream();
45 byte[] bs = new byte[1024 * 10];
47 while ((length = ins.read(bs)) > 0) {
48 baos.write(bs, 0, length);
50 bs = baos.toByteArray();
54 // 删除bom头 -17, -69, -65
55 if (bs[0] == -17 && bs[1] == -69 && bs[2] == -65) {
56 ubs = new byte[bs.length - 3];
57 System.arraycopy(bs, 3, ubs, 0, ubs.length);
63 return objectMapper.readValue(ubs, clazz);
66 <T extends Object> T readFromJson(String str, Class<T> clazz)
67 throws JsonParseException, JsonMappingException, IOException {
68 return objectMapper.readValue(str, clazz);
71 String writeToJson(Object obj) throws JsonProcessingException {
72 return objectMapper.writeValueAsString(obj);
75 static I18nJsonUtil getInstance(ObjectMapper objectMapper) {
80 if (objectMapper == null) {
81 objectMapper = new ObjectMapper();
83 util = new I18nJsonUtil(objectMapper);
92 static I18nJsonUtil getInstance() {
93 return getInstance(null);
103 class I18nLocaleTransfer {
105 private static Logger LOG = LoggerFactory.getLogger(I18nLocaleTransfer.class);
108 * 方言转换<br> 如果存在直接返回<br> 如果不存在,则获取语言进行模糊匹配,未匹配上则返回默认方言<br>
110 * @param theLocale 待转换方言
111 * @param locales 存在的方言
114 public static String transfer(Locale theLocale, Set<String> locales) {
115 if (locales == null || locales.isEmpty()) {
116 LOG.debug("locales is NULL or empty");
119 if (theLocale == null) {
120 String result = fetchDefault(locales);
121 LOG.debug("transfer NULL --> " + result + " in " + locales);
124 String locale = theLocale.toString();
125 if (locale.isEmpty()) {
126 String result = fetchDefault(locales);
127 LOG.debug("transfer EMPTY --> " + result + " in " + locales);
131 if (locales.contains(locale)) {
136 String language = theLocale.getLanguage();
137 if (locales.contains(language)) {
138 LOG.debug("transfer " + locale + " --> " + language + " in " + locales);
142 language = language + "_";
143 for (String temp : locales) {
144 if (temp.startsWith(language)) {
145 LOG.debug("transfer " + locale + " --> " + temp + " in " + locales);
149 String result = fetchDefault(locales);
150 LOG.debug("transfer " + locale + " --> " + result + " in " + locales);
155 * 返回默认方言,优先级为en,en_US,zh,zh_CN,如果都不存在,则随机返回一个
157 private static String fetchDefault(Set<String> locales) {
158 if (locales.contains("en")) {
160 } else if (locales.contains("en_US")) {
163 if (locales.contains("zh")) {
165 } else if (locales.contains("zh_CN")) {
168 return locales.iterator().next();