Java国际化资源绑定-----示例

mess.properties文件:

hello=Hello World!
msg=Hello,{0}!Today is {1}.


mess_en_US.properties

hello=Hello World!
msg=Hello,{0}!Today is {1}.

mess_zh_CN.properties(Properties Editor插件)

hello=您好!
msg=你好,{0}!今天是{1}!

1、简单的资源绑定:

public class LocaleHello {

	public static void main(String[] args) {
		//真正的项目中是这样使用
		//Locale locale = Locale.getDefault();
		
		Locale locale = Locale.US;
		ResourceBundle resourceBundle = ResourceBundle.getBundle("mess", locale);
		System.out.println( resourceBundle.getString("hello") );
		//output:Hello World!
		
		locale = Locale.CHINESE;
		resourceBundle = ResourceBundle.getBundle("mess", locale);
		System.out.println( resourceBundle.getString("hello") );
		//output:您好!

	}

}

2、带占位符的资源绑定:

public class LocaleParameterHello {

	public static void main(String[] args) {
		//真正的项目中是这样使用
		//Locale locale = Locale.getDefault();
		
		Locale locale = Locale.US;
		ResourceBundle resourceBundle = ResourceBundle.getBundle("mess", locale);
		String msg = resourceBundle.getString("msg");
		System.out.println( MessageFormat.format(msg, "GongQiang",new Date()));
		//output:Hello,GongQiang!Today is 11-7-18 下午2:09.
		
		locale = Locale.CHINESE;
		resourceBundle = ResourceBundle.getBundle("mess", locale);
		msg = resourceBundle.getString("msg");
		System.out.println( MessageFormat.format(msg, "龚强",new Date()) );
		//output:你好,龚强!今天是11-7-18 下午2:09!

	}

}


可以看到,使用US本地化,输出的却是CHINESE的时间格式。

这是因为我的操作系统是中文,虽然在取绑定的资源时,我指定为Locale.US。但是对Date而言,它依然是取操作系统默认的Locale!



你可能感兴趣的:(Java国际化资源绑定-----示例)