Struts-2-struts-localization
Struts2-ローカリゼーション、国際化(i18n)
国際化(i18n)は、製品とサービスを特定の現地の言語と文化に簡単に適合させることができるように、製品とサービスを計画および実装するプロセスです。ローカリゼーションと呼ばれるプロセスです。 国際化プロセスは、翻訳またはローカリゼーションの有効化と呼ばれます。
国際化は i18n と省略されます。これは、単語が文字「」で始まり、「」で終わるため、最初のiと最後のnの間に18文字があるためです。
Struts2は、ローカライズ、つまり、以下の場所でリソースバンドル、インターセプター、およびタグライブラリを介した国際化(i18n)サポートを提供します-
- UIタグ
- メッセージとエラー。
- アクションクラス内。
リソースバンドル
Struts2はリソースバンドルを使用して、Webアプリケーションのユーザーに複数の言語およびロケールオプションを提供します。 異なる言語でページを書くことを心配する必要はありません。 必要なことは、必要な言語ごとにリソースバンドルを作成することです。 リソースバンドルには、ユーザーの言語のタイトル、メッセージ、およびその他のテキストが含まれます。 リソースバンドルは、アプリケーションのデフォルト言語のキー/値のペアを含むファイルです。
リソースファイルの最も簡単な命名形式は-
bundlename_language_country.properties
ここで、 bundlename は、ActionClass、Interface、SuperClass、Model、Package、Globalリソースプロパティです。 次の部分 language_country は国のロケールを表します。たとえば、スペイン語(スペイン)ロケールはes_ESで表され、英語(米国)ロケールはen_USなどで表されます。 ここでは、オプションである国の部分をスキップできます。
あなたがそのキーによってメッセージ要素を参照するとき、Strutsフレームワークは次の順序で対応するメッセージバンドルを検索します-
- ActionClass.properties
- Interface.properties
- SuperClass.properties
- model.properties
- package.properties
- struts.properties
- global.properties
アプリケーションを複数の言語で開発するには、それらの言語/ロケールに対応する複数のプロパティファイルを維持し、キー/値のペアに関してすべてのコンテンツを定義する必要があります。
たとえば、米国英語(デフォルト)、スペイン語、フランス語のアプリケーションを開発する場合、3つのプロパティファイルを作成する必要があります。 ここでは、 global.properties ファイルのみを使用します。異なるプロパティファイルを使用して、異なるタイプのメッセージを分離することもできます。
- global.properties -デフォルトでは英語(米国)が適用されます
- global_fr.properties -これはFranchロケールに使用されます。
- global_es.properties -これはスペイン語ロケールで使用されます。
メッセージにアクセスする
getText、textタグ、UIタグのキー属性、i18nタグなど、メッセージリソースにアクセスする方法がいくつかあります。 それらを簡単に見てみましょう-
*i18n* テキストを表示するには、プロパティタグで *getText* の呼び出しを使用するか、次のようなUIタグなどの他のタグを使用します-
<s:property value = "getText('some.key')"/>
- textタグ*は、デフォルトのリソースバンドル(struts.properties)からメッセージを取得します
<s:text name = "some.key"/>
- i18nタグ*は、任意のリソースバンドルを値スタックにプッシュします。 i18nタグのスコープ内の他のタグは、そのリソースバンドルからのメッセージを表示できます。
<s:i18n name = "some.package.bundle">
<s:text name = "some.key"/>
</s:i18n>
ほとんどのUIタグの key 属性を使用して、リソースバンドルからメッセージを生成できます-
<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 を返す場合に呼び出される success.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>Success</title>
</head>
<body>
<s:property value = "getText('global.success')"/>
</body>
</html>
ここでは、次の2つのアクションを作成する必要があります。 (a)ロケールを処理し、同じindex.jspファイルを異なる言語で表示する最初のアクションa(b)別のアクションは、フォーム自体の送信を処理することです。 両方のアクションがSUCCESSを返しますが、目的は両方のアクションで異なるため、戻り値に基づいて異なるアクションを実行します
ロケールの世話をするアクション
package com.finddevguides.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class Locale extends ActionSupport {
public String execute() {
return SUCCESS;
}
}
フォームを送信するアクション
package com.finddevguides.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;
}
}
ここで、次の3つの 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
次の2つのアクションを使用して 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.finddevguides.struts2.Employee"
method = "execute">
<result name = "input">/index.jsp</result>
<result name = "success">/success.jsp</result>
</action>
<action name = "locale"
class = "com.finddevguides.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ファイルを作成します。 次に、このWARをTomcatのwebappsディレクトリにデプロイします。 最後に、Tomcatサーバーを起動して、URL *http://localhost:8080/HelloWorldStruts2/index.jsp にアクセスしてみます。 これにより、次の画面が生成されます-
今、言語のいずれかを選択し、私たちは*スペイン語*を選択したとしましょう、それは次の結果を表示します-
フランス語も試してみることができます。 最後に、スペイン語のロケールにいるときに[送信]ボタンをクリックしてみてください。次の画面が表示されます-
おめでとうございます、多言語のウェブページができたので、ウェブサイトをグローバルに立ち上げることができます。