了解Locale

1、了解缺省Locale是由操作系统决定的,Locale是由语言和国家代码组成

2、国际化资源文件是由baseName+locale组成,如:MessageBundle_en_US.properties
baseName是任意合法的文件名

3、native2ascii命令的位置和用法
* 位置:JAVA_HOME/bin
* 使用native2ascii.exe o.properties MessagesBundle_zh_CN.properties


NoI18nSample.java

package com.bjsxt.i18n;

public class NoI18nSample {

public static void main(String[] args) {

// System.out.println("你好");
// System.out.println("再见");

System.out.println("hello");
System.out.println("good bye");

}
}


I18nSample.java


package com.bjsxt.i18n;

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

public class I18nSample {

public static void main(String[] args) {

Locale defaultLocale = Locale.getDefault();
System.out.println("default country=" + defaultLocale.getCountry());
System.out.println("default language=" + defaultLocale.getLanguage());

//Locale currentLocale = new Locale("en", "US");
//Locale currentLocale = new Locale("zh", "CN");

Locale currentLocale = new Locale("ja", "JP");

ResourceBundle rb = ResourceBundle.getBundle("res.MessagesBundle", currentLocale);
//System.out.println(rb.getString("k1"));
//System.out.println(rb.getString("k2"));

MessageFormat mf = new MessageFormat(rb.getString("k1"));
System.out.println(mf.format(new Object[]{"Tom"}));
//System.out.println(mf.format(new Object[]{"张三"}));
}
}


o.properties

[quote]k1=你好,{0}
k2=再见[/quote]

MessagesBundle.properties

[quote]k1=hello,{0}
k2=good bye[/quote]

MessagesBundle_en_US.properties

[quote]k1=hello,{0}
k2=good bye[/quote]

MessagesBundle_zh_CN.properties


[quote]k1=\u4f60\u597d,{0}
k2=\u518d\u89c1[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值