-
Notifications
You must be signed in to change notification settings - Fork 941
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.nutz.mvc.i18n; | ||
|
||
import java.util.Set; | ||
|
||
import org.nutz.mvc.impl.NutMessageMap; | ||
|
||
public interface LocalizationManager { | ||
|
||
void setDefaultLocal(String local); | ||
|
||
String getDefaultLocal(); | ||
|
||
Set<String> getLocals(); | ||
|
||
NutMessageMap getMessageMap(String local); | ||
|
||
String getMessage(String local, String key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.nutz.mvc.i18n; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.nutz.mvc.impl.NutMessageMap; | ||
|
||
/** | ||
* LocalizationManager的参考实现. | ||
* 可以在MainSetup.init方法内, 通过Mvcs.setLocalizationManager(ioc.get(MyLocalizationManager.class))设置默认实例. | ||
* @author wendal | ||
* | ||
*/ | ||
public class DemoLocalizationManager implements LocalizationManager { | ||
|
||
protected String defaultLocal; | ||
|
||
protected Map<String, NutMessageMap> msgs = new HashMap<String, NutMessageMap>(); | ||
|
||
public void setDefaultLocal(String local) { | ||
this.defaultLocal = local; | ||
} | ||
|
||
public String getDefaultLocal() { | ||
return defaultLocal; | ||
} | ||
|
||
public Set<String> getLocals() { | ||
return msgs.keySet(); | ||
} | ||
|
||
// 如果要动态替换msg, 例如从数据库读取 | ||
// 请实现一个NutMessageMap的子类, 覆盖其get方法, 替换为动态实现 | ||
public NutMessageMap getMessageMap(String local) { | ||
return msgs.get(local); | ||
} | ||
|
||
public String getMessage(String local, String key) { | ||
NutMessageMap map = getMessageMap(local); | ||
if (defaultLocal != null && map == null) { | ||
map = getMessageMap(defaultLocal); | ||
} | ||
if (map == null) | ||
return key; | ||
return (String) map.getOrDefault(key, key); | ||
} | ||
} |