20060911_spring_中文问题

  既然生为中国人,就没有什么好抱怨的了,遇到编码的问题,那只有解决它了。

  如果经常使用 Struts,并做过国际化操作的人来说,对于中文资源文件的处理应该不会感到陌生的。比如下面两个文件,一个是英文的,一个是中文的。对于英文的,一切照常;对于中文的,一般需要用JDK的native2ascii工具进行转换。

application_en.properties
# Resources for the Hello sample application
hello.title=A first Struts program
hello.prompt.user=Please enter a UserName
hello.page.hello=Hello
hello.page.wellocome=Wellcome to Blog of Javamxj


application_zh_CN.properties
# Hello实例使用的资源文件

hello.title=第一个Struts应用
hello.prompt.user=请输入用户名
hello.page.hello=你好
hello.page.wellocome=欢迎来到Javamxj的Blog

  我当前使用的Eclipse版本是3.1M6,如果使用Eclipse编辑application_zh_CN.properties文件,保存后,关闭这个文件,然后再次打开它,会发现这个文件中的中文字符都变成了????。

  为什么会发生这种情况呢?在中文操作系统下,Eclipse中的 Java类型文件的编码的默认设置是GBK,但是对Properties资源文件的编码的默认设置是 ISO-8859-1。所以编辑Java文件中的中文不会出现问题,但编辑Properties资源文件中的中文会出现问题。

  在3.1M6版本下,我们可以直接修改Properties资源文件的默认编码了(Eclipse3.0版本好像不支持)。在Eclipse主界面下,打开 Window ->Perferences -> General ->Editors ->Context Types:


  修改为GBK后,再次打开application_zh_CN.properties文件,编辑后保存,再打开时,文件中的中文字符可以正常显示了。

  这还不行,还必须利用native2ascii来转换一下。

  native2ascii-本地码-至-ASCII 码转换器

  将含有本地编码字符(既非 Latin1 又非 Unicode 字符)的文件转换为 Unicode 编码字符的文件。

  结构

native2ascii [ options] [inputfile [outputfile]]

  说明

  Java 编译器和其它 Java 工具只能处理含有 Latin-1 和/或 Unicode 编码(/udddd 记号)字符的文件。native2ascii 将含有其它字符编码的文件转换成含 Latin-1 和/或 Unicode 编码字符的文件。

  若省略 outputfile,则使用标准输出设备输出。此外,如果也省略 inputfile,则使用标准输入设备输入。

  选项

  -reverse

  执行相反的操作:将含 Latin-1 和/或 Unicode 编码字符的文件转换成含本地编码字符的文件:

  - encoding encoding_name

  指定转换过程使用的编码名称。缺省的编码从系统属性 file.encoding 中得到。encoding_name 字符串必须是符合要求的字符串。

  比如可以这样利用native2ascii:

native2ascii -encoding gbk application_zh_CN.properties temp.properties

  这里将根据application_zh_CN.properties输出了一个 临时文件temp.properties,然后把application_zh_CN.properties中的文件内容替换为这个临时文件的内容就可以了。

  转换后的application_zh_CN.properties

# Hello/u5b9e/u4f8b/u4f7f/u7528/u7684/u8d44/u6e90/u6587/u4ef6

hello.title=/u7b2c/u4e00/u4e2aStruts/u5e94/u7528
hello.prompt.user=/u8bf7/u8f93/u5165/u7528/u6237/u540d
hello.page.hello=/u4f60/u597d
hello.page.wellocome=/u6b22/u8fce/u6765/u5230Javamxj/u7684Blog

  这样的操作对于一个或两个资源文件还行,如果多个资源文件,这样未免有些太麻烦了。你可以建立一个批处理文件(. bat);或者建立一个Ant文件,利用其包含的native2ascii任务,也可以进行批处理。

  还是觉得麻烦!

  但是自从使用Eclipse后,好处多多,在Eclipse下,有这么两个插件,可以用来处理资源文件,它们使用起来各有千秋。

  JInto 0.8.0

  主页 http://www.guh-software.de/jinto.html

  下载 http://www.guh-software.de/jinto/de.guhsoft.jinto_0.8.0.zip

  文档 http://www.guh-software.de/jinto/JIntoGettingStarted.pdf (英文)

  仔细看看文档,它提供的功能还是很不错的。

  Properties Editor

  主页 http://propedit.sourceforge.jp/index_en.html

  它提供了三个版本,单独运行的版本、Eclipse版本、 JBuilder版本,这里只看看Eclipse3下的版本。

   PropertiesEditor_EclipsePlugin_for_3.0 V4.4.0

  分别以links方式安装这两个插件,启动Eclipse,切换到Perferences,如图:

  · JInto插件


  · Properties Editor插件


  · 先利用Properties Editor插件编辑资源文件,选中application_zh_CN.properties,右击,选择对应的插件,如图操作:


  · Properties Editor插件编辑的效果


  · 然后利用JInto插件编辑资源文件:


  附带说说XML文件

  对应XML文件类型,默认的编码是UTF-8,但如果在xml声明中可以指定其编码,如“<?xml version="1.0" encoding="GBK"?>”就指定其编码为GBK。这样,Eclipse在读取xml文件时,会首先根据其声明来设置这个文件的编码,如果在声明中没有指定其编码,那么会采用UTF-8编码来读取这个文件。所以对于xml文件,就不需要强制设置其默认的编码类型了。

 

我用propertiesEditor

下面是它的安装方法:

转自:http://propedit.sourceforge.jp/howto_eclipseplugin_install_en.html

[ INSTALLATION ]

  1. Please choose from the screen of Eclipse with "Help" ->"Software Updates" -> "Update Manager". An 'Update Manager' opens.

  2. In the "Feature Updates" view at the lower left of an 'Update Manager', please carry out the right click of the "Sites to Visit", and create a site bookmark by "New" -> "Site Bookmark...".
    - The bookmark to create should input the following "URL" and should push an "Finish" button.
    Name: Arbitrary input
    URL : http://propedit.sourceforge.jp/eclipse/updates/
    Bookmark type: Eclipse update site

  3. If a site bookmark is created, the bookmark created at the bottom of "Feature Updates" will appear.
    A click of "jp.gr.java_conf.ussiy.app.propedit.eclipse.feature.PropertiesEditorFeature x.x.x" displays a preview on a right window. Since the button "Install Now" is in around the lower right, please click.

  4. Since an installation wizard starts, please click a "Next" button rapidly.

  5. "You will need to restart the workbench for the changes to take effect. Would you like to restart now?" is displayed. Please reboot Eclipse according to a dialog.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值