posts - 32,  comments - 149,  trackbacks - 0

1.引言
  因为最近有个英国的项目要求中英文切换,又由于我们开发是用struts+hibernate的,所以我们就把原先做两个版本的构思抛掉,就用struts的国际化处理方案。
2.资料
  首先,我们要准备两份配置文件中文和英文的
  中文:ApplicationResources_zh_CN.properties
  英文:ApplicationResources_en.properties  (千万不要用默认的ApplicationResources.properties myeclipse自动生成,可能会不成功,我起先就是因为这个怎么都不行的,呵呵!)
3.准备开始
1) ApplicationResources_zh_CN.properties的内容
#zh_CN
username=纯冰
2)ApplicationResources_en.properties的内容
#en
username=chunkyo
3)一个action
/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.lan.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.Globals;
import java.util.Locale;

/**
 * MyEclipse Struts
 * Creation date: 02-07-2007
 *
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class ChangeLanguageAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
  String lan=request.getParameter("lan");
  if(lan.equals("1"))
  {
   request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA);
  }
  else if(lan.equals("0"))
  {
   request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.ENGLISH);
  }
  else
  {
   request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA);
  }
  return mapping.findForward("index");
 }
}

4)一个页面
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
  <head>
    <html:base />
   
    <title><bean:message key="username"/></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>
    <html:link page="/changeLanguage.do?lan=0">English</html:link>
    <html:link page="/changeLanguage.do?lan=1">Chinese</html:link>
    <bean:message key="username"/>
  </body>
</html:html>
4)配置文件(struts-config.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
  <data-sources />
  <form-beans />
  <global-exceptions />
  <global-forwards >
    <forward name="index" path="/index.jsp" />

  </global-forwards>

  <action-mappings >
    <action path="/changeLanguage" type="com.lan.struts.action.ChangeLanguageAction" />

  </action-mappings><message-resources parameter="com.lan.struts.ApplicationResources" />
</struts-config>

5)结果
 1。默认
http://localhost:8080/lan/
EnglishChinese 纯冰
2。英文
http://localhost:8080/lan/changeLanguage.do?lan=0
EnglishChinese chunkyo
3。中文
http://localhost:8080/lan/changeLanguage.do?lan=1
EnglishChinese 纯冰
6)结论
用struts做国际化处理 多国语言处理很方便 呵呵!

posted on 2007-02-07 13:08 chunkyo 阅读(3526) 评论(3)  编辑  收藏 所属分类: openSource(struts&hibernate&spring等等)

FeedBack:
# re: struts多国语言国际化处理(现以中文英文切换为例)
2007-02-08 18:57 | keith
当你使用tiles来组装不同JSP页面时,你就发现不那么好用了。有时候只有部分页面切换了,运行不稳定。  回复  更多评论
  
# re: struts多国语言国际化处理(现以中文英文切换为例)[未登录]
2007-02-12 01:57 | chunkyo
没有啊,我现在就一直再用的 没感觉不好用的,最近在做一个交友网站的,
英国方面要的,所以得有中英文切换,现在运行还行 没出现你说的!  回复  更多评论
  
# re: struts多国语言国际化处理(现以中文英文切换为例)[未登录]
2007-11-29 16:27 | jeff
请问一下,你在新建ApplicationResources_zh_CN.properties文件的时候,文件编码改为了什么。是UTF-8吗  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2007年2月>
28293031123
45678910
11121314151617
18192021222324
25262728123
45678910

这个博客主要是关于java技术和开源技术,大家一起来进步了!

常用链接

留言簿(12)

随笔分类

随笔档案

文章分类

收藏夹

DotNet

Java技术网站

Linux VS Unix

其他常去网站

常光顾的BLOG

文学类网站

游戏类网站

最新随笔

搜索

  •  

积分与排名

  • 积分 - 195809
  • 排名 - 290

最新评论

阅读排行榜

评论排行榜