1.国际化:是开发支持多语言和数据格式的应用程序技术
2.本地化:这是将国际化应用程序改成支持特定语言区域(locale)的技术
1.语言区域
java.util.Locale类表示一个语言区域
public Locale(java.lang.String language)
public Locale(java.lang.String language,java.lang.String country)
public Locale(java.lang.String language,java.lang.String country,java.lang.String variant)
Locale locale = new Locale("en","CA");
Locale locale = Locale.CANADA._FRENCH;
Locale locale = Locale.getDefaule();
2.国际化SpringMVC应用程序
1.将文本元件隔离成属性文件
2.选择和读取正确的属性文件
2.1 将文本文件格里成属性文件
每个文件都包含KEY/VALUE,并且KEY唯一
KEY始终是字符串,VALUE可以是字符串,也可以是任意对象
java.util.ResourceBundle类
它能使你轻松地选择和读取特定于用户语言区域的属性,以及查找值
命名方式:
基准名_语言码_国家码
MyResources_en_US.properties
2.2 选择和读取正确的属性文件
public static ResourceBundle getBundle(java.lang.String baseName);
public static ResourceBundle getBundle(java.lang.String baseName,Locale locale);
ResourceBundle rb = ResourceBundle.getBundle("MyResources",Locale.US);
利用ResourceBundle类的getString()方法传入一个key
public java.lang.String getString(java.lang.String key);
在SpringMVC,不直接使用ResourceBundle,而是利用messageSource bean告诉SpringMVC
将要将属性文件保存在哪里
1.使用ReloadableResourceBundleMessageSource在应用程序目录下搜索这些属性文件
1 <bean id="messageSource" 2 class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 3 <property name="basename" value="/WEB-INF/resource/messages" /> 4 </bean>
2.而使用ResourceBundleMessageSource时,属性文件则必须放在类路径,WEB-INF/class下
1 1 <bean id="messageSource" 2 2 class="org.springframework.context.support.ResourceResourceBundleMessageSource"> 3 3 <property name="basename" value="/WEB-INF/resource/messages" /> 4 4 </bean>
3.告诉SpringMVC使用哪个语言区域
指定使用哪个语言区域
1 <bean id="localResolver" class="org.springframework.web.servlet.i81n.AcceptHeaderLocaleResolver'> 2 </bean>
4.使用message标签
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
5.范例
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/mvc 9 http://www.springframework.org/schema/mvc/spring-mvc.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd"> 12 13 <context:component-scan base-package="app10a.controller" /> 14 <context:component-scan base-package="app10a.formatter" /> 15 16 <mvc:annotation-driven conversion-service="conversionService" /> 17 18 <mvc:resources mapping="/css/**" location="/css/" /> 19 <mvc:resources mapping="/*.html" location="/" /> 20 21 <bean id="viewResolver" 22 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 23 <property name="prefix" value="/WEB-INF/jsp/" /> 24 <property name="suffix" value=".jsp" /> 25 </bean> 26 27 <bean id="conversionService" 28 class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> 29 30 <property name="formatters"> 31 <set> 32 <bean class="app10a.formatter.DateFormatter"> 33 <constructor-arg type="java.lang.String" value="MM-dd-yyyy" /> 34 </bean> 35 </set> 36 </property> 37 </bean> 38 39 <bean id="messageSource" 40 class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 41 <property name="basenames" > 42 <list> 43 <value>/WEB-INF/resource/messages</value> 44 <value>/WEB-INF/resource/labels</value> 45 </list> 46 </property> 47 </bean> 48 49 <bean id="localeResolver" 50 class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"> 51 </bean> 52 53 54 </beans>
messageSource:实现国际化
localeResolver:利用localeResolver Bean利用AcceptHeaderLocaleResolver类实现消息的本地化
1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 2 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4 <!DOCTYPE HTML> 5 <html> 6 <head> 7 <title><spring:message code="page.productform.title"/></title> 8 <style type="text/css">@import url("<c:url value="/css/main.css"/>");</style> 9 </head> 10 <body> 11 12 <div id="global"> 13 Current Locale : ${pageContext.response.locale} 14 <br/> 15 accept-language header: ${header["accept-language"]} 16 17 <form:form commandName="product" action="product_save" method="post"> 18 <fieldset> 19 <legend><spring:message code="form.name"/></legend> 20 <p> 21 <label for="name"><spring:message code="label.productName" text="default text" />:</label> 22 <form:input id="name" path="name" cssErrorClass="error"/> 23 <form:errors path="name" cssClass="error"/> 24 </p> 25 <p> 26 <label for="description"><spring:message code="label.description"/>: </label> 27 <form:input id="description" path="description"/> 28 </p> 29 <p> 30 <label for="price"><spring:message code="label.price" text="default text" />: </label> 31 <form:input id="price" path="price" cssErrorClass="error"/> 32 </p> 33 <p id="buttons"> 34 <input id="reset" type="reset" tabindex="4" 35 value="<spring:message code="button.reset"/>"> 36 <input id="submit" type="submit" tabindex="5" 37 value="<spring:message code="button.submit"/>"> 38 </p> 39 </fieldset> 40 </form:form> 41 </div> 42 </body> 43 </html>
为了测试国际化,修改浏览器accept-language标签
Tools>Internet Options>General(tab)>Languages>Languages Preference中修改