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.openo.baseservice.i18n.DefaultErrorCodeI18n.ErrorCodeLevelUtil;
19 import org.openo.baseservice.i18n.DefaultErrorCodeI18n.ErrorItem2;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import java.util.Locale;
25 import java.util.Optional;
27 public interface ErrorCodeI18n {
29 static final Logger logger = LoggerFactory.getLogger(ErrorCodeI18n.class);
32 * Access to the internationalization of the error code examples, scanning the process of all
33 * classes load path, loading all the *-errorcode-*.json files, and load all the international
34 * language information
38 static ErrorCodeI18n getInstance() {
39 return DefaultErrorCodeI18n.getInstance();
43 * Gets the corresponding error item based on the error code (including the error description
44 * information for all languages)
47 * @return Optional<ErrorItem>
49 public Optional<ErrorItem> getErrorItem(int errorCode);
51 public static interface ErrorItem {
53 public int getErrorCode();
55 public int getLevel();
57 public Map<String, String> getLabels();
59 public String getLabel(Locale theLocale);
62 * All language error information description of assembly. <br>
64 * some modules to store all the error information description or transfer, and finally the time
65 * to choose the appropriate value according to the locale presentation. <br>
67 * it is not necessary to pass the error code (code), but in order to take into account the
68 * subsequent scalability, so also passed, the general module does not need this information.
70 * { "code":53501, "level":"INFO", "errlabels":{"zh_CN":"拓扑定制文件无效。","en_US":"The topology
71 * customized file is invalid.","ru_RU":"Топология настроенный файл недействительно."} }
76 public String getCanonicalLabels(int errorCode);
79 * With the use of the above interface, it is possible to obtain an international string
80 * corresponding to a specific language from the combination of all error messages
82 * @param labels Error message description string (Return value of the getCanonicalLabels
87 public static String getLabelFromCanonicalLabels(String labels, Locale theLocale) {
88 if (labels == null || theLocale == null) {
92 ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
93 Map<String, String> errlabels = errorItem2.getErrlabels();
94 return errlabels.get(I18nLocaleTransfer.transfer(theLocale, errlabels.keySet()));
95 } catch (Exception e) {
97 "getLabelFromCanonicalLabels failed from " + labels + " with local " + theLocale, e);
103 * With the use of the above interface, we can get the error code corresponding to the specific
104 * language from all the error information
106 * @param labels Error message description string (Return value of the getCanonicalLabels
111 public static int getErrorcodeFromCanonicalLabels(String labels, Locale theLocale) {
112 if (labels == null) {
116 ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
117 return errorItem2.getErrorCode();
118 } catch (Exception e) {
120 "getErrorcodeFromCanonicalLabels failed from " + labels + " with local " + theLocale,
127 * With the above interface, the error level of the corresponding string is obtained from all
130 * @param labels Error message description string (Return value of the getCanonicalLabels
133 * @return error level
135 public static int getLevelFromCanonicalLabels(String labels, Locale theLocale) {
136 if (labels == null) {
140 ErrorItem2 errorItem2 = I18nJsonUtil.getInstance().readFromJson(labels, ErrorItem2.class);
141 return ErrorCodeLevelUtil.transfer2Int(errorItem2.getLevel());
142 } catch (Exception e) {
144 "getErrorcodeFromCanonicalLabels failed from " + labels + " with local " + theLocale,