i18n java_java i18n kit

Here is my some recommendations for java i18n .

1.  The correct way to handle text-based resource files for localization

Use java.util.ResourceBoundle to read resource from file.

e.g.

Local local = Local.CHINA;

ResourceBundle rb = ResourceBundle.getBundle("test", local);

String title = rb.getString("helloworld.title");

System.out.println(title);

//The program will read file: test_zh.properties

# This locale is zh_CN

# helloworld.title=中文1234

and the file should use native2ascii program to convert (native2ascii.exe is in JDK)

# This locale is zh_CN

helloworld.title=\u4f60\u597d1234

if you don't use native2ascii  to covert,you must covert it in the java program,like this:

ResourceBundle rb = ResourceBundle.getBundle("test", Locale.CHINA);

String title = rb.getString("helloworld.title");

System.out.println(new String(title.getBytes("8859_1")));  //covert to os/jvm default charset

2.       Locale driven date and time display

Use java.text.DateFormat to format date string

e.g.

DateFormat df = DateFormat.getDateInstance(DateFormat.FULL,Locale.CHINA);

String date = df.format(new Date());

System.out.println(date);

DateFormat df2 = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.DEFAULT,Locale.CHINA);

String datetime = df2.format(new Date());

System.out.println(datetime);

3.       JSP localization method.

1) native method:

use "Local local = request.getLocale();" to get the page accessor's local info

then use ResourceBoundle to read local resource

and page should use utf-8 charset

e.g.

Local local = request.getLocale();

ResourceBundle rb = ResourceBundle.getBundle("test", local);

String title = rb.getString("helloworld.title");

%>

test

notice:put the  test_zh.properties into directionary WEB_INF/classes

2)use jsp taglib to simplify the page

the Jakarta i18n taglib is a good choice. http://jakarta.apache.org/taglibs/doc/i18n-doc/index.html

e.g.

scope="request"

changeResponseLocale="false"/>

test

3)use j2ee web framework(Struts) to simplify

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值