如何重新加载 JSF ResourceBundle?
我目前正在设计一个需要以多种不同语言提供的应用程序,经过大量研究后,我恢复使用 resouceBundles。我正在使用 java se 1.6 和 java ee 6 以及 jsf 1.2 和 glassfish 3.1..
我从互联网上得到了以下代码,并以一种有意义的方式排列它..现在的问题是,ResourceBundle 加载正常,但是它没有刷新..我尝试了很多不同的 ResourceBundle.clearCache() 方法,但这些方法似乎都不起作用...
请注意,在 Control 类中,我正在检查区域设置并加载数据以适应 那。这稍后将被数据库支持的系统取代。这只是测试代码,以确保我的底层结构正常工作...
CODE_SAMPLE:LocalTest.java - 资源包
public class LocalTester extends ResourceBundle {
protected static final String BUNDLE_NAME = "local.lantest.LocalTester";
protected static final Control UTF8_CONTROL = new LocalTesterControl();
public LocalTester() {
setParent(ResourceBundle.getBundle(BUNDLE_NAME, FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
}
@Override
protected Object handleGetObject(String key) {
return parent.getObject(key);
}
@Override
public Enumeration<String> getKeys() {
return parent.getKeys();
}
public static Control getUTF8_CONTROL() {
return UTF8_CONTROL;
}
}
CODE_SAMPLE:LocalTestControl.java - 资源包控制
public class LocalTesterControl extends Control {
@Override
public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) {
return true;
}
@Override
public long getTimeToLive(String baseName, Locale locale) {
return TTL_DONT_CACHE;
}
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
System.out.println("---------------------------LOADING LOCALE PACK-------------------------------------");
System.out.println("Printing baseName = " + baseName);
System.out.println("Printing locale.getLanguage() = " + locale.getLanguage());
System.out.println("Printing locale.getDisplayLanguage() = " + locale.getDisplayLanguage());
ResourceBundle rbdl;
if (locale.getLanguage().compareTo("fr") == 0) {
System.out.println("------- LOAD FRENCH language pack load");
rbdl = new ListResourceBundle() {
@Override
protected Object[][] getContents() {
Object[][] aa = {
{"welcome", "FR- Welcome" + (new Date().toString())},
{"message", "FR- message"},
{"signoff", "FR- Sign Off"},
{"parametWelcome", "FR-Para Meter Welcome... {0} is parameter..."},
{"numberParaFormat", "FR-NUMBER parameter = {0}...."}
};
return aa;
}
};
} else if (locale.getLanguage().compareTo("es") == 0) {
System.out.println("------- LOAD ESPANIOLA language pack load");
rbdl = new ListResourceBundle() {
@Override
protected Object[][] getContents() {
Object[][] aa = {
{"welcome", "ES- Welcome" + (new Date().toString())},
{"message", "ES- message"},
{"signoff", "ES- Sign Off"},
{"parametWelcome", "ES-Para Meter Welcome... {0} is parameter..."},
{"numberParaFormat", "ES-NUMBER parameter = {0}...."}
};
return aa;
}
};
} else {
System.out.println("------- LOAD DEFAULT language pack load");
rbdl = new ListResourceBundle() {
@Override
protected Object[][] getContents() {
Object[][] aa = {
{"welcome", "EN- Welcome" + (new Date().toString())},
{"message", "EN- message"},
{"signoff", "EN- Sign Off"},
{"parametWelcome", "EN-Para Meter Welcome... {0} is parameter..."},
{"numberParaFormat", "EN-NUMBER parameter = {0}...."},
{"notrans", "EN- No Language Translation"}
};
return aa;
}
};
}
return rbdl;
}
}
最后是 Faces 配置:
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<resource-bundle>
<base-name>local.lantest.LocalTester</base-name>
<var>bundi</var>
</resource-bundle>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>es</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
</application>
</faces-config>
任何帮助和建议都非常有价值。
I am currently designing an application that needs to be available in so many different languages and after much research I reverted in to using resouceBundles. I am using java se 1.6 and java ee 6 with jsf 1.2 and glassfish 3.1..
I got following codes from all over the internet and arranged it in a manner to make sense.. Now the question is, the ResourceBundle loads alright, however it doesnt get refreshed.. i tried so many different ResourceBundle.clearCache() methods but none of those seems to be working...
Please note in the Control class I am checking the locale and loading the data to fit that. This will be later replaced by a database backed system. This is only test code to make sure i have the underlying structure working...
CODE_SAMPLE: LocalTest.java - The Resource Bundle
public class LocalTester extends ResourceBundle {
protected static final String BUNDLE_NAME = "local.lantest.LocalTester";
protected static final Control UTF8_CONTROL = new LocalTesterControl();
public LocalTester() {
setParent(ResourceBundle.getBundle(BUNDLE_NAME, FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
}
@Override
protected Object handleGetObject(String key) {
return parent.getObject(key);
}
@Override
public Enumeration<String> getKeys() {
return parent.getKeys();
}
public static Control getUTF8_CONTROL() {
return UTF8_CONTROL;
}
}
CODE_SAMPLE: LocalTestControl.java - The Resource Bundle Control
public class LocalTesterControl extends Control {
@Override
public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) {
return true;
}
@Override
public long getTimeToLive(String baseName, Locale locale) {
return TTL_DONT_CACHE;
}
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
System.out.println("---------------------------LOADING LOCALE PACK-------------------------------------");
System.out.println("Printing baseName = " + baseName);
System.out.println("Printing locale.getLanguage() = " + locale.getLanguage());
System.out.println("Printing locale.getDisplayLanguage() = " + locale.getDisplayLanguage());
ResourceBundle rbdl;
if (locale.getLanguage().compareTo("fr") == 0) {
System.out.println("------- LOAD FRENCH language pack load");
rbdl = new ListResourceBundle() {
@Override
protected Object[][] getContents() {
Object[][] aa = {
{"welcome", "FR- Welcome" + (new Date().toString())},
{"message", "FR- message"},
{"signoff", "FR- Sign Off"},
{"parametWelcome", "FR-Para Meter Welcome... {0} is parameter..."},
{"numberParaFormat", "FR-NUMBER parameter = {0}...."}
};
return aa;
}
};
} else if (locale.getLanguage().compareTo("es") == 0) {
System.out.println("------- LOAD ESPANIOLA language pack load");
rbdl = new ListResourceBundle() {
@Override
protected Object[][] getContents() {
Object[][] aa = {
{"welcome", "ES- Welcome" + (new Date().toString())},
{"message", "ES- message"},
{"signoff", "ES- Sign Off"},
{"parametWelcome", "ES-Para Meter Welcome... {0} is parameter..."},
{"numberParaFormat", "ES-NUMBER parameter = {0}...."}
};
return aa;
}
};
} else {
System.out.println("------- LOAD DEFAULT language pack load");
rbdl = new ListResourceBundle() {
@Override
protected Object[][] getContents() {
Object[][] aa = {
{"welcome", "EN- Welcome" + (new Date().toString())},
{"message", "EN- message"},
{"signoff", "EN- Sign Off"},
{"parametWelcome", "EN-Para Meter Welcome... {0} is parameter..."},
{"numberParaFormat", "EN-NUMBER parameter = {0}...."},
{"notrans", "EN- No Language Translation"}
};
return aa;
}
};
}
return rbdl;
}
}
And finally the Faces config:
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<resource-bundle>
<base-name>local.lantest.LocalTester</base-name>
<var>bundi</var>
</resource-bundle>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>es</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
</application>
</faces-config>
Any help and advise is much valued.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以执行以下操作:
编辑您的 jsf 并添加:
您可以在以下网站中看到区域设置列表: http://www.science.co.il/language/locale-codes.asp
you can do something like this:
Edit your jsf and add:
You can see a locale list in the following website: http://www.science.co.il/language/locale-codes.asp