1、了解缺省Locale是由操作系統決定的,Locale是由語言和國家代碼組成
2、國際化資源文件是由baseName+locale組成,如:MessageBundle_en_US.properties
baseName是任意合法的文件名
3、native2ascii命令的位置和用法
* 位置:JAVA_HOME/bin
* 使用native2ascii.exe o.properties MessagesBundle_zh_CN.properties
1
package com.bjsxt.i18n;
2
3
import java.text.MessageFormat;
4
import java.util.Locale;
5
import java.util.ResourceBundle;
6
7
public class I18nSample {
8
9
public static void main(String[] args) {
10
11
Locale defaultLocale = Locale.getDefault();//獲取默認的本地化
12
System.out.println("default country=" + defaultLocale.getCountry());
13
System.out.println("default language=" + defaultLocale.getLanguage());
14
15
//Locale currentLocale = new Locale("en", "US");//通過初始化指定語言和國際本地化
16
//Locale currentLocale = new Locale("zh", "CN");
17
18
Locale currentLocale = new Locale("ja", "JP");
19
20
ResourceBundle rb = ResourceBundle.getBundle("res.MessagesBundle", currentLocale);//獲取本地化配置
21
//System.out.println(rb.getString("k1"));//通過配置文件中的key顯示
22
//System.out.println(rb.getString("k2"));
23
24
MessageFormat mf = new MessageFormat(rb.getString("k1"));//通過站位符顯示指定的信息
25
System.out.println(mf.format(new Object[]{"Tom"}));//對占位符填充
26
//System.out.println(mf.format(new Object[]{"張三"}));
27
}
28
}
29
o.properties文件
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

1
k1=你好,{0}
2
k2=再見
缺省的properties文件MessagesBundle.properties
2

1
k1=hello,{0}
2
k2=good bye
英文文件MessagesBundle_en_US.properties
2

1
k1=hello,{0}
2
k2=good bye
中文文件MessagesBundle_zh_CN.properties
2

1
k1=\u4f60\u597d,{0}
2
k2=\u518d\u89c1
3

2

3
