【问题标题】:spring mvc i18n: If key not in properties file, how to set default locale?spring mvc i18n:如果键不在属性文件中,如何设置默认语言环境?
【发布时间】:2012-03-12 19:43:32
【问题描述】:

假设如果我的一个属性文件中没有密钥,我会收到如下异常:

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: No message found under code 'label.cancel' for locale 'en'。

假设如果它在我的 messages_ch_CN.properties 中不可用,是否有任何方法可以在该文件中不存在它时检查 messages_en_En 文件。 或者是否有任何解决方法已经实施。

【问题讨论】:

    标签: spring model-view-controller spring-mvc internationalization


    【解决方案1】:

    一种解决方法是将您正在使用的messageSource 子类化(即ReloadableResourceBundleMessageSource)并覆盖它的resolveCode(),这样:

    • 它查找指定代码和区域设置的消息(通过调用super.resolveCode(code, locale))。
    • 如果它没有找到它,那么它会寻找具有默认语言环境的一个(通过调用super.resolveCode(code, defaultLocale))。

    然后将新创建的类用作messageSource

    【讨论】:

    【解决方案2】:

    例如,您有:2 种语言

    messages_ch_CN.properties  /*in the property file lang=Chinese*/
    messages_en_EN.properties  /*in the property file lang=English*/
    messages.properties  /*in the property file lang=English default*/
    

    messages.properties 这是默认属性,它始终包含整个应用程序中使用的每个键。

    NAME-servlet.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context-3.2.xsd
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
        <context:component-scan base-package="com.example.controller"/>
    
        <mvc:annotation-driven/>
        <mvc:resources mapping="/resources/**" location="/resources/"/>
    
        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basename" value="classpath:com/example/i18n/messages"/>
            <property name="fallbackToSystemLocale" value="false"/>
            <property name="defaultEncoding" value="UTF-8"/>
        </bean>
    
        <mvc:interceptors>
            <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
                <property name="paramName" value="lang"/>
            </bean>
        </mvc:interceptors>
    
        <bean id="localeResolver"
              class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
            <property name="defaultLocale" value="en"/>
        </bean>
    
    </beans>
    

    &lt;property name="fallbackToSystemLocale" value="false"/&gt; - 表示,如果您没有属性 messages_ch_CN.propertiesmessages_en_EN.properties 或在其中一个属性中没有必要的键,例如:标题,spring 将使用 messages.properties 作为默认值

    如果&lt;property name="fallbackToSystemLocale" value="true"/&gt; - spring 使用部署环境的用户语言环境,但它不好,因为用户的部署环境,可以与我们提供的语言不同。如果用户的语言环境与我们提供的语言不同,spring 使用messages.properties

    【讨论】:

      【解决方案3】:

      始终提供一个默认的messages 文件(没有locale 规范),它始终包含整个应用程序中使用的每个键。如果不对ResourceBundleimplementations 之一进行子类化,就不可能自动寻找另一个locale 而不是所选的@。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-13
        • 2014-09-14
        • 2012-02-07
        • 1970-01-01
        • 2016-09-07
        • 1970-01-01
        • 2018-06-13
        相关资源
        最近更新 更多