目的:实现国际化多语言开发
properties文件里写不同国家的语言,我这里会出现除英文以外乱码的问题,还没解决,先用不同的文字代替
一 先写出来一种写法
step1 定义properties文件
定义格式:基名_语言_国家.properties
结构如下:
welcome_ny_NYNYNY.properties::
wel=Welcome to visit--NYNYNY
welcome_zh_CN.properties
wel=Welcome to visit--CN
welcome.properties (后面找不到路径就走默认的这个)
wel=Non-existent
step2 测试类
package test;
import java.util.Locale;
import java.util.ResourceBundle;
public class Test {
public static void main(String[] args) {
ResourceBundle bundle1 = ResourceBundle.getBundle("prop.welcome",new Locale("ny","NYNYNY"));
String str = bundle1.getString("wel");
System.out.println(str);
ResourceBundle bundle13 = ResourceBundle.getBundle("prop.welcome",new Locale("zh","CN"));
String strCN3 = bundle13.getString("wel");
System.out.println(strCN3);
//找不到 走 默认的这个 welcome.properties
ResourceBundle bundle3 = ResourceBundle.getBundle("prop.welcome",new Locale("jp","XXX") );
String str3 = bundle3.getString("wel");
System.out.println(str3);
}
}
结果:
备注:基名_语言_国家.properties
"prop.welcome": prop是包名,welcome是properties文件名的基名
new Locale()的第一个参数是properties文件名的语言
new Locale()的第二个参数是properties文件名的国家
二 不同的写法
properties的格式还可以写成:基名_语言.properties
welcome_en.properties(只有语言没有国家)
wel=Welcome to visit--English
测试类
package test;
import java.util.Locale;
import java.util.ResourceBundle;
public class Test2 {
public static void main(String[] args) {
//方法一:language 和 country 自定义
ResourceBundle bundle1 = ResourceBundle.getBundle("prop.welcome",new Locale("ny","NYNYNY"));
String str = bundle1.getString("wel");
System.out.println(str);
System.out.println("=============================================");
//方法二: 按照 Locale 类 里来写language 和 country
ResourceBundle bundle11 = ResourceBundle.getBundle("prop.welcome",Locale.CHINA);
String strCN1 = bundle11.getString("wel");
System.out.println(strCN1);
ResourceBundle bundle12 = ResourceBundle.getBundle("prop.welcome",Locale.SIMPLIFIED_CHINESE);
String strCN2 = bundle12.getString("wel");
System.out.println(strCN2);
ResourceBundle bundle13 = ResourceBundle.getBundle("prop.welcome",new Locale("zh","CN"));
String strCN3 = bundle13.getString("wel");
System.out.println(strCN3);
//找不到 走 默认的这个 welcome.properties
ResourceBundle bundle14 = ResourceBundle.getBundle("prop.welcome",Locale.CHINESE);
String strCN4 = bundle14.getString("wel");
System.out.println(strCN4);
System.out.println("=============================================");
//方法三:按照 Locale 类里只写language
ResourceBundle bundle2 = ResourceBundle.getBundle("prop.welcome",Locale.ENGLISH );
String strEnglish = bundle2.getString("wel");
System.out.println(strEnglish);
System.out.println("=============================================");
//找不到 走 默认的这个 welcome.properties
ResourceBundle bundle3 = ResourceBundle.getBundle("prop.welcome",new Locale("jp","XXX") );
String str3 = bundle3.getString("wel");
System.out.println(str3);
}
}
结果:
方法一
ResourceBundle.getBundle("prop.welcome",new Locale("ny","NYNYNY"))
基名_语言_国家.properties
说明:properties 文件的语言和国家自己定义,没有限制。
方法二
welcome_zh_CN.properties
ResourceBundle.getBundle("prop.welcome",Locale.CHINA);
ResourceBundle.getBundle("prop.welcome",Locale.SIMPLIFIED_CHINESE);
说明:properties 文件的语言和国家 参照这里 Locale.class
方法二的最后一条走的默认,是因为不相符
方法三
welcome_en.properties
ResourceBundle.getBundle("prop.welcome",Locale.ENGLISH )
说明:properties 文件的只定义语言,参照这里 Locale.class
看一下 ResourceBundle.class