一、JDK国际化
1):在src下建立两个资源属性文件
test_zh_CN.properties
test_en_US.properties
2):操作资源文件
public class I18nTest
{
public static void main(String[] args)
{
//得到JVM中所有的国家语言 Locale 类
Locale[] locales = Locale.getAvailableLocales();
for(Locale lo:locales)
{
System.out.println(lo.getDisplayCountry()+":"+
lo.getCountry()+":"+lo.getDisplayLanguage()+":"+lo.getLanguage());
}
//得到特定本地化的资源 ,读取资源文件
ResourceBundle bundle = ResourceBundle.getBundle("test");
String value = bundle.getString("hello");
System.out.println(value);
//程序中指定语言环境
ResourceBundle b = ResourceBundle.getBundle("test",Locale.US);
System.out.println(b.getString("hello"));
//给资源文件传递参数
Locale locale = Locale.US;
ResourceBundle b1 = ResourceBundle.getBundle("test",locale);
String v1 = b1.getString("hello");
//要使用给属性文件进行参数赋值需要用到MessageFormat
String r = MessageFormat.format(v1, new Object[]{"zhangsan"});
System.out.println(r);
}
}
二、struts2国际化
对于国际化的资源文件,其命名规则是: package_ 语言名 _ 国家名,比如: package_zh_CN.properties , packate_en_US.properties同时我们可以提供一个默认的资源文件,该文件是当所有的语言都不匹配时使用该资源文件:该名称直接为:package.properties
应用场景:struts2配置校验框架(xml文件中的<message key = ""></message>)