菜鳥學SSH(二)——Struts2國際化手動切換版


國際化(internationalization,i18n)和本地化(localization,l10n)指讓產品(出版物,軟件,硬件等)能夠適應非本地環境,特別是其他的語言和文化。程序在不修改內部代碼的情況下,能根據不同語言及地區顯示相應的界面。


國際化原理:

國際化資源文件:用不同國家的語言描述相同的信息,並放在各自對應的.properties屬性文件中,程序根據運行時環境決定加載哪個文件。
國際化主要通過以下類完成: 
java.util.Locale:對應一個特定的國家/區域、語言環境。 
java.util.ResourceBundle:用於加載一個資源包。 
I18nInterceptor:struts2所提供的國際化攔截器,負責處理Locale相關信息。

國際化流程:程序得到當前運行環境的國家/區域、語言環境並存放於Locale,ResourceBundle根據Locale中信息自動搜索對應的國際化資源文件並加載。當某個Action被執行前,I18nInterceptor負責檢測Locale相關信息來尋找對應的國際化資源


先來看一下實例的效果圖:


默認中文:


點擊英文,頁面變成英文顯示:



接下來看具體的實現:


LoginAction

 

package com.lsj.action;

import java.util.Locale;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    
    
    
    private String flag;
    public String getFlag() {
        return flag;
    }
    public void setFlag(String flag) {
        this.flag = flag;
    }
    
    public String ha() throws Exception {
        this.flag=ServletActionContext.getRequest().getParameter("flag");
                Locale l = Locale.getDefault();   
                if(this.flag==null){   
                   l = new Locale("zh", "CN");   
                }else if (this.flag.equals("2")) {   
                   l = new Locale("zh", "CN");   
                } else if (this.flag.equals("1")) {   
                   l = new Locale("en", "US");   
                }    
              ActionContext.getContext().setLocale(l);   
              return "success";   
 
    }    

}

 


web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 
        


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.i18n.encoding" value="GBK"/>
    <!-- 配置i18n -->
    <constant name="struts.custom.i18n.resources" value="message"></constant>

       
   <!-- i18n控制 -->
   <package name="i18n" namespace="/"  extends="struts-default">
        <action name="i18n" class="com.lsj.action.LoginAction" method="ha">
            <result name="success">
               /i18nlogin.jsp
            </result>
            <result name="input">
               /i18nlogin.jsp
            </result>
        </action>
    </package>    

</struts>

 

 
        


login.jsp

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html>
  <head>
    <base href="<%=basePath%>">    
    <title>My JSP 'i18nlogin.jsp' starting page</title>    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">    
  </head>
  
  <body>
  <s:i18n name="local.message">
    <s:form action="/i18n.action" method="post">
      <s:textfield name="user.userName" label="%{getText('login.name')}"/>
      <s:password name="user.userPassword" label="%{getText('login.pass')}"/>
      <s:submit value="%{getText('login.submit')}"></s:submit>
       <a href="i18n.action?flag=1">英文</a>
       <a href="i18n.action?flag=2">中文</a>
    </s:form></s:i18n>
  </body>
</html>

 


需要引入的jar包:

 



OK到這里就實現了一個簡單的國際化的小實例,有的說弄國際化沒多大必要,因為咱們做的東西幾乎都只是用於國內。我想說的是:為什么就覺得我們做的東西只會給我們自己用?為什么就不會覺得隨着我們不斷的努力,我們做的東西會越來越好,然后我們的東西會逐漸打入國際市場,讓老外用我們做的東西呢?你們有信心嗎?




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM