一、java对国际化的支持
java.util.ReasourceBudle
java.util.Local
1、如何来获得当前jdk已经支持的国家
import java.util.Locale;
public class Test1
{
public static void main(String[] args)
{
Locale[] locales = Locale.getAvailableLocales();//该方法是静态的,而且返回的是Local的数组
for(Locale locale : locales)
{
System.out.println(locale.getDisplayCountry() + " : " + locale.getCountry());
System.out.println("*********************************************************");
System.out.println(locale.getDisplayLanguage() + " : " + locale.getLanguage());
}
}
}
2、用java实现国际化
文件名字: basename_语言_国家
如
hellofile_en_US.properties
hellofile_zh_CN.properties
文件内容:
hello=helloworld
hello=你好-----》需要转化后写上去----》用java下的工具native2ascii得到 hello=\u4f60\u597d
涉及到类
java.util.ReasourceBudle
java.util.Local
import java.util.*;
public class Test2
{
public static void main(String[] args)
{
Locale locale1=Locale.getDefault();//这个语句可以得到本地的默认Local
Locale locale = Locale.US;//设置本地Local
ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);//通过静态方法得到ResourceBundle
String value = bundle.getString("hello");//通过key得到value
System.out.println(value);
}
}
3、国际化资源文件不写死的情况
文件名字一样: basename_语言_国家
如
hellofile_en_US.properties
hellofile_zh_CN.properties
文件内容:
hello = hello world : {0}
hello = 你好 : {0}
{0}是程序运行时动态传进去的。
注:以上必须从0开始,一个个连续的加上去。
涉及到新类import java.text.MessageFormat类
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);
String value = bundle.getString("hello");
String result = MessageFormat.format(value,new Object[]{"北京"});
System.out.println(result);
}
}
二、struts国际化的支持
1、对jsp页面的国际化
1)国际化前在src文件夹中,有资源文件message.properties
struts.xml文件中有配置:
<constant name="struts.custom.i18n.resources" value="message"></constant>
2)国际化准备工作,就是将原来src文件夹下的message.properties文件
改为需要国际化的那几个国家几个文件:
message_en_US.properties
message_zh_CN.properties
内容分别如下:
addUser = Add User Information
username.invalid = username "{0}" invalid
username.xml.invalid = username xml invalid
addUser = 增加用户信息
username.invalid = 用户名 "{0}" 填写不正确
username.xml.invalid = 验证框架提示用户名不正确
国际化后即可用浏览器预览: Internate选项/常规/语言/添加语言英语/上移
即可显示英文版了
2、对action类validate()方法的国际化
1)用到的文件:工作同上面jsp国际化的1)2)两步
2)在文件message_en_US.properties
message_zh_CN.properties
分别增加以下内容username.invalid=username error !
username.invalid=\u7528\u6237\u540d\u586b\u5199\u4eod
3)validate()方法修改如下
if (null == username || username.length() < 6 || username.length() > 10)
{
this.addActionError(this.getText("username.invalid"));
}
当客户使用英文操作浏览器时,验证错误时提示如下:username error !
当客户使用中文浏览器时。验证错误时提示如下信息:用户名不正确
因为“用户名不正确”正好对应ascii为上面的“\u7528\u6237\u540d\u586b\u5199\u4eod”
2、1同样validate方法中也可以加动态的参数,如下对上面的2、对类validate()方法的国际化修改如下:
2)在文件message_en_US.properties
message_zh_CN.properties
分别增加以下内容username.invalid = username "{0}" invalid
username.invalid = 用户名 "{0}" 填写不正确
3)validate()方法修改如下
if (null == username || username.length() < 6 || username.length() > 10)
{ //this.addActionError(this.getText("username.invalid",new String[]{username}));
//或将上面一句改成以下方式
List list = new ArrayList();
list.add(username);
this.addActionError(this.getText("username.invalid",list));
}
当客户使用英文操作浏览器时,输入"张三"验证错误时提示如下:username"张三"error !
当客户使用中文浏览器时。验证错误时提示如下信息:用户名"张三"不正确
3、验证框架(xml)的国际化
1)用到的文件:工作同上面jsp国际化的1)2)两步
2)在文件message_en_US.properties
message_zh_CN.properties
分别增加以下内容username.xml.invalid = username xml invalid
username.xml.invalid = 验证框架提示用户名不正确
3)在原来的registerAtionValidate.xml文件中增加
<message key="username.xml.invalid"></message>
例如:
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="username.xml.invalid"></message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>username should be between ${minLength} and ${maxLength}</message>
</field-validator>
</field>
4、以上国际化文件都是全局的,还有包级别和类级别的资源文件
1)包级别命名规则。package_en_US.properties
package_zh_CN.properties
此时和java的变量一样,若在包级别中发现内容,就不会再去全局中去找了。
2)类级别命名规则RegisterAction_en_US.properties
RegisterAction_zh_CN.properties
此时和java的变量一样,若在类级别中发现内容,就不会再去全局和包级别中去找了。
5、表单form国际化
1)要求:Struts表单不能设置为simple主题
2)它国际化是和相应的action去关联,会首先去找类级别的国际化。
6、也可以在jsp页面中直接用i18n来挨个字段指定
1)假如我在src目录下有资源 包
temp_en_US.properties
temp_zh_CN.properties
内容为: hello = world,{0}
hello = 你好,{0}
<body> <center> <s:i18n name="temp"> <s:text name="hello"> <s:param>langsin</s:param> </s:text> </s:i18n> </center> </body>