Skip to content

Commit 5768273

Browse files
committed
fix: #1528
1 parent c8057ec commit 5768273

3 files changed

Lines changed: 106 additions & 22 deletions

File tree

src/org/nutz/mvc/Mvcs.java

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.nutz.lang.util.Context;
1010
import org.nutz.lang.util.NutMap;
1111
import org.nutz.mvc.config.AtMap;
12+
import org.nutz.mvc.i18n.LocalizationManager;
1213
import org.nutz.mvc.impl.NutMessageMap;
1314
import org.nutz.mvc.ioc.SessionIocContext;
1415

@@ -51,14 +52,25 @@ public abstract class Mvcs {
5152
public static boolean DISABLE_X_POWERED_BY = false;
5253

5354
public static String X_POWERED_BY = "nutz/"+Nutz.version()+" <nutzam.com>";
55+
56+
public static LocalizationManager localizationManager;
57+
58+
public static void setLocalizationManager(LocalizationManager localizationManager) {
59+
Mvcs.localizationManager = localizationManager;
60+
}
5461

5562
// ====================================================================
5663

57-
public static Map<String, Object> getLocaleMessage(String key) {
58-
Map<String, Map<String, Object>> msgss = getMessageSet();
59-
if (null != msgss)
60-
return msgss.get(key);
61-
return null;
64+
public static Map<String, Object> getLocaleMessage(String local) {
65+
if (localizationManager != null) {
66+
return localizationManager.getMessageMap(local);
67+
}
68+
else {
69+
Map<String, Map<String, Object>> msgss = getMessageSet();
70+
if (null != msgss)
71+
return msgss.get(local);
72+
return null;
73+
}
6274
}
6375

6476
/**
@@ -175,24 +187,30 @@ public static String getDefaultLocalizationKey() {
175187
*/
176188
public static void updateRequestAttributes(HttpServletRequest req) {
177189
// 初始化本次请求的多国语言字符串
178-
Map<String, Map<String, Object>> msgss = getMessageSet();
179-
if (msgss == null && !ctx().localizations.isEmpty())
180-
msgss = ctx().localizations.values().iterator().next();
181-
if (null != msgss) {
182-
Map<String, Object> msgs = null;
183-
184-
String lKey = Strings.sBlank(Mvcs.getLocalizationKey(), getDefaultLocalizationKey());
185-
186-
if (!Strings.isBlank(lKey))
187-
msgs = msgss.get(lKey);
188-
189-
// 没有设定特殊的 Local 名字,随便取一个
190-
if (null == msgs) {
191-
if (msgss.size() > 0)
192-
msgs = msgss.values().iterator().next();
190+
if (localizationManager == null) {
191+
Map<String, Map<String, Object>> msgss = getMessageSet();
192+
if (msgss == null && !ctx().localizations.isEmpty())
193+
msgss = ctx().localizations.values().iterator().next();
194+
if (null != msgss) {
195+
Map<String, Object> msgs = null;
196+
197+
String lKey = Strings.sBlank(Mvcs.getLocalizationKey(), getDefaultLocalizationKey());
198+
199+
if (!Strings.isBlank(lKey))
200+
msgs = msgss.get(lKey);
201+
202+
// 没有设定特殊的 Local 名字,随便取一个
203+
if (null == msgs) {
204+
if (msgss.size() > 0)
205+
msgs = msgss.values().iterator().next();
206+
}
207+
// 记录到请求中
208+
req.setAttribute(MSG, msgs);
193209
}
194-
// 记录到请求中
195-
req.setAttribute(MSG, msgs);
210+
}
211+
else {
212+
NutMessageMap msg = localizationManager.getMessageMap(Mvcs.getLocalizationKey());
213+
req.setAttribute(MSG, msg);
196214
}
197215

198216
// 记录一些数据到请求对象中
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.nutz.mvc.i18n;
2+
3+
import java.util.Set;
4+
5+
import org.nutz.mvc.impl.NutMessageMap;
6+
7+
public interface LocalizationManager {
8+
9+
void setDefaultLocal(String local);
10+
11+
String getDefaultLocal();
12+
13+
Set<String> getLocals();
14+
15+
NutMessageMap getMessageMap(String local);
16+
17+
String getMessage(String local, String key);
18+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.nutz.mvc.i18n;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
import org.nutz.mvc.impl.NutMessageMap;
8+
9+
/**
10+
* LocalizationManager的参考实现.
11+
* 可以在MainSetup.init方法内, 通过Mvcs.setLocalizationManager(ioc.get(MyLocalizationManager.class))设置默认实例.
12+
* @author wendal
13+
*
14+
*/
15+
public class DemoLocalizationManager implements LocalizationManager {
16+
17+
protected String defaultLocal;
18+
19+
protected Map<String, NutMessageMap> msgs = new HashMap<String, NutMessageMap>();
20+
21+
public void setDefaultLocal(String local) {
22+
this.defaultLocal = local;
23+
}
24+
25+
public String getDefaultLocal() {
26+
return defaultLocal;
27+
}
28+
29+
public Set<String> getLocals() {
30+
return msgs.keySet();
31+
}
32+
33+
// 如果要动态替换msg, 例如从数据库读取
34+
// 请实现一个NutMessageMap的子类, 覆盖其get方法, 替换为动态实现
35+
public NutMessageMap getMessageMap(String local) {
36+
return msgs.get(local);
37+
}
38+
39+
public String getMessage(String local, String key) {
40+
NutMessageMap map = getMessageMap(local);
41+
if (defaultLocal != null && map == null) {
42+
map = getMessageMap(defaultLocal);
43+
}
44+
if (map == null)
45+
return key;
46+
return (String) map.getOrDefault(key, key);
47+
}
48+
}

0 commit comments

Comments
 (0)