修改jFinal的I18N类为instance方式使用
001public class I18N
002
{
003
private String baseName;
004
private Locale defaultLocale = Locale.getDefault();
005
private int i18nMaxAgeOfCookie = Const.DEFAULT_I18N_MAX_AGE_OF_COOKIE;
006
private static final NullResourceBundle NULL_RESOURCE_BUNDLE = new NullResourceBundle();
007
private static final ConcurrentMap<String, ResourceBundle> bundlesMap = new ConcurrentHashMap<String, ResourceBundle>();
008
009
private I18N() {
010
}
011
012
public static I18N getInstance(String baseName, Locale defaultLocale, Integer i18nMaxAgeOfCookie) {
013
I18N me = new I18N();
014
me.baseName = baseName;
015
me.defaultLocale = defaultLocale;
016
if (i18nMaxAgeOfCookie != null)
017
me.i18nMaxAgeOfCookie = i18nMaxAgeOfCookie;
018
return me;
019
}
020
021
public Locale getDefaultLocale() {
022
return defaultLocale;
023
}
024
025
final public int getI18nMaxAgeOfCookie() {
026
return i18nMaxAgeOfCookie;
027
}
028
029
private ResourceBundle getResourceBundle(Locale locale) {
030
String resourceBundleKey = getresourceBundleKey(locale);
031
ResourceBundle resourceBundle = bundlesMap.get(resourceBundleKey);
032
if (resourceBundle == null) {
033
try {
034
resourceBundle = ResourceBundle.getBundle(baseName, locale);
035
bundlesMap.put(resourceBundleKey, resourceBundle);
036
}
037
catch (MissingResourceException e) {
038
resourceBundle = NULL_RESOURCE_BUNDLE;
039
}
040
}
041
return resourceBundle;
042
}
043
044
private String getresourceBundleKey(Locale locale) {
045
return baseName + locale.toString();
046
}
047
048
public String getText(String key) {
049
return getResourceBundle(defaultLocale).getString(key);
050
}
051
052
public String getText(String key, String defaultValue) {
053
String result = getResourceBundle(defaultLocale).getString(key);
054
return result != null ? result : defaultValue;
055
}
056
057
public String getText(String key, Locale locale) {
058
return getResourceBundle(locale).getString(key);
059
}
060
061
public String getText(String key, String defaultValue, Locale locale) {
062
String result = getResourceBundle(locale).getString(key);
063
return result != null ? result : defaultValue;
064
}
065
066
public Locale localeFromString(String localeStr) {
067
if ((localeStr == null) || (localeStr.trim().length() == 0) || ("_".equals(localeStr))) {
068
return defaultLocale;
069
}
070
071
int index = localeStr.indexOf('_');
072
if (index < 0) {
073
return new Locale(localeStr);
074
}
075
076
String language = localeStr.substring(0, index);
077
if (index == localeStr.length()) {
078
return new Locale(language);
079
}
080
081
localeStr = localeStr.substring(index + 1);
082
index = localeStr.indexOf('_');
083
if (index < 0) {
084
return new Locale(language, localeStr);
085
}
086
087
String country = localeStr.substring(0, index);
088
if (index == localeStr.length()) {
089
return new Locale(language, country);
090
}
091
092
localeStr = localeStr.substring(index + 1);
093
return new Locale(language, country, localeStr);
094
}
095
096
private static class NullResourceBundle extends ResourceBundle {
097
public Enumeration<String> getKeys() {
098
return null; // dummy www.zzzyk.com
099
}
100
protected Object handleGetObject(String key) {
101
return null; // dummy
102
}
103
}
104
}
作者:Alex.yang
补充:综合编程 , 其他综合 ,