Struts2 - 本地化,国际化(i18n)

国际化(i18n)是规划和实施产品和服务的过程,以便能够轻松地适应特定的当地语言和文化,这一过程称为本地化.国际化过程称为翻译或本地化启用.

国际化缩写 i18n 因为单词以字母"i"开头,以"n"结尾,第一个i和最后一个n之间有18个字符.

Struts2提供本地化,即通过国际化(i18n)支持资源包,拦截器和标签库位于以下位置 :

  • UI标签

  • 消息和错误.

  • 在行动类中.

资源包

Struts2使用资源包为Web应用程序的用户提供多种语言和区域设置选项.您不必担心用不同语言编写页面.您所要做的就是为您想要的每种语言创建资源包.资源包将包含用户语言的标题,消息和其他文本.资源包是包含应用程序默认语言的键/值对的文件.

资源文件的最简单命名格式是 :

bundlename_language_country.properties

此处, bundlename 可以是ActionClass,Interface, SuperClass,Model,Package,全局资源属性.下一部分 language_country 表示国家/地区区域设置,例如,西班牙语(西班牙)区域设置由es_ES表示,英语(美国)区域设置由en_US等表示,您可以跳过可选的国家/地区部分.

当您通过键引用消息元素时,Struts框架按以下顺序搜索相应的消息包 :

  • ActionClass.properties

  • Interface.properties

  • SuperClass.properties

  • model.properties

  • package.properties

  • struts.properties

  • global.properties

要以多种语言开发应用程序,您应该维护与这些语言/区域设置相对应的多个属性文件,并根据键/值对定义所有内容.

例如,如果您要开发美国英语(默认),西班牙语和法语的申请,那么您会我要创建三个属性文件.这里我将仅使用 global.properties 文件,您还可以使用不同的属性文件来隔离不同类型的消息.

  • global.properties : 默认情况下,将应用英语(美国)

  • global_fr.properties : 这将用于Franch语言环境.

  • global_es.properties : 这将用于西班牙语区域设置.

访问消息

有几种方法可以访问消息资源,包括getText,文本标记,UI标记的关键属性和i18n标记.让我们简单地看一下 :

要显示 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"/>

本地化示例

让我们定位从前一章创建 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>

这里我们需要创建以下两个动作. (a)第一个动作a来处理Locale并用不同的语言显示相同的index.jsp文件(b)另一个动作是负责提交表单本身.这两个操作都将返回SUCCESS,但我们将根据返回值采取不同的操作,因为我们的目的不同于两个操作

处理区域设置的操作

package com.it1352.struts2; 
import com.opensymphony.xwork2.ActionSupport;

public class Locale extends ActionSupport {
   public String execute() {
       return SUCCESS;
   }
}

提交表格的行动

package com.it1352.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'&acirc;ge
global.submit = Soumettre des
global.heading = S&eacute; lectionnez Local
global.success = Authentifi	&eacute;  avec succ&egrave;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.IT屋.struts2.Employee"
         method = "execute">
         <result name = "input">/index.jsp</result>
         <result name = "success">/success.jsp</result>
      </action>
      
      <action name = "locale" 
         class = "com.IT屋.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>

现在,右键单击项目名称,然后单击导出> WAR文件以创建War文件.然后在Tomcat的webapps目录中部署此WAR.最后,启动Tomcat服务器并尝试访问URL http://localhost:8080/HelloWorldStruts2/index.jsp .这将产生以下屏幕 :

English Output

现在选择任何一种语言,让我们说我们选择西班牙语,它会显示以下结果 :

Spanish Output

您也可以尝试使用法语.最后,当我们处于西班牙语语言环境时,让我们尝试点击提交按钮,它会显示以下屏幕 :

西班牙语成功

恭喜,现在你有一个多语言的网页,你可以在全球推出你的网站.