【问题标题】:How to switch between 2 message bundles?如何在 2 个消息包之间切换?
【发布时间】:2014-10-27 13:19:48
【问题描述】:

我在处理我的 webapp 的 i18n 时遇到了一些问题。 如果在另一个消息源中找不到消息,是否可以通过 Spring 在消息源中找到消息?

这是我的 Spring 配置

    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="xxMessage" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

示例: 如果键是“login.user”并且语言环境是“en”,则应用程序会在我的 xx_en.properties 中显示值(如果存在),但如果键不存在,我想在另一个文件(xx_es.properties)中搜索. 这可能吗?

P.S:对不起我的英语:D

提前致谢!

【问题讨论】:

    标签: java spring internationalization


    【解决方案1】:

    我认为你的 spring 配置中需要有拦截器配置

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

    【讨论】:

      【解决方案2】:

      感谢您的回答@DDK,我已经设法解决了这个问题:D

      下面的解决方案。

      定义链式消息源,由接口 HierarchicalMessageSource 提供。

      例如,如果您有 i18n 文件 baseMessages.properties 和 消息属性

      您可以将它们链接为

      <bean id="baseMessageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
          <property name="basename" value="baseMessages" />
      </bean>
      <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
          <property name="basename" value="messages" />
          <property name="parentMessageSource" ref="baseMessageSource" />
      </bean>
      

      【讨论】:

        猜你喜欢
        • 2011-02-19
        • 2016-03-04
        • 1970-01-01
        • 1970-01-01
        • 2021-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多