出处:http://www.mkyong.com/struts2/struts-2-i18n-or-localization-example/
A Struts 2 internationalizing(i18n), localization(i10n) or multi-lingua example to show the use of resource bundle to display the message from different languages. In this example, you will create a simple login screen, display the message from resource bundle via the Struts 2 UI components, and change the locale base on the selected language option.
1. Project Structure
The project structure for this example
2. Properties file
Make sure the properties file are named as country specified code.
In some “non-Europe” or “non-English” like characters, you should always encode the content with
native2ascii tool.
global.properties
#Global messages
global.username = Username
global.password = Password
global.submit = Submit
global_zh_CN.properties
#Global messages
global.username = \u7528\u6237\u540d
global.password = \u5bc6\u7801
global.submit=\u63d0\u4ea4
global_fr.properties
#Global messages
global.username = Nom d'utilisateur
global.password = Mot de passe
global.submit = Soumettre
global_de.properties
#Global messages
global.username = Benutzername
global.password = Kennwort
global.submit = Einreichen
3. Action
Two action classes, the LocaleAction is basically do nothing, and the LoginAction will do a simple validation and display the error message from resource bundle via getText().
LocaleAction.java
package com.mkyong.common.action;
import com.opensymphony.xwork2.ActionSupport;
public class LocaleAction extends ActionSupport{
//business logic
public String execute() {
return "SUCCESS";
}
}
LoginAction.java
package com.mkyong.user.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
private String username;
private String password;
//...getter and setter methods
//business logic
public String execute() {
return "SUCCESS";
}
//simple validation
public void validate(){
if("".equals(getUsername())){
addFieldError("username", getText("username.required"));
}
if("".equals(getPassword())){
addFieldError("password", getText("password.required"));
}
}
}
4. View page
A login page with a textbox, password and submit UI components.
To support Struts 2 localization, you HAVE TO declared the
<%@ page contentType=”text/html;charset=UTF-8″ %> in your view page, else you will have problem to display the “UTF-8 data” correctly, especially the Chinese characters. Read this article about
Struts 2 Chinese localization issue.
login.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body>
<h1>Struts 2 localization example</h1>
<s:form action="validateUser" namespace="/user">
<s:textfield key="global.username" name="username" />
<s:password key="global.password" name="password"/>
<s:submit key="global.submit" name="submit" />
</s:form>
<s:url id="localeEN" namespace="/" action="locale" >
<s:param name="request_locale" >en</s:param>
</s:url>
<s:url id="localezhCN" namespace="/" action="locale" >
<s:param name="request_locale" >zh_CN</s:param>
</s:url>
<s:url id="localeDE" namespace="/" action="locale" >
<s:param name="request_locale" >de</s:param>
</s:url>
<s:url id="localeFR" namespace="/" action="locale" >
<s:param name="request_locale" >fr</s:param>
</s:url>
<s:a href="%{localeEN}" >English</s:a>
<s:a href="%{localezhCN}" >Chinese</s:a>
<s:a href="%{localeDE}" >German</s:a>
<s:a href="%{localeFR}" >France</s:a>
</body>
</html>
To change the default locale, you just need to declared a “request_locale” parameter, set your prefer language code and pass to an Action class. In Struts 2 thecom.opensymphony.xwork2.interceptor.I18nInterceptor interceptor, which declared in the struts-default.xml, will hijack your Action class and handle the locale stuff accordingly.
5. Display the resource bundle message?
In Struts 2 , there are many ways to display the resource bundle message base on the selected language or locale. For examples,
<s:textfield key="global.username" name="username" />
<s:text name="global.username" />
<s:property value="getText('global.username')" />
<s:text name="global.password" />
In Struts 1, there are one standard bean:message to display the resource bundle message, which is more prefer. But in Struts 2, there are so many equivalent ways to display the resource bundle message (even internal work is different), it’s quite confuse at the first glance. Basically, no matter what you choose, Struts 2 also will display the resource bundle message correctly.
6. struts.xml
Struts 2 configuration file, link it all together.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="global" />
<constant name="struts.devMode" value="true" />
<package name="user" namespace="/user" extends="struts-default">
<action name="login">
<result>pages/login.jsp</result>
</action>
<action name="validateUser" class="com.mkyong.user.action.LoginAction">
<result name="SUCCESS">pages/welcome.jsp</result>
<result name="input">pages/login.jsp</result>
</action>
</package>
<package name="default" namespace="/" extends="struts-default">
<action name="locale" class="com.mkyong.common.action.LocaleAction">
<result name="SUCCESS">user/pages/login.jsp</result>
</action>
</package>
</struts>
7. Demo
http://localhost:8080/Struts2Example/user/login.action
http://localhost:8080/Struts2Example/locale.action?request_locale=en
http://localhost:8080/Struts2Example/locale.action?request_locale=zh_CN
http://localhost:8080/Struts2Example/locale.action?request_locale=de
http://localhost:8080/Struts2Example/locale.action?request_locale=fr