Home
JavaEE
Java GUI
Framework
DHtml
Android
插画
教程下载

ATOM 2.0   RSS 2.0
Add to iGoogle Add to Live.com
  • DeZender对用Zend Encoder加密过的PHP文件反编译
  • 写一个用于微博同步的Google Desktop Gadget
  • 在同学的QZone中看到的BB用药大全
  • 今天终于收到金山快盘的邀请码
  • 利用最短ie判断在不同浏览器上播放背景音乐
  • 对插画方面的一点感受与回忆
  • 网上看到的两个死里逃生的图片,记录一下
  • 今天开通了糖果社区,附几个激活码
  • 终于可以实现我的多个微博实时同步了
  • 备案和现场拍照就是对站长们的瞎折腾
  • 今天开通了糖果社区,附几个激活码
  • 终于可以实现我的多个微博实时同步了
  • 备案和现场拍照就是对站长们的瞎折腾
  • 昨天借用飞姐的佳作做的一个浪漫爱情网页
  • 有几个腾讯微博的邀请码(实时更新)
  • BB的网站移到了GAE-JAVA上了
  • 一个基于GAE Python Web.py和JQuery的留言本程序
  • 类许愿墙的一个告白网站和一个电子书网站
  • Android中用Bitmap获取图片中某个区域的图象
  • 对暴力破解带密码的QQ相册的可行性分析
  • 对淘宝web旺旺版一些通讯接口的研究

使用ResourceBundle的i18n国际化

posted by David Chen at July 20, 2007, 3:45 AM    

i18 ResourceBundle MessageFormat native2ascii

java.util.ResourceBundle
//支持i18国际化的基础类
Resource bundles contain locale-specific objects. When your program needs a
locale-specific resource, a String for example, your program can load it from the
resource bundle that is appropriate for the current user's locale. In this way, you
can write program code that is largely independent of the user's locale isolating
most, if not all, of the locale-specific information in resource bundles.

比如有一个lang_zh_CN.properties文件,为了国际化,将文件编码设成utf-8,这时候,如果用下面的类
public class I18Resources {
private ResourceBundle propers = null;
public I18Resources() {
propers = ResourceBundle.getBundle("res".concat(File.separator).concat("lang"));
// 在res目录中找到 lang_zh_CN.properties的文件,虽然文件是utf-8编码,但是得到的
将还会是乱码,因为默认是用iso来处理的,可以,用java.util.Properties来处理也是一样的乱码,
因为都是用iso的...网上有方法说 System.setProperty("file.encoding", "utf-8");
这也是不行的,这似乎只对System.out有影响,但不会影响jvm的
}

public String getI18(String name, String[] params) {
String value = propers.getString(name);
if(value == null)
return "";
return MessageFormat.format(value, params);
//这里是将参数替换 properties文件中某个key值可能存在的变量 {0}, {1}, {2} ...
}

public String getI18(String name) {
return getI18(name, new String[] {});
}
}

在上面的lang_zh_CN.properties为utf-8编码,和上面的类存的时候,使用得到的仍是乱码,
还差一步,就是要用 native2ascii 命令将 lang_zh_CN.properties转成 \uxxxx这种格式的
码.在ant中或cmd中使用都可以.

<target name="native2ascii">
<delete file="${targetclassdir}/res/lang_zh_CN.properties" />
<native2ascii encoding="utf-8" src="${sourcedir}" dest="${targetclassdir}" includes="res/lang_zh_CN.properties" />
</target>
Labels:
Trackback:   http://cwq.iou1314.com/_a159
微博最近更新
[2010-09-04 04:43]    又是要加班的双休日,昨晚和领导去县城做资料做到11:30,确定这两天都要加班,刚准备回家,跟着领导接 电话说有村民想自杀不成,已带到卫生院,又跟领导回乡镇处理劝说一下。搞完到了1:30。回来凉都冲就睡 觉了。
[2010-09-03 09:28]    多个微博同步又增加了JavaEye。
本站内的任何文章,只代表个人意见或学习所用,如有版权声明,请尊重作者的劳动成果,在转载时请保留原始链接并注明出处。
Power by David's PhpCms, © 2007 - 2010 CWQ.IOU1314.COM All Rights Reserved.