java----程序国际化

主程序

package com.zy;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Scanner;

public class Demo {
	public static void main(String[] args) {
		//创建一个本地语言环境
		Locale locale_CN = new Locale("zh","CN");
		Locale locale_US= new Locale("en","US");
		//根据操作系统默认选择语言环境
		//Locale locale_default= Locale.getDefault();
		
		//如果后面不加参数,找系统默认的语言的配置文件(info_zh_CN)
		ResourceBundle r = ResourceBundle.getBundle("com.property.info");
		
		//找英语中的配置文件(info_en_US)
		//ResourceBundle r = ResourceBundle.getBundle("com.property.info",locale_US);
		
		//通过配置文件字段(key)读取的value
		//System.out.println(r.getString("input.username"));
		
		
		//示例
		System.out.println(r.getString("System.name"));
		Scanner input = new Scanner(System.in);
		System.out.println(r.getString("input.username"));
		String username = input.nextLine();
		System.out.println(r.getString("input.password"));
		String password = input.next();
		if("admin".equals(username)&&"111".equals(password)){
			System.out.println(r.getString("login.success"));
			//动态文本格式化
			String s = r.getString("welcom");
			s = MessageFormat.format(s, username);
			System.out.println(s);
			
		}else{
			System.out.println(r.getString("login.error"));			
		}
	}

}

新建一个包(com.property),里面放入配置文件(语言数据)

在该包下新建一个文件info_zh_CN.properties

System.name=\u5458\u5DE5\u7BA1\u7406\u7CFB\u7EDF
input.username=\u8BF7\u8F93\u5165\u7528\u6237\u540D\uFF1A
input.password=\u8BF7\u8F93\u5165\u5BC6\u7801\uFF1A
login.success=\u767B\u5F55\u6210\u529F
login.error=\u767B\u5F55\u5931\u8D25
welcom=\u6B22\u8FCE\u4F60\uFF0C{0}

在该包下新建一个文件info_en_US.properties

System.name = EMP Manage System
input.username = Input UserName:
input.password = Input PassWord:
login.success = Login Success
login.error = Login Error
welcom=WelCom,{0}

  

猜你喜欢

转载自www.cnblogs.com/yanxiaoge/p/10688705.html
今日推荐