博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
国际化技术(MessageBundle)
阅读量:4118 次
发布时间:2019-05-25

本文共 5001 字,大约阅读时间需要 16 分钟。

国际化技术
文章出处:中国计算机技术联盟网   发布时间:2005-11-19
resourceBoundle
(1)resourceBoudle的使用
有两种类型的resourceBoudle, 一种从properties文件中读取数据 ,另一种从class文件中读取数据, 但是他们的使用方法都是一样的。如下所示:
//第一个参数为语种,
//第二个参数为国家
//第三个参数随便定,一般用于区别操作系统
Locale currentLocale = new Locale("cn","CN", "windows") ;
// 获得 ResourceBundle
ResourceBundle messages=ResourceBundle.getBundle("MessageBundle", currentLocale);
// 获得所需的值
String value = messages.getString(key);
(2)对于properties文件中读取数据的ResourceBundle, getBundle函数的第一个参数是资源文件的名称(暂时取名为名称),第二个参数为区域(暂时取名为区域)。 对于(1)
中的例子, 系统将以下面的顺序在class所在路径下查找文件(假设jre运行在english版的windows平台上)
MessageBundle_cn_CN_windows.properties
MessageBundle_cn_CN.properties
MessageBundle_cn.properties
MessageBundle_en_En.properties(默认locale,与jre所在的操作系统有关)
MessageBundle_en.properties(默认locale,与jre所在的操作系统有关)
MessageBundle.properties
注意: 如果ResourceBundle的代码如下所示,第一个参数为com.neusoft.MessageBudle,
则,系统将到class所在文件夹的中子路径com/neusoft中查找
ResourceBundle messages=ResourceBundle.getBundle("com.neusoft.MessageBudle", currentLocale)
(2)对于class文件中读取resourceBundle, 则被读取的资源文件(class文件)必须从ListResourceBundle中继承,并且实现getContents方法。如下所示
import java.util.ResourceBundle;
import java.util.ListResourceBundle;
public class MessageBundle_cn_CN_windows extends ListResourceBundle {
// key-value , contained the resource item
private Object[][] contents = {
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"},
{"key4", "value4"}
};
// must be inplement
public Object[][] getContents(){
return contents ;
}
}
对于(1)中的例子, 系统将以下面的顺序在class所在路径下查找文件(假设jre运行在english版的windows平台上)
MessageBundle_cn_CN_windows.class
MessageBundle_cn_CN.class
MessageBundle_cn.class
MessageBundle_en_En.class(默认locale,与jre所在的操作系统有关)
MessageBundle_en.class(默认locale,与jre所在的操作系统有关)
MessageBundle.class
那么,resourceBundle既然已经提供从properties中读取资源项,为什么要提供listResourceBundle,这是因为从listResourceBundle读取数据不需要i/o开销,具有更快的速度。
注意:如果在给定目录下同时有properties资源文件和listResourceBunle资源class
,则 listResourceBunle资源class具有更高的优先权。
Message format
最简单的例子
代码:
String message = "these is the {0} message format ";
String messageOut = MessageFormat.format(message, new String[]{ "first"});
System.out.println(messageOut);
运行结果:
these is the first message format
说明:
大括号内的数字表示第几个参数
使用日期的例子
代码:
String message = "At {1,time} on {1,date}, there was {2} on planet {0,number,integer} ";
Object[] arguments = { new Integer(7),
new Date(System.currentTimeMillis()),
"a disturbance in the Force"
};
String messageOut = MessageFormat.format(message,arguments );
System.out.println(messageOut);
运行结果:
At 13:30:51 on 2003-5-14, there was a disturbance in the Force on planet 7
说明:
大括号内的第一个参数表示位置 ,第二个参数是类型,第三个参数是风格(可以没有,没有表示default),如下表所示:
Format Type Format style Subformat Created
(none) null
number (none) NumberFormat.getInstance(getLocale())
integer NumberFormat.getIntegerInstance(getLocale())
currency NumberFormat.getCurrencyInstance(getLocale())
percent NumberFormat.getPercentInstance(getLocale())
SubformatPattern new DecimalFormat(subformatPattern, new DecimalFormatSymbols(getLocale()))
date (none) DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
short DateFormat.getDateInstance(DateFormat.SHORT, getLocale())
medium DateFormat.getDateInstance(DateFormat.DEFAULT, getLocale())
long DateFormat.getDateInstance(DateFormat.LONG, getLocale())
full DateFormat.getDateInstance(DateFormat.FULL, getLocale())
SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())
time (none) DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
short DateFormat.getTimeInstance(DateFormat.SHORT, getLocale())
medium DateFormat.getTimeInstance(DateFormat.DEFAULT, getLocale())
long DateFormat.getTimeInstance(DateFormat.LONG, getLocale())
full DateFormat.getTimeInstance(DateFormat.FULL, getLocale())
SubformatPattern new SimpleDateFormat(subformatPattern, getLocale())
choice SubformatPattern new ChoiceFormat(subformatPattern)
使用数字例子
代码:
MessageFormat mf = new MessageFormat("{0,number,#.##}, {0,number,#.#}");
Object[] objs = {new Double(3.1415)};
String result = mf.format( objs );
System.out.println(result);
运行结果:
3.14, 3.1
说明:
对于float 和double , 风格可以#.##形式表示,逗号后面有几个#, 就表示有几位小数,分号之前的无所谓,甚至可以不写
Character internalize
错误的例子:
char ch;
// ch is a letter
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
// ch is a digit
if (ch >= '0' && ch <= '9')
// ch is a whitespace
if ((ch == ' ') || (ch =='/n') || (ch == '/t'))
正确的例子
//import java.lang.Character;
char ch;
// ch is a letter
if (Character.isLetter(ch))
// ch is a digit
if (Character.isDigit(ch))
// ch is a whitespace
if (Character.isSpaceChar(ch))
原因(copy from sun document):
The preceding code is wrong because it works only with English and a few other languages.
String compare
错误的例子:
String a
String b
// compare a and b
if(a.compareTo(b) < 0)
正确的例子:
// import java.text. Collator
String a = "及";
String b = "中" ;
Collator collator = Collator.getInstance();
// compare a and b
if(collator.compare(a, b) < 0)
原因(copy from sun document):
The preceding code is wrong because it works only with English and a few other languages.

转载地址:http://ggdpi.baihongyu.com/

你可能感兴趣的文章
实战:用 C 语言实现操作系统
查看>>
Java 与底层操作系统的交互细节
查看>>
看完这篇 Linux 的基本操作你就会了!
查看>>
为什么大部分码农做不了架构师?
查看>>
Redis ,真的不完美
查看>>
Java 面试如何坐等 offer?
查看>>
5 分钟入门 Python 协程
查看>>
学会 C 语言真可以开发很多东西吗?
查看>>
现在学 NLP 还来得及吗?
查看>>
代码案例讲解程序员的第一性原理
查看>>
iOS 工程开发中的 Code Review
查看>>
大型微服务架构稳定性建设策略
查看>>
现在哪个 UI 会写代码?
查看>>
前端 「人傻钱多 」 速来!
查看>>
实现 AI 换脸的第一步:人脸搜索
查看>>
Python 数据分析师必备技能!
查看>>
996?你没见过的,是我每天待到凌晨3点的西二旗...
查看>>
没有代码也能学算法?美妆博主竟然也在推荐!
查看>>
本文也许可以帮你解决 Redis 的疑难杂症
查看>>
Java 工程师核心基础修炼
查看>>