國際化(i18n)是規劃和實施的產品和服務,使他們能很容易地適應特定的本地語言和文化的過程中,這個過程被稱為本地化。國際化的過程有時也被稱為翻譯或本地化啟用。國際化是縮寫i18n,因為我和兩端用n字打頭,並有18個字元之間的第i個和最後n。
Struts2提供本地化,即,國際化(i18n)支持,通過資源包,攔截器和標籤庫在以下地方:
-
UI 標籤
-
消息和錯誤
-
動作類
資源包:
Struts2 使用資源包來提供Web應用程式的用戶多語言和區域選項。不必擔心在不同的語言編寫的網頁。所有必須做的是創造一個資源包為每個想要的語言。資源包將包含標題,消息和其他文本的語言用戶。資源包的檔,該檔包含鍵/值對您的應用程式的默認語言。
簡單的命名格式的資源檔是:
bundlename_language_country.properties
這裏,軟體包可以ActionClass,介面,超類,型號,封裝,全球資源屬性。接下來的部分 language_country ,En_US的等在這裏,可以跳過這是可選的全國部分區域表示es_ES和英語(美國),西班牙語(西班牙)表示語言環境的語言環境,例如代表國家。
當引用消息元素,其關鍵,按照下列順序進行相應的消息包的Struts框架搜索:
-
ActionClass.properties
-
Interface.properties
-
SuperClass.properties
-
model.properties
-
package.properties
-
struts.properties
-
global.properties
多語言開發應用程式,就必須保持相應的到那些語言/區域設置多個屬性檔定義的鍵/值對中的所有內容。例如,如果要開發應用程式(默認)為美國英語,西班牙語,和法語就必須創建三個屬性檔。在這裏,我將使用只global.properties檔,你可以利用不同的屬性檔來隔離不同類型的消息。
-
global.properties: 默認情況下,英語(美國)將被應用
-
global_fr.properties: 這將是法語環境中使用。
-
global_es.properties: 這將被用於西班牙語言環境。
訪問消息:
有幾種方法可以訪問的資訊資源,包括gettext的,文本標籤,UI標籤的關鍵屬性,國際化標籤。讓我們來看看他們簡單:
要顯示i18n的文本,使用的調用屬性標記gettext,或其他任何標記,例如UI標籤如下:
<s:property value="getText('some.key')" />
文本標記檢索從默認的資源包,即一個消息 struts.properties
<s:text name="some.key" />
i18n標籤推值棧上的任意資源束。 i18n標籤範圍內的其他標籤可以顯示該資源包的消息:
<s:i18n name="some.package.bundle"> <s:text name="some.key" /> </s:i18n>
大多數UI標籤的鍵屬性,可以用來檢索的消息,從一個資源包:
<s:textfield key="some.key" name="textfieldName"/>
Localization 例子:
創建的index.jsp從前一章到多種語言。相同的檔將被寫入,如下所示:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ 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="indexEN" namespace="/" action="locale" > <s:param name="request_locale" >en</s:param> </s:url> <s:url id="indexES" namespace="/" action="locale" > <s:param name="request_locale" >es</s:param> </s:url> <s:url id="indexFR" namespace="/" action="locale" > <s:param name="request_locale" >fr</s:param> </s:url> <s:a href="%{indexEN}" >English</s:a> <s:a href="%{indexES}" >Spanish</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檔,該檔將被調用的情況下定義的動作返回SUCCESS。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ 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>
在這裏,我們需要創建兩個動作。 (一)第一個動作一個Locale和照顧,用不同的語言顯示相同的index.jsp檔(二)另一項行動是為了照顧提交表單本身。的動作都將返回SUCCESS,但我們會採取不同的動作,返回值的基礎上,因為我們的目的是不同的兩個動作:
動作處理locale:
package com.zaixian.struts2; import com.opensymphony.xwork2.ActionSupport; public class Locale extends ActionSupport{ public String execute() { return SUCCESS; } }
提交表單處理動作:
package com.zaixian.struts2; 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; } }
現在。讓我們創建以下三個global.properties檔放在CLASSPATH中:
GLOBAL.PROPERTIES:
global.name = Name global.age = Age global.submit = Submit global.heading = Select Locale global.success = Successfully authenticated
GLOBAL_FR.PROPERTIES:
global.name = Nom d'utilisateur global.age = l'âge global.submit = Soumettre des global.heading = Sé lectionnez Local global.success = Authentifi é avec succès
GLOBAL_ES.PROPERTIES:
global.name = Nombre de usuario global.age = Edad global.submit = Presentar global.heading = seleccionar la configuracion regional global.success = Autenticado correctamente
我們將創建struts.xml中兩個動作如下:
<?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" /> <constant name="struts.custom.i18n.resources" value="global" /> <package name="helloworld" extends="struts-default" namespace="/"> <action name="empinfo" class="com.zaixian.struts2.Employee" method="execute"> <result name="input">/index.jsp</result> <result name="success">/success.jsp</result> </action> <action name="locale" class="com.zaixian.struts2.Locale" method="execute"> <result name="success">/index.jsp</result> </action> </package> </struts>
以下是web.xml檔中的內容:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Struts 2</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
現在,右鍵點擊專案名稱,並單擊 Export > WAR File創建一個WAR檔。然後部署此WAR在Tomcat的webapps目錄下。最後,啟動Tomcat伺服器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2/index.jsp。這會給出以下畫面:

現在選擇的任何一種語言,讓我們說,我們選擇西班牙語,這將顯示以下結果:

您可以嘗試用法語。最後,讓我們嘗試點擊“Submit ”按鈕,當我們在西班牙語言,它會顯示以下畫面:

恭喜你,現在有一個多語種的網頁,可以在全球範圍內啟動您的網站。