springMVC国际化

it2022-05-09  35

国际化资源命名规则

基名_en.properties :所有英文语言的资源基名_en_US.properties:针对美国地区、英文语言的资源基名_zh.properties:所有的中文语言的资源基名_zh_CN.properties:针对中国大陆的、中文语言的资源基名_zh_HK.properties:针对中国香港的、中文语言的资源基名.properties: 默认资源文件。如果请求相应语言的资源文件不存在,将使用此资源文件。例如,若是中国大陆地区用户,应该访问“基名_zh_CN.properties”,而如果不存在此文件,就会去访问默认的“基名.properties”。

添加国际化资源文件

配置springmvc.xml文件

<!--加载国际化资源文件,springmvc启动时会自动查找为一个id为 messageSource的bean(固定的)--> <!-- 如果配置了ResourceBundleMessageSource这个类,这个类会在程序响应的时候介入(否则国际化没有效果) --> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="com.znsd.spring.i18/message"></property> </bean> <!--配置LocalResolver用来获取本地化语言 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean> <!--配置LocaleChanceInterceptor拦截器 --> <mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" /> </mvc:interceptors> <!-- 如果配置了mvc-controller会导致其它页面没法正常访问,还需要添加一个标签 --> <mvc:annotation-driven /> <!--如果不配置这个图片的超链接会找不到 --> <mvc:default-servlet-handler/> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="from" uri="http://www.springframework.org/tags/form"%> <%@taglib prefix="tag" uri="http://www.springframework.org/tags" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <from:form action="login" method="post" commandName="user"> <tag:message code="login.name" /><from:input path="name"/> <br/> <tag:message code="login.pass"/><from:password path="pass"/> <br/> <input type="submit" value="<tag:message code="login.submit"/>"/> </from:form> //点击转换成中文 <a href="login?locale=zh_CN">中文</a> //点击转换成英文 <a href="login?locale=en_US">英文</a> </body> </html>

最新回复(0)