Java多语言

2016-06-01 10:05:00
admin
原创 200
摘要:Java多语言

一、Java多语言

1、语言包含两个关键字段,lang和country;

2、系统常用语言Locale.CHINA、Locale.PRC、Locale.US;

3、Locale.toLanguageTag使用小横杠拼接语言和国家,中文zh-CN,英文en-US;

4、Locale.toString使用下划线拼接语言和国家,中文zh_CN,英文en_US;

5、ResourceBundle.getBundle(baseName,locale),获取指定语言资源;

6、ResourceBundle是抽象类,包含子类PropertyResourceBundle和ListResourceBundle;

7、baseName是文件名前缀,生成PropertyResourceBundle实例;

8、baseName是类名前缀,生成ListResourceBundle实例;

9、Spring多语言支持org.springframework.web.servlet.i18n;


获取资源规则:

1、baseName通常是i18n/lang资源文件名通常是i18n/lang_lang_country.properties;

2、中文资源是lang_zh_CN.properties,英文资源是lang_en_US.properties,默认资源是lang.properties

3、如果无法找到指定语言资源,将会使用默认语言资源,如果没有默认语言资源,将会使用默认资源;

4、JDK自带的native2ascii转换本地编码到unicode,-encoding指定本地编码,-reverse进行反向转换;

5、JDK支持编码:https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html


Locale常用函数:

1、System.out.println(locale.getDisplayName());
2、System.out.println(locale.getDisplayLanguage());
3、System.out.println(locale.getDisplayCountry());
4、System.out.println(locale.getLanguage());
5、System.out.println(locale.getCountry());
6、System.out.println(locale.toLanguageTag());
7、System.out.println(locale.toString());


ResourceBundle常用函数:

1、getObject(String key),获取对应资源;

2、getString(String key),获取对应资源;

3、getStringArray(String key),获取对应资源;

发表评论
评论通过审核之后才会显示。