summaryrefslogtreecommitdiffstats
path: root/wso2/baseservice-i18n/src/main/java/org/openo/baseservice/i18n/I18nJsonUtil.java
blob: e3746dcc99852ee1c5301ddbe23dfe570cd2e8b6 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 * 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 com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class I18nJsonUtil {

    private static I18nJsonUtil util;
    private static Lock lock = new ReentrantLock();
    private ObjectMapper objectMapper;

    public I18nJsonUtil(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    <T extends Object> T readFromJson(InputStream ins, Class<T> clazz)
        throws JsonParseException, JsonMappingException, IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bs = new byte[1024 * 10];
        int length = -1;
        while ((length = ins.read(bs)) > 0) {
            baos.write(bs, 0, length);
        }
        bs = baos.toByteArray();

        byte[] ubs = null;
        if (bs.length > 3) {
            // 删除bom头 -17, -69, -65
            if (bs[0] == -17 && bs[1] == -69 && bs[2] == -65) {
                ubs = new byte[bs.length - 3];
                System.arraycopy(bs, 3, ubs, 0, ubs.length);
            }
        }
        if (ubs == null) {
            ubs = bs;
        }
        return objectMapper.readValue(ubs, clazz);
    }

    <T extends Object> T readFromJson(String str, Class<T> clazz)
        throws JsonParseException, JsonMappingException, IOException {
        return objectMapper.readValue(str, clazz);
    }

    String writeToJson(Object obj) throws JsonProcessingException {
        return objectMapper.writeValueAsString(obj);
    }

    static I18nJsonUtil getInstance(ObjectMapper objectMapper) {
        if (util == null) {
            lock.lock();
            try {
                if (util == null) {
                    if (objectMapper == null) {
                        objectMapper = new ObjectMapper();
                    }
                    util = new I18nJsonUtil(objectMapper);
                }
            } finally {
                lock.unlock();
            }
        }
        return util;
    }

    static I18nJsonUtil getInstance() {
        return getInstance(null);
    }
}


/**
 * 国际化转换工具
 *
 * @author 10163976
 */
class I18nLocaleTransfer {

    private static Logger LOG = LoggerFactory.getLogger(I18nLocaleTransfer.class);

    /**
     * 方言转换<br> 如果存在直接返回<br> 如果不存在,则获取语言进行模糊匹配,未匹配上则返回默认方言<br>
     *
     * @param theLocale 待转换方言
     * @param locales 存在的方言
     * @return 转换后的方言
     */
    public static String transfer(Locale theLocale, Set<String> locales) {
        if (locales == null || locales.isEmpty()) {
            LOG.debug("locales is NULL or empty");
            return null;
        }
        if (theLocale == null) {
            String result = fetchDefault(locales);
            LOG.debug("transfer NULL --> " + result + " in " + locales);
            return result;
        }
        String locale = theLocale.toString();
        if (locale.isEmpty()) {
            String result = fetchDefault(locales);
            LOG.debug("transfer EMPTY --> " + result + " in " + locales);
            return result;
        }
        // 精确匹配
        if (locales.contains(locale)) {
            return locale;
        }

        // 根据语言模糊匹配
        String language = theLocale.getLanguage();
        if (locales.contains(language)) {
            LOG.debug("transfer " + locale + " --> " + language + " in " + locales);
            return language;
        }

        language = language + "_";
        for (String temp : locales) {
            if (temp.startsWith(language)) {
                LOG.debug("transfer " + locale + " --> " + temp + " in " + locales);
                return temp;
            }
        }
        String result = fetchDefault(locales);
        LOG.debug("transfer " + locale + " --> " + result + " in " + locales);
        return result;
    }

    /**
     * 返回默认方言,优先级为en,en_US,zh,zh_CN,如果都不存在,则随机返回一个
     */
    private static String fetchDefault(Set<String> locales) {
        if (locales.contains("en")) {
            return "en";
        } else if (locales.contains("en_US")) {
            return "en_US";
        }
        if (locales.contains("zh")) {
            return "zh";
        } else if (locales.contains("zh_CN")) {
            return "zh_CN";
        }
        return locales.iterator().next();
    }
}