1 package com.github.mygreen.cellformatter.lang;
2
3 import java.util.Locale;
4 import java.util.Map;
5 import java.util.concurrent.ConcurrentHashMap;
6
7
8
9
10
11
12
13
14
15
16
17
18
19 public class MSLocale {
20
21 private static final MessageResolvergeResolver.html#MessageResolver">MessageResolver messageResolver = new MessageResolver("com.github.mygreen.cellformatter.locale");
22
23
24
25
26
27
28 private static Map<Integer, MSLocale> KNOWN_LOCALES = new ConcurrentHashMap<>();
29
30
31 public static final MSLocale JAPANESE = MSLocale.createKnownLocale(0x0411);
32
33
34 public static final MSLocale US = MSLocale.createKnownLocale(0x0409);
35
36
37 public static final MSLocale UK = MSLocale.createKnownLocale(0x0809);
38
39
40 public static final MSLocale CANADA = MSLocale.createKnownLocale(0x2809);
41
42
43 public static final MSLocale GERMAN = MSLocale.createKnownLocale(0x0407);
44
45
46 public static final MSLocale FRENCE = MSLocale.createKnownLocale(0x040C);
47
48
49 public static final MSLocale CANADA_FRENCH = MSLocale.createKnownLocale(0x0C0C);
50
51
52 public static final MSLocale ITALY = MSLocale.createKnownLocale(0x0410);
53
54
55 public static final MSLocale KOREA = MSLocale.createKnownLocale(0x0412);
56
57
58 public static final MSLocale PRC = MSLocale.createKnownLocale(0x0804);
59
60
61 public static final MSLocale TAIWAN = MSLocale.createKnownLocale(0x0404);
62
63
64 private final int id;
65
66
67 private final String hexId;
68
69
70 private String code;
71
72
73 private String language;
74
75
76 private String country;
77
78
79 private String name;
80
81
82 private Locale locale;
83
84
85
86
87
88
89
90 public static boolean isKnownById(int id) {
91 final String hexId = Utils.supplyZero(Integer.toHexString(id).toUpperCase(), 4);
92
93 String code = messageResolver.getMessage(String.format("locale.%s.code", hexId));
94 return Utils.isNotEmpty(code);
95 }
96
97
98
99
100
101 public MSLocale(final int id) {
102 this.id = id;
103
104
105 final String hexId = Utils.supplyZero(Integer.toHexString(id).toUpperCase(), 4);
106 this.hexId = hexId;
107
108 }
109
110
111
112
113
114
115
116
117 public static MSLocale createKnownLocale(final int id) {
118
119 if(KNOWN_LOCALES.containsKey(id)) {
120 return KNOWN_LOCALES.get(id);
121 }
122
123 final MSLocaleatter/lang/MSLocale.html#MSLocale">MSLocale locale = new MSLocale(id);
124
125
126 final String hexId = Utils.supplyZero(Integer.toHexString(id).toUpperCase(), 4);
127
128 String code = messageResolver.getMessage(String.format("locale.%s.code", hexId));
129 if(Utils.isEmpty(code)) {
130 return null;
131 }
132
133 locale.code = code;
134 locale.language = messageResolver.getMessage(String.format("locale.%s.language", hexId));
135 locale.country = messageResolver.getMessage(String.format("locale.%s.country", hexId));
136 locale.name = messageResolver.getMessage(String.format("locale.%s.name", hexId));
137
138
139 String jid = messageResolver.getMessage(String.format("locale.%s.jid", hexId));
140 if(Utils.isNotEmpty(jid)) {
141 locale.locale = parseLocale(jid);
142 }
143
144
145 KNOWN_LOCALES.put(id, locale);
146
147 return locale;
148
149 }
150
151 private static Locale parseLocale(final String jid) {
152
153 String[] split = jid.split("_");
154 if(split.length == 1) {
155 return new Locale(split[0]);
156
157 } if(split.length == 2) {
158 return new Locale(split[0], split[1]);
159
160 } else if(split.length == 3) {
161 return new Locale(split[0], split[2]);
162
163 }
164
165 return null;
166
167 }
168
169
170
171
172
173
174 public boolean isSystemDate() {
175 return getHexId().equalsIgnoreCase("F800");
176 }
177
178
179
180
181
182
183 public boolean isSystemTime() {
184 return getHexId().equalsIgnoreCase("F400");
185 }
186
187
188
189
190
191 public int getId() {
192 return id;
193 }
194
195
196
197
198
199
200 public String getHexId() {
201 return hexId;
202 }
203
204
205
206
207
208 public String getCode() {
209 return code;
210 }
211
212
213
214
215
216 public String getLanguage() {
217 return language;
218 }
219
220
221
222
223
224 public String getCountry() {
225 return country;
226 }
227
228
229
230
231
232 public String getName() {
233 return name;
234 }
235
236
237
238
239
240
241 public String getName(final Locale locale) {
242 return messageResolver.getMessage(locale, String.format("locale.%s.name", hexId));
243 }
244
245
246
247
248
249
250 public Locale getLocale() {
251 return locale;
252 }
253
254 @Override
255 public int hashCode() {
256 final int prime = 31;
257 int result = 1;
258 result = prime * result + id;
259 return result;
260 }
261
262 @Override
263 public boolean equals(Object obj) {
264 if(this == obj)
265 return true;
266 if(obj == null)
267 return false;
268 if(getClass() != obj.getClass())
269 return false;
270 MSLocale./../../../../com/github/mygreen/cellformatter/lang/MSLocale.html#MSLocale">MSLocale other = (MSLocale) obj;
271 if(id != other.id)
272 return false;
273 return true;
274 }
275
276 }