JAVA中所涉及的类
Locale类、ResourceBundle、MessageFormat
Locale类主要获取本地系统环境相关的信息
Locale的使用事例如下:
Locale[] local=Locale.getAvailableLocales();
for (Locale locale : local) {
System.out.println("国别:"+locale.getCountry()+"语言:"+locale.getLanguage());
}
ResourceBundle主要用来读取资源文件,形如i18n_zh_CN.properties
ResourceBundle使用事例如下:
ResourceBundle bundle=ResourceBundle.getBundle("i18n",locale);
String value=(String) bundle.getObject("username");
MessageFormat主要用来对信息进行格式转换:
MessageFormat使用事例如下
String newValue=MessageFormat.format(value, new Object[]{"你好"});
System.out.println(newValue);
完整事例如下:
本例演示如何读取带参数的资源文件进行国际化操作
Locale locale=Locale.CHINA;
ResourceBundle bundle=ResourceBundle.getBundle("i18n",locale);
String value=(String) bundle.getObject("username");
String newValue=MessageFormat.format(value, new Object[]{"你好"});
System.out.println(newValue);
i18n_zh_CN.properties文件内容如下:
username=java/u6768/:{0}