国际化 多语言 ResourceBundle

 目的:实现国际化多语言开发

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值