2011-11-15 47 views
3

我有一个奇怪的问题,使用Spring MVC消息包:正在提取错误的消息包文件。我已经仔细检查过了,在我的Java控制器类中,我有fr_FR语言环境,但Spring标记(类中的appContext.getMessage(code, null, locale);)也会将英文消息返回给我!Spring MVC - 提取错误的消息包

这是怎么回事?

我正在开发Liferay Portal的Portlet。让我告诉你我的代码部分:

applicationContext.xml中

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basenames"> 
     <list> 
      <value>messages</value> 
     </list> 
    </property> 
</bean> 

在我的JSP我一直在寻找这样的代码:

... 
<spring:message code="button.help"/> 
... 

和路径我的消息是这样的:

开发:

  • /src/main/webapp/WEB-INF/classes/messages.properties(英语,默认)
  • /src/main/webapp/WEB-INF/classes/messages_fr.properties

部署在Tomcat的

  • /webapps/MY_APP/WEB-INF/classes/messages.properties
  • /WEBA pps/MY_APP/WEB-INF/classes/messages_fr.properties
+0

这个问题可以有3个原因:未加载法国属性文件,或由于某种原因标签使用英语的地方,或frensh消息文件不包含密钥。 – Ralph

+0

最奇怪的是,只有这个特定的portlet本地化不起作用。每个其他的portlet工作得很好。两者都有相同的密钥 - 没有一个丢失。 – Queequeg

+0

我不熟悉Portlet,但是您配置了本地拦截器吗?春天Referrence章15.6 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-localeresolver – Ralph

回答

1

我有一个类似的问题时,我的本地化消息文件有一个坏的Unicode字符( \ UXXX形式)。

无论是Java中,春天也Tomcat的确实写了一个关于它的信息,没有反馈可言。

固定文件后,一切突然开始工作。

Regards

+0

谢谢,这解决了我的问题。我有一个糟糕的性格 – Queequeg

1

尝试在浏览器中设置语言(to fr_FR)。在Firefox的版本,我用的,它在

Edit ->Preferences -> Content -> Langauges

,并使用移动式或移动向下按钮有fr_FR时作为最高优先级。

这使得浏览器使用首选语言环境集发送请求。

+0

但我有它设置为法语。每个可能的调试输出都打印出我选择了fr_FR语言环境(通过http请求)! 如果我删除messages.properties文件,我会收到无法找到消息的错误。 – Queequeg

+0

我猜你的属性文件名应该是messages_fr_FR.properties代替messages_fr.properties – stratwine

+0

我都试过,太多,它不会改变任何东西。 – Queequeg

0

您必须拥有的applicationContext像

<mvc:interceptors> 
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
     <property name="paramName" value="lang" /> 
    </bean> 
</mvc:interceptors> 

拦截您还需要

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="classpath:messages" /> 
    <property name="defaultEncoding" value="UTF-8" /> 
</bean> 

<bean id="localeResolver" 
     class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> 

在我的项目的第一个JSP页面我给了一个选择。这样的语言:

<span style="float: right"> <a href="?lang=en">en</a> | <a 
     href="?lang=fr">fr</a> </span> 

请告诉我这是否有帮助您。

P.S.我的消息_ *。properties文件位于源文件夹src/main/resources中,而不是在webapp中,我不知道这是否重要。

P.P.S.有用的教程在这里: http://springbyexample.org/examples/basic-webapp-internationalization.html

我想补充一点,在你的XML的开始,你应该有这样的东西:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:security="http://www.springframework.org/schema/security" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

这是必要的,以便识别前缀,如MVC。确保你有它。

+0

属性名称=“basename”或** basenames **? – Queequeg

+0

它是基本名称,而不是基准名称。 – user998692

0

首先您必须确保liferay支持所需的语言环境。另外,您必须在每个Portlet的portlet.xml中添加受支持的语言环境。

portlet.xml

<portlet> 
    ... 
    <supported-locale>en</supported-locale> 
    <supported-locale>fr</supported-locale> 
    ... 
</portlet> 

如果你错过的都只是将无法工作之一。检查是否为此portlet添加了受支持的语言环境。