國際化(internationalization,i18n)和本地化(localization,l10n)指讓產(chǎn)品(出版物,軟件,硬件等)能夠適應(yīng)非本地環(huán)境,特別是其他的語言和文化。程序在不修改內(nèi)部代碼的情況下,能根據(jù)不同語言及地區(qū)顯示相應(yīng)的界面。
國際化資源文件:用不同國家的語言描述相同的信息,并放在各自對應(yīng)的.properties屬性文件中,程序根據(jù)運行時環(huán)境決定加載哪個文件。國際化主要通過以下類完成:java.util.Locale:對應(yīng)一個特定的國家/區(qū)域、語言環(huán)境。java.util.ResourceBundle:用于加載一個資源包。I18nInterceptor:struts2所提供的國際化攔截器,負責(zé)處理Locale相關(guān)信息。國際化流程:程序得到當(dāng)前運行環(huán)境的國家/區(qū)域、語言環(huán)境并存放于Locale,ResourceBundle根據(jù)Locale中信息自動搜索對應(yīng)的國際化資源文件并加載。當(dāng)某個Action被執(zhí)行前,I18nInterceptor負責(zé)檢測Locale相關(guān)信息來尋找對應(yīng)的國際化資源
先來看一下實例的效果圖:
默認(rèn)中文:
點擊英文,頁面變成英文顯示:
接下來看具體的實現(xiàn):
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>
<!-- 自動發(fā)布 -->
<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到這里就實現(xiàn)了一個簡單的國際化的小實例,有的說弄國際化沒多大必要,因為咱們做的東西幾乎都只是用于國內(nèi)。我想說的是:為什么就覺得我們做的東西只會給我們自己用?為什么就不會覺得隨著我們不斷的努力,我們做的東西會越來越好,然后我們的東西會逐漸打入國際市場,讓老外用我們做的東西呢?你們有信心嗎?
更多建議: