1、了解缺省Locale是由操作系统决定的,Locale是由语言和国家代码组成
在浏览器上:工具---Internet选项---语言 可以查看Locale
2、国际化资源文件是由baseName+locale组成,如:MessageBundle_en_US.properties
baseName是任意合法的文件名
------比如:
需要提供配置文件,这写命名是由规则的,且放置在classpath能直接能找到的地方,一般放在src文件下
MessagesBundle_en_US.properties--美国
MessagesBundle_zh_CN.properties--中国
3、native2ascii命令的位置和用法
* 位置:JAVA_HOME/bin
* 使用native2ascii.exe o.properties MessagesBundle_zh_CN.properties
4、I18nSimple.java类

Code
package com.bjsxt.i18n;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
public class I18nSample {
public static void main(String[] args) {
/**
* 得到缺省的Locale(两个参数(国家代码、语言代码)),这个与浏览器上的设置无关,是
* 与操作系统(控制面板--语言和区域)有关
*/
Locale defaultLocale = Locale.getDefault();
System.out.println("default country=" + defaultLocale.getCountry());
System.out.println("default language=" + defaultLocale.getLanguage());
//Locale currentLocale = new Locale("en", "US");
//Locale currentLocale = new Locale("zh", "CN");
Locale currentLocale = new Locale("ja", "JP");
//把配置文件加载进来,src/res/MessagesBundle
properties
/**
*
* 用getString方法拿到**.properties文件中k1,k2的值,如果输出中文则需要在***.properties中使用ascci码。
* 转换ascii码,可以用jdk中的navtiv2ascii.exe进行转换,如果需要批量转,在cmd下,进入要转换文件所在的目录,然后
* 敲入navtiv2ascii.exe o.properties MessagesBundle_zh_CN.properties进行转换,在建立国际化文件是,最好
* 建立一个缺省的MessageBundle.properties
*/
ResourceBundle rb = ResourceBundle.getBundle("res.MessagesBundle", currentLocale);
//System.out.println(rb.getString("k1"));
//System.out.println(rb.getString("k2"));
/**
* 运行是生成的,如***.properties
* k1=hello,{0}
* k2=good bye
* 这时涉及到java.text中的MessageFormat类
*/
MessageFormat mf = new MessageFormat(rb.getString("k1"));
System.out.println(mf.format(new Object[]{"Tom"}));
//System.out.println(mf.format(new Object[]{"张三"}));
}
}
5、MessagesBundle_en_US.properties
k1=hello,{0}
k2=good bye
6、MessagesBundle_zh_CN.properties
k1=\u4F60\u597D,{0}
k2=\u518D\u89C1
7、MessageBundle.properties---默认的
k1=hello,{0}
k2=good bye