假设如果我的属性文件之一中没有 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.properties
或 messages_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/