【问题标题】:JSF - message bundle always uses default properties fileJSF - 消息包始终使用默认属性文件
【发布时间】:2012-07-29 20:04:03
【问题描述】:

我无法让语言环境工作。

系统

EAR 和 WAR

玻璃鱼:3.12
Hibernate 4.2 - EAR 和 WAR 上的 jar
莫哈拉:2.1.11
Primefaces:3.3
Omnifaces:1.1

源文件夹中的属性文件:

messages_de.properties messages.properties

faces-config.xml:

<locale-config>
    <default-locale>en</default-locale>
    <supported-locale>de</supported-locale>
</locale-config>
<message-bundle>messages</message-bundle>
<resource-bundle>
    <base-name>messages</base-name>
    <var>msg</var>
</resource-bundle>

问题:

示例标签和使用消息包的所有其他文本都是错误的:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html">
    <f:view locale="de">
        <h:head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        </h:head>
        <h:body>
            <h:form>
                <h:outputLabel for="postalcode" value="#{msg.postalcode_u}: *"/>
                <h:inputText id="postalcode" label="#{msg.postalcode_u}" required="true"/>
                <h:commandButton value="#{msg.save_u}"/>
            </h:form>
        </h:body>
    </f:view>
</html>

必需的验证消息:默认验证消息使用正确的语言。

错误消息:Postalcode: Überprüfungsfehler: Wert ist erforderlich.
应该是:Postleitzahl: Überprüfungsfehler: Wert ist erforderlich.

设置:

Glassfish 3.1.2
Mojarra 2.1.6 和 2.1.11 测试

编辑:

1.
使用FacesContext.getCurrentInstance().getViewRoot().getLocale() 查看相同的消息资源包以显示一些自定义面孔消息不起作用。

2.
我还有第二个消息包,目前也在源文件夹中,只能由 bean 访问以显示错误消息:

errorMessages_de.properties
errorMessages_en.properties

在 bean 中创建的那些错误消息已使用 FacesContext.getCurrentInstance().getViewRoot().getLocale()! 正确本地化!

3.
使用以下配置“强制”德语仍然不起作用。仍然从messages.properties 而非messages_de.properties 检索消息。

<locale-config>
    <default-locale>de</default-locale>
    <supported-locale>de</supported-locale>
</locale-config>

新编辑:

当我启动服务器时,我收到此警告:

Unexpected exception when attempting to tear down the Mojarra runtime
java.lang.NullPointerException
    at javax.faces.component.UIViewRoot.setLocale(UIViewRoot.java:1463)
    at com.sun.faces.config.InitFacesContext.getViewRoot(InitFacesContext.java:213)
    at com.sun.faces.application.ApplicationImpl.invokeViewListenersFor(ApplicationImpl.java:2026)
    at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:291)
    at com.sun.faces.config.ConfigureListener.contextDestroyed(ConfigureListener.java:335)
    at org.apache.catalina.core.StandardContext.contextListenerStop(StandardContext.java:4840)
    at org.apache.catalina.core.StandardContext.stop(StandardContext.java:5584)
    at com.sun.enterprise.web.WebModule.stop(WebModule.java:527)
    at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1049)
    at com.sun.enterprise.web.WebContainer.unloadWebModule(WebContainer.java:2229)
    at com.sun.enterprise.web.WebContainer.unloadWebModule(WebContainer.java:2184)
    at com.sun.enterprise.web.WebApplication.stop(WebApplication.java:159)
    at org.glassfish.internal.data.EngineRef.stop(EngineRef.java:169)
    at org.glassfish.internal.data.ModuleInfo.stop(ModuleInfo.java:302)
    at org.glassfish.internal.data.ApplicationInfo.stop(ApplicationInfo.java:329)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.unload(ApplicationLifecycle.java:998)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.undeploy(ApplicationLifecycle.java:1024)
    at org.glassfish.deployment.admin.UndeployCommand.execute(UndeployCommand.java:330)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:348)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:363)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1085)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:95)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1291)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1259)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:461)
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:212)
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:179)
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$Hk2DispatcherCallable.call(ContainerMapper.java:354)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

编辑2:

上面的错误信息与问题无关。请参阅我自己的答案。

【问题讨论】:

  • 无法在 Mojarra 2.1.10 中重现您的问题。您是否在某个请求范围的后备 bean 中使用错误的语言环境做一个明确的UIViewRoot#setLocale()?这可能会解释奇怪的行为。
  • 更新到 mojarra 2.1.11 仍然是同样的问题。不,我没有在任何地方设置它。 (有一个语言环境会话 bean,但现在删除了它以进行测试。)我什至创建了一个新的 xhtml 站点,其中仅包含我在一个简单表单中发布的代码。还删除了 glassfish 缓存并删除了应用程序并重新添加了它们。示例消息没有意义。服务器设置会导致这种行为吗?语言环境在一段时间前确实有效。当它停止工作时没有注意到。
  • 你没有把这个例子过于简单化,你实际上&lt;f:view locale="#{someBean.locale}"&gt;吗?
  • 我之前删除了它进行测试。我只有一个使用&lt;f:view locale="#{localeBean.locale}">的模板文件。我的浏览器语言也设置为de
  • @BalusC 我在编辑段落中添加了更多信息,哪些有效,哪些无效,以及有时会打印的异常的 stracktrace。

标签: jsf internationalization locale resourcebundle


【解决方案1】:

属性文件中有一个条目值使用 utf-8 编码转义了德语变音符号。

ü -&gt; \u00FC

问题是,我错过了最后一个 C,这使得属性文件不知何故无法读取!

不敢相信没有打印出适当的错误消息。如果它不能被转义,为什么不像在许多情况下那样打印值。

但该警告消息仍然存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    相关资源
    最近更新 更多