JSF中的国际化,何时使用消息捆绑和资源捆绑? [英] Internationalization in JSF, when to use message-bundle and resource-bundle?

查看:73
本文介绍了JSF中的国际化,何时使用消息捆绑和资源捆绑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时以及如何在faces-config.xml中使用<resource-bundle><message-bundle>标记进行本地化?这两个之间的区别对我来说不是很清楚.

When and how should I use <resource-bundle> and <message-bundle> tags for localization in faces-config.xml? The differences between those two are not very clear to me.

推荐答案

< message-bundle>

只要您要覆盖JSF验证/转换东西使用的JSF默认警告/错误消息,就将使用<message-bundle>. 您可以在例如,如下所示的com.example.i18n程序包中的Messages_xx_XX.properties文件将覆盖默认的required="true"消息:

For example, Messages_xx_XX.properties files in com.example.i18n package as below which overrides the default required="true" message:

com/example/i18n/Messages_en.properties

javax.faces.component.UIInput.REQUIRED = {0}: This field is required

com/example/i18n/Messages_nl.properties

javax.faces.component.UIInput.REQUIRED = {0}: Dit veld is vereist

可以按以下方式配置(没有语言环境说明符_xx_XX和文件扩展名!):

can be configured as follows (without the locale specifier _xx_XX and the file extension!):

<message-bundle>com.example.i18n.Messages</message-bundle>


<资源束>

只要您要注册一个本地化的资源束即可使用<resource-bundle>,该资源束在整个JSF应用程序中都可用,而无需在每个视图中都指定<f:loadBundle>.


<resource-bundle>

The <resource-bundle> is to be used whenever you want to register a localized resource bundle which is available throughout the entire JSF application without the need to specify <f:loadBundle> in every single view.

例如,com.example.i18n软件包中的Text_xx_XX.properties文件如下:

For example, Text_xx_XX.properties files in com.example.i18n package as below:

com/example/i18n/Text_en.properties

main.title = Title of main page
main.head1 = Top heading of main page
main.form1.input1.label = Label of input1 of form1 of main page

com/example/i18n/Text_nl.properties

main.title = Titel van hoofd pagina
main.head1 = Bovenste kop van hoofd pagina
main.form1.input1.label = Label van input1 van form1 van hoofd pagina

可以按以下方式配置(没有语言环境说明符_xx_XX和文件扩展名!):

can be configured as follows (without the locale specifier _xx_XX and the file extension!):

<resource-bundle>
    <base-name>com.example.i18n.Text</base-name>
    <var>text</var>
</resource-bundle>

并在main.xhtml中使用,如下所示:

and be used in main.xhtml as follows:

<h:head>
    <title>#{text['main.title']}</title>
</h:head>
<h:body>
    <h1 id="head1">#{text['main.head1']}</h1>
    <h:form id="form1">
        <h:outputLabel for="input1" value="#{text['main.form1.input1.label']}" />
        <h:inputText id="input1" label="#{text['main.form1.input1.label']}" />
    </h:form>
</h:body>


ValidationMessages(JSR303 Bean验证)

自Java EE 6/JSF 2起,还提供了新的JSR303 Bean验证API,该API由禁用).


ValidationMessages (JSR303 Bean Validation)

Since Java EE 6 / JSF 2, there's also the new JSR303 Bean Validation API which is represented by those @NotNull, Size, @Max, etc annotations of the javax.validation.constraints package. You should understand that this API is completely unrelated to JSF. It is not part of JSF, but JSF just happens to have support for it during validations phase. I.e. it determines and recognizes the presence of a JSR303 implementation (e.g. Hibernate Validator) and then delegates the validation to it (which can be disabled by using <f:validateBean disabled="true"/>, by the way).

根据

As per chapter 4.3.1.1 of the JSR303 specification, the custom JSR303 validation messages file needs to have exactly the name ValidationMessages_xx_XX.properties and it needs to be placed in the root of the classpath (thus, not in a package!).

在以上示例中,文件名中的_xx_XX表示(可选)语言和国家/地区代码.如果完全不存在,则它将成为默认(备用)捆绑包.如果存在该语言,例如_en,则当客户端在 Accept-Language HTTP请求标头.这同样适用于该国,例如_en_US_en_GB.

In the above examples, the _xx_XX in the filename represents the (optional) language and country codes. If this is absent altogether, then it becomes the default (fallback) bundle. If the language is present, e.g. _en, then it'll be used when the client has explicitly requested for this language in the Accept-Language HTTP request header. The same applies to the country, e.g. _en_US or _en_GB.

通常可以在faces-config.xml<locale-config>元素中为消息和资源包指定支持的语言环境.

You can specify the supported locales for both the message and resource bundle generically in <locale-config> element of faces-config.xml.

<locale-config>
    <default-locale>en</default-locale>
    <supported-locale>nl</supported-locale>
    <supported-locale>de</supported-locale>
    <supported-locale>es</supported-locale>
    <supported-locale>fr</supported-locale>
</locale-config>

需要通过<f:view locale>设置所需的语言环境.另请参见 JSF中的本地化,如何记住每个会话而不是每个请求/视图的选定语言环境.

The desired locale needs to be set via <f:view locale>. See also Localization in JSF, how to remember selected locale per session instead of per request/view.

这篇关于JSF中的国际化,何时使用消息捆绑和资源捆绑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆