JSF 2.0中的国际化 [英] Internationalization in JSF 2.0
问题描述
我想知道国际化在jsf中如何工作?我已阅读有关 coreservlets.com 的教程,但是就我而言,它的工作方式略有不同.在该教程中,我必须使用
I'm wondering how internationalization works in jsf? I have read tutorial on coreservlets.com about it, but in my case it works slightly differently. In that tutorial said that i have to use
FacesContext.getCurrentInstance().getViewRoot().setLocale(newLocale);
在actionListener(用于更改语言环境的侦听器)中,并且后备bean必须具有属性getCurrentLocale()
才能在<f:view>
标记中使用它.
in actionListener(listener for changing locale) and also backing bean has to have property getCurrentLocale()
to use it in <f:view>
tag.
我有2个带有消息的属性文件(默认和指定的语言环境),它们在faces-config.xml
中注册. <f:view>
标签我只有一页(index.xhtml)
I have 2 property files with messages(default and with specified locale), they are registered in faces-config.xml
. <f:view>
tag I have only at one page(index.xhtml)
<f:view locale="#{bean.locale}">
...
</f:view>
我还为每个区域设置2个按钮(带有actionListener).在支持Bean中,我只需修改当前的语言环境变量(不要使用getViewRoot().setLocale(newLocale)
).但是所有页面的语言环境都会发生变化(即使它们没有<f:view locale="#{bean.locale}">
)
Also I have 2 buttons(with actionListener) for each locale. In backing bean I simply modify current locale variable(don't use getViewRoot().setLocale(newLocale)
). But locale changes for all pages(even if they don't have <f:view locale="#{bean.locale}">
)
推荐答案
假设您有以下两个消息文件
Lets say you have following two messages files
messages.properties
messages_de.properties
设置应用程序区域设置
共有三种设置应用程序区域设置的方法,我想您需要在这里设置第一个.
1-您可以让浏览器选择语言环境.
在WEB-INF/faces-config.xml:
<faces-config>
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>de</supported-locale>
</locale-config>
</application>
</faces-config>
浏览器连接到您的应用程序时,通常会在HTTP标头中包含一个Accept-Language值
When a browser connects to your application, it usually includes an Accept-Language value in the HTTP header
2-您可以通过编程方式设置区域设置.
调用UIViewRoot对象的setLocale方法:
Call the setLocale method of the UIViewRoot object:
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
viewRoot.setLocale(new Locale("de"));
3-您可以为单个页面设置区域设置
通过将f:view
元素与语言环境属性一起使用-例如:
3-You can set the locale for an individual page
By using the f:view
element with a locale attribute—for example:
<f:view locale="de">
可以动态设置语言环境:
The locale can be dynamically set:
<f:view locale="#{user.locale}"/>
声明消息包
现在已经设置了语言环境,您可以使用以下两种方法之一来声明消息束
Declaring message bundles
Now that the Locale is set you can use one of the following two ways to declare message bundles
1-通过面孔配置 最简单的方法是在应用程序的WEB-INF目录中提供一个名为faces-config.xml的文件,其内容如下:
1-Via faces-config The simplest way is to supply a file named faces-config.xml in the WEB-INF directory of your application, with the following contents:
<?xml version="1.0"?>
<faces-config 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"
version="2.0">
<application>
<resource-bundle>
<base-name>com.corejsf.messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
</faces-config>
2-在每个需要访问它的JSF页面上. 您可以将f:loadBundle元素添加到需要访问该捆绑包的每个JSF页面中,而不是使用全局资源捆绑包声明,如下所示:
2-At each JSF page that needs access it. Instead of using a global resource bundle declaration, you can add the f:loadBundle element to each JSF page that needs access to the bundle, like this:
<f:loadBundle basename="com.corejsf.messages" var="msgs"/>
在任何一种情况下,都可以通过名称为msgs的map变量来访问分发包中的消息.
In either case, the messages in the bundle are accessible through a map variable with the name msgs.
在按钮上显示适当的标签 现在,假设默认属性文件,即英语具有属性
Showing appropriate label on button Now lets say default properties file i.e english has property
next=Next
德国人具有同等学历,即
and German has equivallent i.e
next=Weiter
您已经设置了语言环境并声明了消息包,您可以访问它以将标签放在命令按钮上,例如
And you have set the locale and declared mesg bundle you can access it to put the label on a command button like
<h:commandButton value="#{msgs.next}"/>
以上答案是从 Hortsmen Core Java Server Faces书 .
这篇关于JSF 2.0中的国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!