spring mvc i18n : If key not in properties file, 如何设置默认语言环境?

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

假设如果我的属性文件之一中没有 key ,则会出现如下异常:

javax.servlet.ServletException:javax.servlet.jsp.JspTagException:在语言环境“en”的代码“label.cancel”下找不到任何消息。

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

最佳答案

例如,您有: 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>
<property name="fallbackToSystemLocale" value="false"/> - 说,如果你没有属性(property)messages_ch_CN.propertiesmessages_en_EN.properties或者在其中一个属性中没有必要的键,例如:title,spring 将使用 messages.properties默认

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

关于spring mvc i18n : If key not in properties file, 如何设置默认语言环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9395491/

相关文章:

php - 将实时站点迁移到 MVC 结构的策略?

java - Spring MVC 请求/ session 作用域 bean 线程安全

java - 在 Spring applicationContext.xml 中使用 P6Spy 和数据源

java - @Test 创建新记录

ios - 如何在 MVC 中测试 Controller 类?

java - 尝试将 Spring 应用程序部署到 Tomcat

java - Spring Boot 中 MessageSource 的 NoSuchMessageException

java - Spring Hibernate 数据库查询不起作用

java - 我可以在spring服务层使用静态变量吗?

java - 从位于服务器文件系统中的外部文件导入 Spring 属性