转载自:http://blog.csdn.net/liushuijinger/article/details/16834039
国际化(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"/>
-
- <constant name="struts.custom.i18n.resources" value="message"></constant>
-
-
-
- <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包: