I18N
public class I18N {
public static void main(String[] args){
Locale defaultL=Locale.getDefault();
System.out.println(defaultL.getCountry());
System.out.println(defaultL.getLanguage());
Locale my=new Locale("en","US");
ResourceBundle r=ResourceBundle.getBundle("Message",my);
System.out.println(r.getString("k1")+"---");
System.out.println(r.getString("k2")+"222");
MessageFormat mf=new MessageFormat(r.getString("k1"));
System.out.println(mf.format(new Object[]{"aaa"}));
}
}
public class I18nSample {
public static void main(String[] args) {
Locale defaultLocale = Locale.getDefault();
System.out.println("default country=" + defaultLocale.getCountry());
System.out.println("defalut language=" + defaultLocale.getLanguage());
//Locale currentLocale = new Locale("zh", "CN");
Locale currentLocale = new Locale("en", "US");
//Locale currentLocale = new Locale("aaa", "asdfdsf");
ResourceBundle rb = ResourceBundle.getBundle("MessagesBundle", currentLocale);
// System.out.println(rb.getString("k1"));
// System.out.println(rb.getString("k2"));
MessageFormat mf = new MessageFormat(rb.getString("k1"));
System.out.println(mf.format(new Object[]{"张三"}));
}
}
中文 乱码 转成 unicode
native2ascii Message_zh_CN1.properties Message_zh_CN.properties
1、struts的国际化配置
* 在struts-config.xml文件中加入<message-resources parameter="MessageResources" />
* 将MessageResources.propertis文件拷贝到src下
2、提供不同版本的国际化资源文件,中文需要采用native2ascii转换为unicode
3、在jsp中采用<bean:message/>标签读取国际化资源文件
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean"%>
<form action="login.do" method="post">
<bean:message key="id" />:<input name="id" type="text"><br>
<bean:message key="pw"/>:<input name="pw" type="text"><br>
<input type="submit" >
</form>
4、利用struts默认把Locale放到session中的特性,可以采用编程的方式手动切换语言设置
参见:ChangeLanguageAction.java
public class LangAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Locale l=null;
//Locale locale = Locale.getDefault();
String lang=request.getParameter("lang");
if("cn".equals(lang)){
l=new Locale("zh","CN");
}else if("en".equals(lang)){
l=new Locale("en","US");
}
//request.getSession().setAttribute(Globals.LOCALE_KEY, l);
this.setLocale(request, l);
return mapping.findForward("success");
}
}
-------------------------------------
jstl
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<fmt:setLocale value="${header['accept-language']}"/>
<fmt:setBundle basename="res.MessageResources"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title><fmt:message key="user.title"/></title>
</head>
<body>
<h1><fmt:message key="user.title"/></h1>
<hr>
<form action="login.do" method="post">
<fmt:message key="user.username"/>:<input type="text" name="username"><br>
<fmt:message key="user.password"/>:<input type="password" name="password"><br>
<input type="submit" value="<fmt:message key="user.login"/>">
</form>
</body>
</html>
消息文本的国际化,分为三个步骤:
* 创建消息
* 传递消息
* 显示消息
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();
ActionMessages messages = new ActionMessages();
try {
UserManager.getInstance().validate(username, password);
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.login.success", username);
//ActionMessage message = new ActionMessage("user.login.success", new Object[]{username});
messages.add("loginSuccess1", message);
messages.add("loginSuccess2", message);
//传递国际化消息文本
this.saveMessages(request, messages);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) {
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.not.found", username);
messages.add("error1", message);
//传递国际化消息文本
this.saveErrors(request, messages);
}catch(PasswordErrorException pee ) {
//创建国际化消息文本
ActionMessage message = new ActionMessage("user.password.error");
messages.add("error2", message);
//传递国际化消息文本
this.saveErrors(request, messages);
}
//return mapping.findForward("error");
return mapping.getInputForward();
}
}
如何创建消息?
* 理解ActionMessage和ActionMessages的区别
如何传递消息?
* 理解saveMessages和saveErrors的区别
如何显示消息?
* 通过<html:messages/>标签显示消息(可以显示提示消息和错误消息,既能读取MESSAGE_KEY也能读取ERROR_KEY)
<font color="red">
<html:messages id="msg" property="error1">
<bean:write name="msg"/>
</html:messages>
</font>
<font color="blue">
<html:messages id="msg" property="error2">
<bean:write name="msg"/>
</html:messages>
</font>
-----------------------
<html:messages id="msg" message="true" property="loginSuccess1">
<bean:write name="msg"/>
</html:messages>
* 通过<html:errors/>标签显示消息(只能显示错误消息),只能读取ERROR_KEY
<html:errors/>
-----------------------------