国际化i18n

jsp的国际化

action的国际化

类型转换错误信息的国际化

输入校验的国际化

 

一个Locale由两部分组成,一个是country,一个是language。

//得到本机安装的地区

Locale[] locales = Locale.getAvailableLocales();

for(Locale locale : locales) {

// 中国 CN

// System.out.println(locale.getDisplayCountry() + " : " + locale.getCountry());

// 中文 zh

System.out.println(locale.getDisplayLanguage() + " : " + locale.getLanguage());

}

 

 

 

src目录下建立国际化文件,文件名格式如下

baseName_language_contry.properties

hellofile_zh_CN.properties

hello=/u4f60/u597d : {0}

 

hellofile_en_US.properties

hello=hello : {0}

 

{0}由程序动态传进来,用来被替换掉

 

使用国际化文件

import java.util.*;

public class Test2 {

public static void main(String[] args) {

Locale locale = Locale.getDefault(); //获得本地的local

Locale locale = Locale.US;

ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);

String value = bundle.getString("hello");

System.out.println(value);

}

}

 

 

替换国际化文件中的占位符

import java.util.*;

import java.text.*;

public class Test3 {

public static void main(String[] args) {

Locale locale = Locale.US;

ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);

//将国际化文件中的value值原封不动的输出

String value = bundle.getString("hello");

//用Object数组中的值来替换value中的占位符,{0}代表下标

String result = MessageFormat.format(value,new Object[]{"北京"});

System.out.println(result);

}

}

 

 

 

struts2中应用国际化

 

struts.xml

//message就是国际化文件名中的baseName

<constant name="struts.custom.i18n.resoutces" value="message"></constant>

 

 

register2.jsp

<body>

<!-- struts2国际化的显示方式,用text标签 -->

<!-- 若国际化文件中有此ket则显示对应的value

若无则直接显示name中的值 -->

<s:text name="addUser"></s:text>

 

Add User <!-- 原始页面显示方式 -->

</body>

 

message_en_US.properties

addUser=Add User Information

 

message_zh_CH.properties

addUser=XXX

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值