在JSF中使用多个资源包 [英] Using Multiple Resource bundles in JSF
问题描述
我正在尝试从JSF页面访问多个资源包.我有两个资源包:
I am trying to access multiple resource bundles from a JSF page. I have two resource bundles:
-
general_messages.properties
-
module_message.properties
general_messages.properties
module_message.properties
我想访问JSF文件中的这两个资源包.我可以这样做的一种方法是为每个捆绑软件定义特定的属性:
I want to access both these resource bundles in a JSF file. One way I can do this is to define specific properties for each of these bundles:
<f:loadBundle basename="com.sample.general_messages" var="general"/>
<f:loadBundle basename="com.sample.module_message" var="module"/>
有没有一种方法可以使用相同的变量名访问这两个资源包. 像这样:
Is there a way I can access both these resource bundles using the same variable name. Something like:
<f:loadBundle basename="com.sample.general_messages, com.sample.module_message" var="general"/>
还是访问多个资源包的任何其他最佳方法?
Or any other best way to access multiple resource bundles?
推荐答案
您用Spring标记了您的问题,所以我建议您使用Spring MessageSource. Spring MessageSource甚至可以按层次聚合许多属性文件.与旧的Java ResourceBundle
相比,它具有许多优点.
You tagged your question with Spring, so I recommend you using Spring MessageSource. Spring MessageSource can aggregate many property files even hierarchically. It gives you many advantages over old java ResourceBundle
.
您可以像这样在您的spring-config.xml
中定义弹簧MessageSource
:
You can define spring MessageSource
in you spring-config.xml
like this:
<!--
Application messages configuration.
-->
<bean id="messageSource" name="resourceBundle"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:fallbackToSystemLocale="false"
p:cacheSeconds="0">
<property name="basenames">
<list>
<value>/messages/Messages</value>
<!-- <value>${application.messages}</value>-->
</list>
</property>
</bean>
比您可以定义这样扩展ResourceBundle
的Class
(需要一些清理和重构):
Than you can define your Class
which extends ResourceBundle
like this (Needs some cleaning and refactoring):
public class SpringResourceBundle extends ResourceBundle
{
private MessageSource messages;
private FacesContext fc;
private Locale locale = null;
public SpringResourceBundle()
{
fc = FacesContext.getCurrentInstance();
WebApplicationContext webAppCtx = (WebApplicationContext) fc.getExternalContext().getApplicationMap().get(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
messages = (MessageSource) webAppCtx.getBean("messageSource");
}
@Override
public Locale getLocale()
{
Locale loc = fc.getELContext().getLocale();
if (fc.getExternalContext() != null) {
loc = fc.getExternalContext().getRequestLocale();
}
try {
UIViewRoot viewRoot = fc.getViewRoot();
if (viewRoot != null) {
loc = viewRoot.getLocale();
}
if (loc == null) {
loc = fc.getApplication().getDefaultLocale();
}
} catch (Throwable th) {
System.out.println(th.getMessage());
loc = locale;
}
locale = loc;
return loc;
}
@Override
protected Object handleGetObject(String key)
{
try {
return messages.getMessage(key, null, getLocale());
} catch (NoSuchMessageException e) {
return "???" + key + "???";
}
}
@Override
public Enumeration<String> getKeys()
{
return Collections.enumeration(Collections.EMPTY_LIST);
}
}
faces-config.xml
中的最后一个声明了以上Class的资源束.像这样:
Finnaly in faces-config.xml
declare your resource bundle with Class above. Something like this:
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>cs</supported-locale>
<supported-locale>de</supported-locale>
<supported-locale>en</supported-locale>
</locale-config>
<message-bundle>your.package.SpringResourceBundle</message-bundle>
</application>
在这里您使用JSF中的Spring MessageSource.希望这是可以理解的.
Here you go Spring MessageSource in JSF. Hope it's understandable.
这篇关于在JSF中使用多个资源包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!