國際化(i18n)是規劃和實施的產品和服務,使他們能很容易地適應特定的本地語言和文化的過程當中,這個過程被稱爲本地化。國際化的過程有時也被稱爲翻譯或本地化啓用。國際化是縮寫i18n,由於我和兩端用n字打頭,並有18個字符之間的第i個和最後n。html
有幾種方法能夠訪問的信息資源,包括gettext的,文本標籤,UI標籤的關鍵屬性,國際化標籤。讓咱們來看看他們簡單:java
要顯示i18n的文本,使用的調用屬性標記gettext,或其餘任何標記,例如UI標籤以下:apache
<s:property value="getText('some.key')" />
文本標記檢索從默認的資源包,即一個消息 struts.propertiesyii
<s:text name="some.key" />
i18n標籤推值棧上的任意資源束。 i18n標籤範圍內的其餘標籤能夠顯示該資源包的消息:jsp
<s:i18n name="some.package.bundle"> <s:text name="some.key" /> </s:i18n>
大多數UI標籤的鍵屬性,能夠用來檢索的消息,從一個資源包:post
<s:textfield key="some.key" name="textfieldName"/>
下面實現國際化處理this
結構目錄:url
配置文件Struts.xmlspa
<?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.devMode" value="true" /> <!-- 掃描資源目錄下以global開頭的文件 --> <constant name="struts.custom.i18n.resources" value="global" /> <package name="helloworld" extends="struts-default" namespace="/"> <action name="empinfo" class="com.oumyye.action.Employee" method="execute"> <result name="input">/index.jsp</result> <result name="success">/success.jsp</result> </action> <action name="locale" class="com.oumyye.action.Locale" method="execute"> <result name="success">/index.jsp</result> </action> </package> </struts>
處理類 Employee .java翻譯
package com.oumyye.action; import com.opensymphony.xwork2.ActionSupport; public class Employee extends ActionSupport{ private String name; private int age; public String execute() { return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
處理類Locale.java
package com.oumyye.action; import com.opensymphony.xwork2.ActionSupport; public class Locale extends ActionSupport{ public String execute() { return SUCCESS; } }
視圖index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Employee Form with Multilingual Support</title> </head> <body> <h1><s:text name="global.heading"/></h1> <s:url id="indexUS" namespace="/" action="locale" > <s:param name="request_locale" >us</s:param> </s:url> <s:url id="indexZH" namespace="/" action="locale" > <s:param name="request_locale" >zh</s:param> </s:url> <s:url id="indexFR" namespace="/" action="locale" > <s:param name="request_locale" >fr</s:param> </s:url> <s:a href="%{indexUS}" >English</s:a> <s:a href="%{indexZH}" >中文</s:a> <s:a href="%{indexFR}" >France</s:a> <s:form action="empinfo" method="post" namespace="/"> <s:textfield name="name" key="global.name" size="20" /> <s:textfield name="age" key="global.age" size="20" /> <s:submit name="submit" key="global.submit" /> </s:form> </body> </html>
跳轉成功頁Success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Success</title> </head> <body> <s:property value="getText('global.success')" /> </body> </html>
global_fr.properties
global.name = Nom d'utilisateur
global.age = l'âge
global.submit = Soumettre des
global.heading = Sé lectionnez Local
global.success =Authentifi\t\u00E9 avec succ\u00E8s
global_us.properties
global.name = Name
global.age = Age
global.submit = Submit
global.heading = Select Locale
global.success =Successfully authenticated
global_zh.properties
global.name = \u59D3\u540D
global.age = \u5E74\u9F84
global.submit = \u63D0\u4EA4
global.heading = \u9009\u62E9\u4E00\u79CD\u8BED\u8A00
global.success =\u6210\u529F
效果界面
注意訪問時的url地址。。
借鑑於:http://www.yiibai.com/struts_2/struts_localization.html