Struts application i18n

Java 为应用程序的国际化提供了良好的支持, Struts 也继承了这一优点。通过全程使用 unicode 编码可以非常简单地实现应用的国际化。

1,全部页面声明使用 UTF-8
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

2,建立一个 filter 来过滤所有页面文件,以确保始终使用 UTF-8 来分析所有的 incoming request.

4,以 English 来编写 default 的消息资源(message resources)文件 ApplicationResources.properties (文件名可以自行在 struts-config.xml 中指定),然后再基于这个消息资源文件翻译成想要的语言。

5,以翻译成简体中文为例子。
先新建一个文本文件 ApplicationResources_zh_CN_original.properties,将其中的英文翻译成中文,然后用 native2ascii 指令转换之:
# native2ascii -encoding GB18030 ApplicationResources_zh_CN_original.properties ApplicationResources_zh_CN.properties
注:翻译文件(original)一定要在使用“本地”字符集的系统上建立,比如我使用 Windows XP Simplified Chinese 来翻译,所以我的文本文件的缺省编码就是 GB18030.  若我们需要一个繁体中文的翻译,那么应该确保使用的系统缺省编码是 big5.

4,把生成的 ApplicationResources_zh_CN.properties 放到相应的位置下即可。

注意:
启动 tomcat 的用户的环境变量中的 LANG 一定要设置为 en_US.UTF-8

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">