jsp的国际化
action的国际化
类型转换错误信息的国际化
输入校验的国际化
一个Locale由两部分组成,一个是country,一个是language。
//得到本机安装的地区
Locale[] locales = Locale.getAvailableLocales();
for(Locale locale : locales) {
// 中国 CN
// System.out.println(locale.getDisplayCountry() + " : " + locale.getCountry());
// 中文 zh
System.out.println(locale.getDisplayLanguage() + " : " + locale.getLanguage());
}
src目录下建立国际化文件,文件名格式如下
baseName_language_contry.properties
hellofile_zh_CN.properties
hello=/u4f60/u597d : {0}
hellofile_en_US.properties
hello=hello : {0}
{0}由程序动态传进来,用来被替换掉
使用国际化文件
import java.util.*;
public class Test2 {
public static void main(String[] args) {
Locale locale = Locale.getDefault(); //获得本地的local
Locale locale = Locale.US;
ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);
String value = bundle.getString("hello");
System.out.println(value);
}
}
替换国际化文件中的占位符
import java.util.*;
import java.text.*;
public class Test3 {
public static void main(String[] args) {
Locale locale = Locale.US;
ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);
//将国际化文件中的value值原封不动的输出
String value = bundle.getString("hello");
//用Object数组中的值来替换value中的占位符,{0}代表下标
String result = MessageFormat.format(value,new Object[]{"北京"});
System.out.println(result);
}
}
struts2中应用国际化
struts.xml
//message就是国际化文件名中的baseName
<constant name="struts.custom.i18n.resoutces" value="message"></constant>
register2.jsp
<body>
<!-- struts2国际化的显示方式,用text标签 -->
<!-- 若国际化文件中有此ket则显示对应的value
若无则直接显示name中的值 -->
<s:text name="addUser"></s:text>
Add User <!-- 原始页面显示方式 -->
</body>
message_en_US.properties
addUser=Add User Information
message_zh_CH.properties
addUser=XXX