7

试图让 Spring 国际化工作。我使用了 classpath:messages basename,为语言创建了 .properties 文件。它们被正确复制到 web-inf 文件夹,并且代码存在于属性文件中......

这是显示一切的ide,请帮助我。我已经从另一个我已经完成的项目中复制了设置,效果很好。我曾尝试创建大量不同的消息文件,但它没有提取任何东西……图片显示了 web.xml、spring-servlet.xml 和目录结构。

这显示了一切,我看不到我错过了什么

编辑 如果我将bean定义添加到applicationContext而不是spring-servlet它可以工作..?

4

4 回答 4

12

我会去尝试:

如果文件位于WEB-INF/classes目录下,请尝试:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/classes/messages" />
</bean>

并且文件的名称应该是:

  • 消息属性
  • messages_en.properties
  • messages_en_GB.properties

编辑- 最后尝试!

这种编写配置的方式怎么样,在你最后一次评论之后,我在这里闻到了一些味道:

<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.CookieLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>

<mvc:interceptors>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>
于 2012-06-20T11:16:05.620 回答
3

将您的消息属性文件保存在类路径(WEB-INF/classes)之外,并如下定义 bean

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages"/>
<property name="cacheSeconds" value="1"/>

根据文档 ReloadableResourceBundleMessageSource 为您提供了即时更改 spring 通过 cachSeconds 获取的消息的好处。此类与 ResourceBundleMessageSource 的区别仅在于指定资源位置。

于 2012-06-20T12:05:38.373 回答
0

所有配置细节都是正确的,但是做一件事,在子配置文件中配置属性文件,这意味着您的 spring-servlet.xml 将所有属性配置到 applicationContext.xml 中,这意味着父配置文件最大可以工作。试试看,去掉spring-servlet.xml中属性细节的配置............

于 2015-01-12T14:02:52.880 回答
-1

尽管对许多人来说这听起来很愚蠢,但我的代码所犯的错误是我们编写了自己的 MessageSource。这是调用 Spring 的 MessageSource。

但在代码中它就像 (MessageSource(MessageSource))。因此,我们正在查找而不是查找。

删除了额外的调用,它现在可以工作了。

于 2015-04-21T15:43:17.907 回答