菜鸟学SSH——Struts2国际化手动切换版

75 篇文章 2 订阅
19 篇文章 111 订阅

国际化(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到这里就实现了一个简单的国际化的小实例,有的说弄国际化没多大必要,因为咱们做的东西几乎都只是用于国内。我想说的是:为什么就觉得我们做的东西只会给我们自己用?为什么就不会觉得随着我们不断的努力,我们做的东西会越来越好,然后我们的东西会逐渐打入国际市场,让老外用我们做的东西呢?你们有信心吗?

 

 

  • 17
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 41
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 41
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘水镜

文章写得不错,我要让更多人看到

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值