鍍金池/ 教程/ Java/ 本地化
注釋
主題/模板
驗證
有用的資源
Struts 2 注解類型
實例
攔截器
異常處理
表單標(biāo)簽
結(jié)果類型
值棧/OGNL
Spring 集成
數(shù)據(jù)標(biāo)簽
環(huán)境配置
配置
類型轉(zhuǎn)換
動作
Hibernate 集成
本地化
發(fā)送郵件
Ajax 標(biāo)簽
數(shù)據(jù)庫訪問
體系結(jié)構(gòu)
文件上傳
Tiles 集成
概述
基本的 MVC 架構(gòu)
控制標(biāo)簽

本地化

國際化(i18n)是規(guī)劃和實施產(chǎn)品和服務(wù),以便他們可以很容易地適應(yīng)特定的本地語言和文化的過程,這個過程被稱為本地化。國際化過程有時被稱為翻譯或本地化啟用。國際化縮寫為 i18n,因為這個單詞以 i 開始,以 n 結(jié)束,而且第一個 i 和最后的 n 之間有 18 個字符。

Struts2 提供本地化,即國際化(i18n)支持,通過使用下面的資源包,攔截器和標(biāo)簽庫:

  • UI標(biāo)簽

  • 消息和錯誤

  • 動作類內(nèi)

 資源包 

Struts 2 使用資源包來給 Web 應(yīng)用程序的用戶提供多種語言和本地化選項。你不必?fù)?dān)心用不同的語言編寫網(wǎng)頁。你所要做的是為每個你想要的語言創(chuàng)建一個資源包。這個資源包將包含你的用戶語言的標(biāo)題,消息和其他文本。資源包是包含使用你的應(yīng)用程序的默認(rèn)語言的鍵/值對的文件。

對資源文件最簡單的命名格式是:

bundlename_language_country.properties

這里,bundlename 可以是動作類,接口,超類,模型,包,全局資源屬性。接下來的部分語言**_**國家代表國家地區(qū),例如 Spanish (Spain) 地區(qū)用 es_ES 代表,English (United States) 地區(qū)用 en_US 代表等等。這里你可以跳過可選的國家部分。

當(dāng)你用它的鍵引用消息元素時,Struts 框架按照下列順序進(jìn)行檢索相應(yīng)的消息:

  • ActionClass.properties
  • Interface.properties
  • SuperClass.properties
  • model.properties
  • package.properties
  • struts.properties
  • global.properties

為了用多種語言開發(fā)你的應(yīng)用程序,你將不得不保留多個對應(yīng)于這些語言/地區(qū)的屬性文件相,并且根據(jù)鍵/值對定義所有的內(nèi)容。例如,如果你要為美國英語(默認(rèn)),西班牙語,和法語開發(fā)應(yīng)用程序,你就必須創(chuàng)建三個屬性文件。這里,我將只使用 global.properties 文件,你可以利用不同的屬性文件來隔離不同類型的消息。

  • global.properties:默認(rèn)情況下,英語(美國)將被應(yīng)用
  • global_fr.properties:這將用于法語環(huán)境。
  • global_es.properties:這將被用于西班牙語言環(huán)境。

訪問信息

有幾種方法可以訪問信息資源,包括 getText,文本標(biāo)簽,UI 標(biāo)簽的鍵屬性和國際化標(biāo)簽。讓我們來看看他們,如下簡單地說:

為了顯示 i18n 的文本,在屬性標(biāo)簽或其他任何標(biāo)簽中調(diào)用 getText,例如 UI 標(biāo)簽,如下所示:

<s:property value="getText('some.key')" />

文本標(biāo)簽檢索默認(rèn)的資源包的信息,即 struts.properties

<s:text name="some.key" />

i18n 標(biāo)簽把任意資源包放進(jìn)值棧中。i18n 標(biāo)簽范圍內(nèi)的其他標(biāo)簽可以顯示該資源包的信息:

<s:i18n name="some.package.bundle">
     <s:text name="some.key" />
</s:i18n>

大多數(shù) UI 標(biāo)簽的屬性,可以用來檢索一個資源包的消息:

<s:textfield key="some.key" name="textfieldName"/>

本地化例子

讓我們把前一章中用多種語言創(chuàng)建的 index.jsp 作為目標(biāo)。相同的文件將被編寫,如下所示:

<%@ 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>

我們將創(chuàng)建 success.jsp 文件,假如動作返回 SUCCESS,該文件將被調(diào)用。

<%@ 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>

這里,我們需要創(chuàng)建下面的兩個動作。(a)第一個動作監(jiān)督區(qū)域,顯示相同的用不同的語言編寫的 index.jsp 文件(b)另一個行動是為了監(jiān)督提交表單本身。動作都將返回 SUCCESS,但根據(jù)返回值,我們會采取不同的動作,因為對兩個動作來說我們的目的是不同的:

監(jiān)督區(qū)域的動作

package com.tutorialspoint.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class Locale extends ActionSupport{
   public String execute() 
   {
       return SUCCESS;
   }
}

提交表單的動作

package com.tutorialspoint.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;
   }
}

現(xiàn)在,讓我們創(chuàng)建下面三個 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'age
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

我們將創(chuàng)建帶有兩個動作的 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.tutorialspoint.struts2.Employee"
         method="execute">
         <result name="input">/index.jsp</result>
         <result name="success">/success.jsp</result>
      </action>     
      <action name="locale" 
         class="com.tutorialspoint.struts2.Locale"
         method="execute">
         <result name="success">/index.jsp</result>
      </action>
   </package>
</struts>

下面是 web.xml 文件中的內(nèi)容:

<?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>

現(xiàn)在,右鍵點擊項目名稱,并單擊 Export > WAR File 創(chuàng)建一個 WAR 文件。然后部署此 WAR 在 Tomcat 的 webapps 目錄下。最后,啟動 Tomcat 服務(wù)器和嘗試訪問 URL http://localhost:8080/HelloWorldStruts2/index.jsp. 這會給出以下畫面:

http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts14.gif" alt="" />

現(xiàn)在選擇任何一種語言,假如說我們選擇西班牙語,它將顯示下面的結(jié)果:

http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts15.gif" alt="" />

同樣,你可以嘗試用法語。最后,當(dāng)我們使用西班牙語言時,讓我們嘗試單擊 Submit 按鈕,它會顯示下面的畫面:

http://wiki.jikexueyuan.com/project/struts-2/images/helloworldstruts16.gif" alt="" />

恭喜你,現(xiàn)在你有一個多語言的網(wǎng)頁,你可以在全局范圍內(nèi)啟動你的網(wǎng)站。

下一篇:概述