Spring MVC - 获取错误的消息包

发布于 2024-12-16 13:32:04 字数 1208 浏览 0 评论 0原文

我在使用 Spring MVC 消息包时遇到了一个奇怪的问题:获取了错误的消息包文件。我已经仔细检查过,在我的 Java 控制器类中,我有 fr_FR 语言环境,但 Spring 标签 (appContext.getMessage(code, null, locale); 在类中以及)给我回复英文信息!

到底是怎么回事?

我正在为 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"/> 
...

我的消息的路径如下所示:

development:

  • /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
  • /webapps/MY_APP/WEB-INF/classes/messages_fr.properties

I have a strange problem using Spring MVC message bundles: the wrong message bundle file is being fetched. I have double-checked, and in my Java controller class I have the fr_FR locale, but Spring tags (appContext.getMessage(code, null, locale); in the class as well) return me English messages!

What is going on?

I am developing portlets for Liferay Portal. Let me show you parts of my code:

in applicationContext.xml:

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

in my JSPs I have code looking like this:

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

and the paths to my messages look like this:

development:

  • /src/main/webapp/WEB-INF/classes/messages.properties (English, default)
  • /src/main/webapp/WEB-INF/classes/messages_fr.properties

deployed in Tomcat

  • /webapps/MY_APP/WEB-INF/classes/messages.properties
  • /webapps/MY_APP/WEB-INF/classes/messages_fr.properties

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岛徒 2024-12-23 13:32:04

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

Edit -> Preferences ->内容->语言

并使用上移或下移按钮将 fr_FR 作为首选。

这使得浏览器发送带有首选区域设置的请求。

Try setting the language (to fr_FR ) in your browser. In firefox version that I use, its at

Edit ->Preferences -> Content -> Langauges

And use the move-up or move-down buttons to have fr_FR as the top preference.

This makes the browser send requests with the prefered locale set.

太阳公公是暖光 2024-12-23 13:32:04

当我的本地化消息文件有错误的 unicode 字符(
\uXXX 形式)。

Java、Spring和Tomcat都没有写过相关的信息,根本没有任何反馈。

修复文件后,一切突然开始工作。

问候

I had a similar issue when my localized message file had a bad unicode character (the
\uXXX form).

Neither Java, Spring nor Tomcat did write an information about it, no feedback at all.

After fixing the file, everything suddenly started to work.

Regards

噩梦成真你也成魔 2024-12-23 13:32:04

首先,您必须确保 liferay 支持您想要的区域设置。此外,您还必须在每个 portlet 的 portlet.xml 中添加支持的区域设置。

portlet.xml

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

如果您错过了两者之一,它将无法工作。检查您是否也为此 portlet 添加了受支持的区域设置。

Frist you have to make sure that liferay supports the locale you want. In addition you have to add your supported locales in the portlet.xml for each portlet.

portlet.xml:

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

If you miss one of both it just won't work. Check if you added the supported-locale for this portlet as well.

淡淡離愁欲言轉身 2024-12-23 13:32:04

您必须在 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>

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

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

PPS 有用的教程在这里:
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 之类的前缀。确保你拥有它。

You must have an interceptor in applicationContext like

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

You also need

<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"/>

On the first JSP page of my project I gave a choice for languages like that:

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

Please tell me if this helped you.

P.S. My messages_*.properties files are in a source folder src/main/resources, not in webapp, I don't know if this matters.

P.P.S. Useful tutorial here:
http://springbyexample.org/examples/basic-webapp-internationalization.html

I would like to add also that at the beginning of your xml you should have stuff like:

<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">

It is necessary in order to recognize the prefixes such as mvc. Make sure you have it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文