【问题标题】:How to internationalize REST Spring-MVC application?如何国际化 REST Spring-MVC 应用程序?
【发布时间】:2014-03-08 08:44:17
【问题描述】:

我是 spring 新手,但我仍然不太了解 spring 的层次结构和术语。我正在用 spring 实现一个 RESTful 应用程序。在搜索并阅读了有关如何使 spring 国际化的信息后,我尝试在我的应用程序中进行操作。但在我看来,它没有正确配置。因为我得到了例外。我想向您展示我的项目结构的屏幕截图。还有我想问你为什么我的applicationContext.xml显示有问题。

spring-servlet.xml

<!-- SPRING INTERNALIZATION CONFIGURATION -->
    <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>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
    </bean>

【问题讨论】:

    标签: spring spring-mvc internationalization


    【解决方案1】:

    ReloadableResourceBundleMessageSource.setBaseNames() 的 javadoc 说:

    设置一个基本名称数组,每个基本名称都遵循不指定文件扩展名或语言代码的基本 ResourceBundle 约定,但与引用 Spring 资源位置的 ResourceBundleMessageSource 形成对比:例如“WEB-INF/messages.properties”、“WEB-INF/messages_en.properties”等的“WEB-INF/messages”。

    但是您已将消息属性文件放在 Maven 项目结构中的 src/main/resources 下,因此它们最终将位于类路径的根目录中(这意味着即使 Spring 在类路径中查找它们,您在配置中使用的 /resources 前缀会阻止 Spring 找到它们)。

    所以将属性文件放在 WEB-INF 下,并使用 WEB-INF/messages 作为 basenames 属性。

    【讨论】:

    • 我更新了我的问题。我再次替换了资源下的消息文件。它现在正在工作。但是,当我发送包含“?language=tr”的请求时,它会从 en 文件中返回消息。请帮忙!!!
    • 这是另一个不同的问题。因此发布另一个不同的问题。并将此答案标记为已接受,因为它解决了您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多