forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI18n.java
More file actions
262 lines (222 loc) · 10.2 KB
/
I18n.java
File metadata and controls
262 lines (222 loc) · 10.2 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package act.i18n;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* 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.
* #L%
*/
import act.Act;
import act.act_messages;
import act.app.App;
import act.util.ActContext;
import org.osgl.$;
import org.osgl.exception.NotAppliedException;
import org.osgl.logging.LogManager;
import org.osgl.logging.Logger;
import org.osgl.util.C;
import org.osgl.util.S;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
public class I18n {
private static final Logger logger = LogManager.get(I18n.class);
public static final String DEF_RESOURCE_BUNDLE_NAME = "messages";
public static Locale locale() {
ActContext context = ActContext.Base.currentContext();
return null != context ? context.locale(true) : Act.appConfig().locale();
}
public static String i18n(String msgId, Object... args) {
return _i18n(true, locale(), DEF_RESOURCE_BUNDLE_NAME, msgId, args);
}
public static String i18n(boolean ignoreError, String msgId, Object... args) {
return _i18n(ignoreError, locale(), DEF_RESOURCE_BUNDLE_NAME, msgId, args);
}
public static String i18n(Locale locale, String msgId, Object... args) {
return _i18n(true, locale, DEF_RESOURCE_BUNDLE_NAME, msgId, args);
}
public static String i18n(boolean ignoreError, Locale locale, String msgId, Object... args) {
return _i18n(ignoreError, locale, DEF_RESOURCE_BUNDLE_NAME, msgId, args);
}
public static String i18n(Class<?> bundleSpec, String msgId, Object... args) {
return _i18n(true, locale(), bundleSpec.getName(), msgId, args);
}
public static String i18n(boolean ignoreError, Class<?> bundleSpec, String msgId, Object... args) {
return _i18n(ignoreError, locale(), bundleSpec.getName(), msgId, args);
}
public static String i18n(Locale locale, Class<?> bundleSpec, String msgId, Object... args) {
return _i18n(true, locale, bundleSpec.getName(), msgId, args);
}
public static String i18n(boolean ignoreError, Locale locale, Class<?> bundleSpec, String msgId, Object... args) {
return _i18n(ignoreError, locale, bundleSpec.getName(), msgId, args);
}
private static String _i18n(boolean ignoreError, Locale locale, String bundleName, String msgId, Object... args) {
if (null == msgId) {
return "";
}
if (bundleName.startsWith("act.")) {
bundleName = act_messages.class.getName();
}
ResourceBundle bundle;
try {
bundle = ResourceBundle.getBundle(bundleName, $.requireNotNull(locale), Act.app().classLoader());
} catch (MissingResourceException e) {
return msgId;
}
String msg = msgId;
if (ignoreError) {
if (bundle.containsKey(msgId)) {
msg = bundle.getString(msgId);
}
} else {
try {
msg = bundle.getString(msgId);
} catch (MissingResourceException e) {
logger.warn("Cannot find i18n message key: %s", msgId);
}
}
int len = args.length;
if (len > 0) {
Object[] resolvedArgs = new Object[len];
for (int i = 0; i < len; ++i) {
Object arg = args[i];
if (arg instanceof String) {
resolvedArgs[i] = _i18n(true, locale, bundleName, (String) arg);
} else {
resolvedArgs[i] = arg;
}
}
MessageFormat formatter = new MessageFormat(msg, locale);
msg = formatter.format(resolvedArgs);
}
return msg;
}
public static String i18n(Enum<?> msgId) {
return i18n(locale(), msgId);
}
public static String i18n(Locale locale, Enum<?> msgId) {
return i18n(locale, DEF_RESOURCE_BUNDLE_NAME, msgId);
}
public static String i18n(Class<?> bundleSpec, Enum<?> msgId) {
return i18n(locale(), bundleSpec, msgId);
}
public static String i18n(Locale locale, Class<?> bundleSpec, Enum<?> msgId) {
return i18n(true, locale, bundleSpec.getName(), msgId);
}
public static String i18n(Locale locale, String bundleName, Enum<?> msgId) {
// #333 try `enum.transaction_type.pay_work=Pay work` first
String key = S.newBuffer("enum.").append(S.underscore(msgId.getDeclaringClass().getSimpleName())).append(".").append(msgId.name().toLowerCase()).toString();
String resolved = _i18n(true, locale, bundleName, key);
if (key == resolved || S.blank(resolved)) {
// not found? then try `enum.transactiontype.pay_work=Pay work`
key = S.newBuffer("enum.").append(msgId.getDeclaringClass().getSimpleName().toLowerCase()).append(".").append(msgId.name().toLowerCase()).toString();
resolved = _i18n(true, locale, bundleName, key);
}
return resolved;
}
public static Map<String, Object> i18n(Class<? extends Enum> enumClass) {
return i18n(locale(), enumClass);
}
public static Map<String, Object> i18n(Locale locale, Class<? extends Enum> enumClass) {
return i18n(locale, DEF_RESOURCE_BUNDLE_NAME, enumClass);
}
public static Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass) {
return i18n(locale(), bundleSpec, enumClass);
}
public static Map<String, Object> i18n(Locale locale, Class<?> bundleSpec, Class<? extends Enum> enumClass) {
return i18n(locale, bundleSpec.getName(), enumClass);
}
public static Map<String, Object> i18n(Locale locale, String bundleName, Class<? extends Enum> enumClass) {
return i18n(locale, bundleName, enumClass, false);
}
public static Map<String, Object> i18n(Class<? extends Enum> enumClass, boolean outputProperties) {
return i18n(locale(), enumClass, outputProperties);
}
public static Map<String, Object> i18n(Locale locale, Class<? extends Enum> enumClass, boolean outputProperties) {
return i18n(locale, DEF_RESOURCE_BUNDLE_NAME, enumClass, outputProperties);
}
public static Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass, boolean outputProperties) {
return i18n(locale(), bundleSpec, enumClass, outputProperties);
}
public static Map<String, Object> i18n(Locale locale, Class<?> bundleSpec, Class<? extends Enum> enumClass, boolean outputProperties) {
return i18n(locale, bundleSpec.getName(), enumClass, outputProperties);
}
public static Map<String, Object> i18n(Locale locale, String bundleName, Class<? extends Enum> enumClass, boolean outputProperties) {
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
for (Enum<?> enumInstance : enumClass.getEnumConstants()) {
String name = enumInstance.name();
String val = i18n(locale, bundleName, enumInstance);
if (outputProperties) {
Map<String, Object> values = new HashMap<>();
map.put(name, values);
values.put("message", val);
Map<String, $.Function<Object, Object>> getters = enumPropertyGetters(enumClass);
for (Map.Entry<String, $.Function<Object, Object>> entry : getters.entrySet()) {
values.put(entry.getKey(), entry.getValue().apply(enumInstance));
}
} else {
map.put(name, val);
}
}
return map;
}
private static Set<String> standardsAnnotationMethods = C.newSet(C.list("declaringClass", "hashCode", "toString", "ordinal", "name", "class"));
private static ConcurrentMap<Class<? extends Enum>, Map<String, $.Function<Object, Object>>> enumPropertyGetterCache;
public static void classInit(App app) {
enumPropertyGetterCache = app.createConcurrentMap();
}
private static Map<String, $.Function<Object, Object>> enumPropertyGetters(Class<? extends Enum> enumClass) {
Map<String, $.Function<Object, Object>> map = enumPropertyGetterCache.get(enumClass);
if (null == map) {
Map<String, $.Function<Object, Object>> newMap = buildEnumPropertyGetters(enumClass);
map = enumPropertyGetterCache.putIfAbsent(enumClass, newMap);
if (null == map) {
map = newMap;
}
}
return map;
}
private static Map<String, $.Function<Object, Object>> buildEnumPropertyGetters(Class<? extends Enum> enumClass) {
Map<String, $.Function<Object, Object>> map = new HashMap<>();
for (final Method method : enumClass.getMethods()) {
if (void.class == method.getReturnType() || Void.class == method.getReturnType() || Modifier.isStatic(method.getModifiers()) || method.getParameterTypes().length > 0) {
continue;
}
String name = propertyName(method);
if (standardsAnnotationMethods.contains(name)) {
continue;
}
map.put(method.getName(), new $.Function<Object, Object>() {
@Override
public Object apply(Object o) throws NotAppliedException, $.Break {
return $.invokeVirtual(o, method);
}
});
}
return map;
}
private static String propertyName(Method method) {
String name = method.getName();
int len = name.length();
if (len > 3 && name.startsWith("get")) {
name = S.lowerFirst(name.substring(3));
} else if (len > 2 && name.startsWith("is")) {
name = S.lowerFirst(name.substring(2));
}
return name;
}
}