jsf*_*sfQ 82 jsf localization internationalization
何时以及如何使用<resource-bundle>和<message-bundle>标记本地化faces-config.xml?这两者之间的差异对我来说不是很清楚.
Bal*_*usC 145
该<message-bundle>每当你要覆盖其被使用的JSF验证/转换的东西JSF默认的警告/错误信息将被使用.您可以在JSF规范的第2.5.2.4章中找到默认警告/错误消息的键.
例如,下面的包中的Messages_xx_XX.properties文件com.example.i18n会覆盖默认required="true"消息:
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和文件扩展名!):
<message-bundle>com.example.i18n.Messages</message-bundle>
该<resource-bundle>每当你想注册一个本地化资源束,其在整个JSF应用程序可用而不需要指定要使用的<f:loadBundle>在每一个单一视图.
例如,包中的Text_xx_XX.properties文件com.example.i18n如下:
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和文件扩展名!):
<resource-bundle>
    <base-name>com.example.i18n.Text</base-name>
    <var>text</var>
</resource-bundle>
并用于main.xhtml如下:
<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>
由于Java EE 6/JSF 2,这里还有其由那些代表的新的JSR303 Bean验证API @NotNull,Size,@Max等所述的注释javax.validation.constraints包.您应该了解此API 与 JSF 完全无关.它不是JSF的一部分,但JSF恰好在验证阶段支持它.即它确定并识别JSR303实现的存在(例如Hibernate Validator),然后将验证委托给它(<f:validateBean disabled="true"/>顺便说一下,可以通过使用来禁用它).
根据JSR303规范的第4.3.1.1章,自定义JSR303验证消息文件需要具有完全名称ValidationMessages_xx_XX.properties,并且需要将其放置在类路径的根目录中(因此,不在包中!).
在上面的例子中,_xx_XX文件名中的(可选的)语言和国家代码.如果完全不存在,则它将成为默认(回退)捆绑包.如果语言存在,例如_en,当客户端在Accept-LanguageHTTP请求头中明确请求该语言时,将使用该语言.这同样适用于该国家,例如_en_US或_en_GB.
您可以在<locale-config>元素中一般为消息和资源包指定支持的语言环境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中的本地化,如何记住每个会话的选定区域设置,而不是每个请求/视图.