欢乐编程-知识培训-Struts1.x学习-I18N国际化-006
快乐编程-知识培训-Struts1.x学习-I18N国际化-006
目标:学会使用struts1.x的国际化操作
说明:项目或产品“见人说人话,见鬼说鬼话”,多语言版本需求
对象:适合自学者、初学者、兴趣爱好者。
理念: 先行动(Coding),后理解(Thinking) ; 在最短的时间内采取最大量的Coding 。 分享越多,收获越大
--------------------------------------------------------
一、在strutsConfig.xml配置文件中配置
<!-- ================ Message Resources Definitions ================ --> <message-resources parameter="MessageResources" />
二、编写多语言版本的资源文件
MessageResources.properties(默认资源文件)
raky.train.login.page.title=\u767B\u5F55\u9875\u9762 raky.train.login.form.username=\u7528 \u6237\uFF1A raky.train.login.form.password=\u5BC6 \u7801\uFF1A raky.train.login.form.button.submit=\u786E\u5B9A raky.train.login.form.button.reset=\u91CD\u7F6E raky.train.login.form.back=\u8FD4\u56DE raky.train.success.page.title=\u6210\u529F\u9875\u9762 raky.train.failure.page.title=\u5931\u8D25\u9875\u9762 raky.train.failure.page.error=\u7528\u6237\u540D\u6216\u5BC6\u7801\u9519\u8BEF\u5566
MessageResources_zh_CN.properties(中文资源文件)
raky.train.login.page.title=\u767B\u5F55\u9875\u9762 raky.train.login.form.username=\u7528 \u6237\uFF1A raky.train.login.form.password=\u5BC6 \u7801\uFF1A raky.train.login.form.button.submit=\u786E\u5B9A raky.train.login.form.button.reset=\u91CD\u7F6E raky.train.login.form.back=\u8FD4\u56DE raky.train.success.page.title=\u6210\u529F\u9875\u9762 raky.train.failure.page.title=\u5931\u8D25\u9875\u9762 raky.train.failure.page.error=\u7528\u6237\u540D\u6216\u5BC6\u7801\u9519\u8BEF\u5566
MessageResources_en_US.properties(美式英语资源文件)
raky.train.login.page.title=login page raky.train.login.form.username=username: raky.train.login.form.password=password: raky.train.login.form.button.submit=submit raky.train.login.form.button.reset=reset raky.train.login.form.back=back raky.train.success.page.title=success page raky.train.failure.page.title=failure page raky.train.failure.page.error=username or password is error
注意:1、资源文件的命名方式;2、中文需要编码转换(方法一,在eclipse中写,自动转换;方法二:利用%JAVA_HOM%\bin\native2ascii工具手动转换)
native2ascii 工具将带有本机编码字符(非拉丁、数字和非单一码字符)的文件转换成带有Unicode编码字符的文件,也可以将Unicode编码的文件转换为本地编码的文件 用法: native2ascii [-reverse] [-encoding encoding] [inputfile [outputfile]] 正向转换 native2ascii xxx.properties yyy.properties 反向转换 native2ascii yyy.properties xxx.properties (其中xxx.properties包含中文)
三、在页面使用资源文件
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %> <%@ taglib uri="http://struts.apache.org/tags-bean-el" prefix="bean-el"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><bean-el:message key="raky.train.login.page.title"/></title> </head> <body> <html-el:form action="/login" method="post"> <table> <tr> <td><bean-el:message key="raky.train.login.form.username"/></td> <td><html-el:text property="username" maxlength="40" size="18" /></td> </tr> <tr> <td><bean-el:message key="raky.train.login.form.password"/></td> <td><html-el:password property="password" maxlength="40" size="18" /></td> </tr> <tr> <td colspan="2" align="center"> <html-el:submit property="submitValue" ><bean-el:message key="raky.train.login.form.button.submit"/></html-el:submit> <html-el:reset property="resetValue" ><bean-el:message key="raky.train.login.form.button.reset"/></html-el:reset> </td> </tr> </table> </html-el:form> </body> </html>
四、在浏览器中配置language,每个浏览器会有差异,比如FireFox 工具-->选项-->内容-->语言
本案例仅限于中文和英文,如果支持其他语言,需要自定义相关语言的资源文件。