java框架篇---Struts2 本地化/国际化(i18n)(转)

源地址:https://www.cnblogs.com/oumyye/p/4368453.html

国际化(i18n)是规划和实施的产品和服务,使他们能很容易地适应特定的本地语言和文化的过程中,这个过程被称为本地化。国际化的过程有时也被称为翻译或本地化启用。国际化是缩写i18n,因为我和两端用n字打头,并有18个字符之间的第i个和最后n。

访问方式:

有几种方法可以访问的信息资源,包括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"/>

下面实现国际化处理

结构目录:

配置文件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" />
   <!-- 扫描资源目录下以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地址。。

猜你喜欢

转载自www.cnblogs.com/pingxin/p/p00010.html