Java国际化使用
Locale
是表示语言和国际的类,是国际化应用的基础
语言完整版:http://www.loc.gov/standards/iso639-2/php/English_list.php
国家完整版:https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes(科学上网)
https://blog.csdn.net/qq_16393511/article/details/92794742(网友)
语言 | 代码 |
---|---|
中文 | zh |
英语 | en |
国家 | 代码 |
---|---|
中国大陆 | CN |
中国台湾 | TW |
中国香港 | HK |
英国 | EN |
美国 | US |
加南大 | CA |
//语言+地区 = 中国大陆 中文
Locale dl = new Locale("zh", "CN");
//中国台湾 中文
Locale tw = new Locale("zh", "TW");
//等于zh CN
Locale china = Locale.CHINA;
//只有语言
new Locale("zh");
//等于 new Locale("zh")
Locale chinese = Locale.CHINESE;
//获得本地系统本地化
Locale aDefault = Locale.getDefault();
可以根据jvm参数修改本地的环境
-Duser.language=en -Duser.region=Us
本地化工具类
NumberFormat,DateFormat,MessageFormat
//MessageFormat 给不同地区的钱加上货币符号
Locale china = Locale.CHINA;
Locale us = Locale.US;
NumberFormat currencyInstance = NumberFormat.getCurrencyInstance(china);
NumberFormat currencyInstance1 = NumberFormat.getCurrencyInstance(us);
double amt = 10.999d;
System.out.println(currencyInstance.format(amt)); //¥11.00
System.out.println(currencyInstance1.format(amt)); //$11.00
//根据不同地区显示 显示不同的时间格式
DateFormat dateInstance = DateFormat.getDateInstance(DateFormat.MEDIUM, china);
DateFormat dateInstance1 = DateFormat.getDateInstance(DateFormat.MEDIUM, us);
System.out.println(dateInstance.format(new Date()));//2023年1月7日
System.out.println(dateInstance1.format(new Date()));//Jan 7, 2023
//根据不同的地区,可以格式化字符串
String patter = "{0},{1,time,long},{2,number,currency}";
Object[] para = {"cyz",new Date(),1.0E3};
MessageFormat messageFormat = new MessageFormat(patter,china);
MessageFormat messageFormat1 = new MessageFormat(patter,us);
System.out.println(messageFormat.format(para));//cyz,CST 上午12:18:55,¥1,000.00
System.out.println(messageFormat1.format(para));//cyz,12:18:55 AM CST,$1,000.00
配置文件本地化 ResourceBundle
配置文件命名规范
[资源名] _ [语言代码] _ [国家/地区代码].properties
默认资源名
[资源名] .properties
定义了一个资源名为resource的多语言配置
简单的使用,配置文件内容
resource_en_US.properties
greeting.common=How are you
greeting.morning= good moring {0},give me {1,number,currency}
resource_zh_CN.properties
greeting.common=你好
greeting.morning= 早上好啊 {0},给我 {1,number,currency}
resource_cyz_CYZ.properties
greeting.common=#¥%!@#
greeting.morning= #¥%!@ {0},#¥%!@ {1,number,currency}
使用代码
Locale china = Locale.CHINA;
Locale us = Locale.US;
//乱设置用来测试
Locale cyz = new Locale("cyz", "CYZ");
//第一个参数为相对路径 +/资源名 就是文件的前缀
//如果resource123_en_US.properties 下面的就要写resource123
ResourceBundle bundleChina = ResourceBundle.getBundle("resource", china);
ResourceBundle bundleUs = ResourceBundle.getBundle("resource", us);
ResourceBundle bundleCyz = ResourceBundle.getBundle("resource", cyz);
Object[] para = {"cxk",10.11d};
String formatChina = new MessageFormat(bundleChina.getString("greeting.morning"), china).format(para);
String formatUs = new MessageFormat(bundleUs.getString("greeting.morning"), us).format(para);
String formatCyz = new MessageFormat(bundleCyz.getString("greeting.morning"), cyz).format(para);
System.out.println(formatChina);//早上好啊 cxk,给我 ¥10.11
System.out.println(formatUs);//good moring cxk,give me $10.11
System.out.println(formatCyz);//#¥%!@ cxk,#¥%!@ ¤ 10.11 没有实际的国家找不到货币符号
在java9之前中文不能直接明文写在配置文件,写完后要用java指令native2ascii进行转码才能使用
如果对应的语言+地区找不到配置文件,就会默认寻找其他的配置文件,最后会找[资源名] .properties,如果还找不到抛出异常