呆呆向前冲的blog

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  78 随笔 :: 43 文章 :: 5 评论 :: 74 Trackbacks

步骤
1 资源文件
     添加资源文件:JB里默认的资源文件名称为ApplicationResources.properties。需要添加和语言地区对应的资源文件。如:ApplicationResources_zh_CN.properties、ApplicationResources_en_US.properties
      注意:命名一定要注意后缀的正确。否则struts不能自动加载

      转换编码:将资源文件内容转换为unicode编码。以中文资源文件转换为例,命令如下:            

native2ascii -encoding GBK src/zh_cn.properties src/ApplicationResources_zh_CN.properties
            注:zh_cn.properties文件是暂存原始内容的文件

      配置资源文件到struts-config.xml中:在配置文件中加入如下资源文件引用

<message-resources parameter="ApplicationResources" />

2 设置JSP编码为UTF-8

3 切换当前Locale
      Struts是根据Session中存储的关键字为org.apache.struts.action.LOCALE的Locale对象判断当前的Locale,
并自动加载相应的资源文件。
      即如果当前locale是zh CN,那么会自动加载 ApplicationResources_zh_CN.properties。所以如果要显示多语言版本,必须切换当前locale。例子如下:
      切换Locale的Action代码:

public class SetLocaleAction extends Action {
  
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest req, HttpServletResponse httpServletResponse) {
    String langFlag 
= req.getParameter("lang");
    String[] lang 
= langFlag.split("_");
    Locale locale 
= new Locale(lang[0],lang[1]);
    HttpSession session 
= req.getSession(true);
    session.setAttribute(
"org.apache.struts.action.LOCALE",locale);
    
return actionMapping.findForward("forward");
  }

}

      页面文件中调用该Action的链接:

<html:link action="setLocale.do?lang=zh_CN">chinese</html:link>
<html:link action="setLocale.do?lang=en_US">America</html:link>

不过对于Java中的编码问题,理解的还不透彻。比如中文字符在页面之间的传递问题。如何保证能正确的接收各种语言的输入。

posted on 2005-08-28 03:34 呆呆向前冲的blog 阅读(1299) 评论(0)  编辑  收藏 所属分类: 工作:Java技术

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


网站导航: