java 多语言ResourceBundle类

 

package hb.resource;

import java.util.Locale;
import java.util.ResourceBundle;

public class Test {

	public static void main(String[] args) {
		
		//获取默认的地区语言(根据操作系统决定)
		//hb.resource.property是文件夹的名称,后面的message是资源文件的前缀message_zh.properties
		ResourceBundle rb =  ResourceBundle.getBundle("hb.resource.property.message");
		System.out.println(rb.getString("language"));
		
		//指定地区码,获取对应的资源文件
		ResourceBundle rb1 =  ResourceBundle.getBundle("hb.resource.property.message",Locale.US);		
		System.out.println(rb1.getString("language"));
		
		System.out.println(Locale.US);
		System.out.println(Locale.CHINA);
		System.out.println(Locale.TAIWAN);
		
		//统一地区码,但是不同的语言种类,如何区分的方式some_key=some_value
		//台湾的地区码是zh_TW,所以在语言资源文件后缀应该是message_zh_TW.properties
		ResourceBundle rb2 =  ResourceBundle.getBundle("hb.resource.property.message",Locale.TAIWAN);		
		System.out.println(rb2.getString("language"));
		
	}

}

 

 

package hb.resource;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;


public class ResourceDemo {

	public static void main(String[] args) {
		
		System.out.println(Locale.CHINA);
		System.out.println(Locale.US);
		
		ResourceBundle rb = ResourceBundle.getBundle("message");
		String hello = rb.getString("hello");
		System.out.println(hello);
		Object[] arrobj = new Object[]{"黄彪","我也好"};
		//动态参数是使用数组传递多个参数,参数的序号对应于数组的序号{0}---黄彪,{1}对应“我也好”
		String result = MessageFormat.format(hello, arrobj);
		//如果使用多个
		System.out.println(result);
		
	}

}

 

 

下面资源文件的目录是hb.resource.property

 

hello={0}hello{1}
language=english

 

hello={0}\u4f60\u597d{1}
language=\u4e2d\u6587

 

你可能感兴趣的:(ResourceBundle)