zoukankan      html  css  js  c++  java
  • Struts2之i18N国际化

      对于i18n其实没有太多内容,一般的公司用不到这些内容,除非是跨国公司,但即便是跨国公司也不一定会使用i18n来进行国际化处理,所以本篇内容仅供大家了解,不做深入的探讨,希望通过本篇内容,可以帮助大家了解i18n的使用。好了废话不多说,下面进入今天的正题。

      首先说一下i18n在Struts中分为三个等级:Action、package、Application三种,分别对应着其作用的范围,我想你一定已经知道什么意思了。先通过一个java project简单认识一下i18n的使用:i18n文件上的后缀名默认都为:.properties,简单介绍一下i18n文件的命名规则:文件名+"_"+地区英文缩写(zh:大中华区;en:英语)+"_"+国家英文代码(CN:中国;US:美国)+".properties";需要指明的是多个properties文件的文件名称必须一致,通过地区英文缩写和国家英文代码来区分不同的语境。这里我们通过最基本的java程序为你简单呈现一下i18n的使用。

      既然是Struts中的i18n使用,首先我们先来看一下我们的国际化配置文件:app_en_US.properties与app_zh_CN.properties的信息:

    美国

    welcome.msg = welcome US

    中国

    welcome.msg = welcome CN
    welcome.zh =u4E2Du534E

      这里说明一点,这里的welcome.zh =u4E2Du534E是中文“你好”的字符编码,为什么要使用字符编码,为了防止出现中文乱码,那么问题来了,我们总不能先用这样一个一个的转吧,这里我们可以通过myeclipse的properties文件打开方式,将我们的中文信息输入,myeclipse会自动为我们将中文转化为字符编码格式。

      说了这么多,下面我们来看一下具体的程序代码怎么编写吧:

    import java.util.Locale;
    import java.util.ResourceBundle;
    
    public class Iu8n {
        public static void main(String[] args) {
            ResourceBundle resourceBundleUS = ResourceBundle.getBundle("app", Locale.US);//加载英文配置文件
            System.out.println(resourceBundleUS.getString("welcome.msg"));
            ResourceBundle resourceBundleCN = ResourceBundle.getBundle("app", Locale.CHINA);//加载中文配置文件
            System.out.println(resourceBundleCN.getString("welcome.msg"));
            System.out.println(resourceBundleCN.getString("welcome.zh"));
        }
    }

      你是不是已经明白什么意思了呢,对就是通过加载不同的配置文件来实现不同语言环境下文本信息动态变化的。当然在Struts中我们不需要做这些操作,我们只需要配置好properties文件,通过el表达式,在不同语言环境下进行切换即可。说来这么多,下面我们开始具体的内容介绍,首先介绍一下关于i18n文件的三种不同级别的使用:

      三种级别的使用,我们使用的配置文件都一样,所以先给大家看一下我们的properties文件的内容:

    User_en_US.properties:

    welcome.msg =welcome:

    User_zh_CN.properties:

    welcome.msg =u6B22u8FCEu4F60uFF1A

      关于三种级别properties文件的区别在于文件名,Action级别的配置文件,文件名必须和Action的名称一致,和action类放置于同一个包下;package级别的配置文件,文件名必须为package,既然是包级别的配置文件,所以文件需要放置于指定的包下;最后就是我们的application级别的配置文件了,这个文件应置于src目录下,文件名没有特殊要求。

      我们的struts.xml文件:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    
        <constant name="struts.enable.DynamicMethodInvocation" value="true" />
        <constant name="struts.devMode" value="true" />
        <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
        
        <package name="default" namespace="/" extends="struts-default">
        
            <default-action-ref name="user"></default-action-ref>
            
            <action name="user" class="com.edu.test.User">
                <result name="success">/success.jsp</result>
            </action>
        </package>
    
    </struts>

      我们的Action文件:

    public class User extends ActionSupport {
    
        private String name;
        private String password;
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        @Override
        public String execute() throws Exception {
            return SUCCESS;
        }
        
    }

      这里我们通过登录功能的实现来模拟国际化操作,首先我们先看一下action级别的配置文件的使用,上面已经提到,Action级别的配置文件只能在对应的Action下使用,这个怎么来判断呢,我们到struts.xml文件下,看我们的Action result返回到那些jsp页面,这些jsp页面才可以使用我们配置的国际化文件,我们看一下我们的success.jsp文件:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <%@ taglib uri="/struts-tags" prefix="s" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.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">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <s:property value="getText('welcome.msg')" /><s:property value="name"/></br>
        <s:debug></s:debug>
      </body>
    </html>

      下面我们看一下package级别的内容,package级别的配置文件,和action级别的使用基本一致,只是在配置文件的文件名和创建位置稍微有些不同外,其它内容完全一致,这里就不在详述,大家可以尝试一下。

      下面我们看一下appliction级别的配置文件的使用,这里我们的配置文件名我设置的为:app_en_US.properties与app_zh_CN.properties,我们这里需要配置一些我们的struts.xml文件,添加全局i18n文件配置:

    <constant name="struts.custom.i18n.resources" value="app"></constant>

      这样我们的所有页面便都可以使用配置信息了。

      下面我们再来看一个小问题,我们通过配置文件添加内容后缀,这样我们通过配置文件便可以将我们的其他信息与我们的国际化文件匹配起来:

    welcome.msg =welcome:{0}
    welcome.msg =u6B22u8FCEu4F60uFF1A{0}

      这里的{0}就是指我们需要传递的参数,这样的话我们的sucess.jsp文件就可以这样使用了:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <%@ taglib uri="/struts-tags" prefix="s" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.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">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <s:text name="welcome.msg" >
            <s:param value="name"></s:param>
        </s:text>
        <s:debug></s:debug>
      </body>
    </html>

      最后在为大家介绍一下,如何动态切换网页语言环境,我们一定注意到过,一些网站设置有不同的语言环境,当我们点击不同的语言时,网页的语言会动态进行切换,接下来我们就来看一下这是如何实现的:

    app_en_US.properties

    input.name=name:
    input.password=password:

    app_zh_CN.properties

    input.name=u59D3u540DuFF1A
    input.password=u5BC6u7801uFF1A

      我们的struts.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    
    <struts>
    
        <constant name="struts.enable.DynamicMethodInvocation" value="true" />
        
        <constant name="struts.devMode" value="true" />
        
        <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
        
        <constant name="struts.custom.i18n.resources" value="app"></constant>
        
        <package name="default" namespace="/" extends="struts-default">
        
            <default-action-ref name="Lang"></default-action-ref>
            
            <action name="Lang" class="com.edu.test.Lang">
                <result name="success">/index.jsp</result>
                <result name="error">/success.jsp</result>
            </action>
        </package>
    
    </struts>

      我们的index.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <%@ taglib uri="/struts-tags" prefix="s" %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>国际化</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">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <form action="Lang" method="post">
            <s:property value="getText('input.name')" /><input type="text" name="name" placeholder="请输入姓名"><br/>
            <s:property value="getText('input.password')" /><input type="text" name="password" placeholder="请输入密码"><br/>
            <input type="submit" value="登录"><br/>
            <a href="Lang?request_locale=en_US">US</a> | <a href="Lang?request_locale=zh_CN">CN</a>
        </form>
      </body>
    </html>

      好了关于struts中i18n的知识就为大家介绍到这里,内容很简单。

  • 相关阅读:
    flume杀掉重启
    Tomcat访问日志浅析 (转)
    httpclient Accept-Encoding 乱码
    解决Maven报Plugin execution not covered by lifecycle configuration
    mahout基于Hadoop的CF代码分析(转)
    hadoop Mahout中相似度计算方法介绍(转)
    nginx配置文件结构,语法,配置命令解释
    nginx 中文和英文资料
    使用异步 I/O 大大提高应用程序的性能
    nginx AIO机制与sendfile机制
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/6628719.html
Copyright © 2011-2022 走看看