【问题标题】:Internationalization using SPRING MVC使用 SPRING MVC 进行国际化
【发布时间】:2014-06-27 09:28:39
【问题描述】:

下面是项目结构,显示了我保存消息文件的位置:

  • 网络
  • Java 资源
  • src/main/resources
  • messages_en.properties
  • messages_fr.properties

我定义消息源的 Bean 配置文件如下所示:

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

我对国际化这个概念不熟悉。所以除了如果你把你的 .properies 文件放在标签内,它会被挑选出来并反映变化之外,不要知道太多。但是我在处理多个 .properties 文件时遇到了问题。 说如果我有多个 .properties 文件,那么我应该如何实施语言环境更改?程序如何知道要获取哪个文件?

【问题讨论】:

    标签: java spring internationalization


    【解决方案1】:

    你应该使用:

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

    Spring 将在您调用消息包时使用指定的本地,因此您不必在包的声明中指定它。

    注意:我认为您必须为文件命名: messages_fr_FR.properties 或 messages_en_EN.properties。

    编辑:

    你可以指定一个本地使用:

    messageSource.getMessage("messageKey", args, Locale.FRANCE);
    

    使用messages_fr_FR

    messageSource.getMessage("messageKey", args,LocaleContextHolder.getLocale());
    

    使用用户选择的本地。

    希望对你有帮助

    【讨论】:

    • 我怀疑应用程序如何知道要获取哪个文件。我需要传递某种参数还是需要更改浏览器语言才能看到更改。
    • 这取决于你如何调用消息包我将在我的帖子中添加示例。
    • 写一个控制器来查看变化很重要。
    • 可以,但您也可以从视图中调用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多