"Go" Using Eclipse to edit a Chinese resource file (application_zh_cn.properties)

Source: Internet
Author: User

Since the birth of Chinese people, there is no good to complain about, and encountered the problem of coding, it is only to solve it.

If you frequently use Struts, and for those who have done international operations, should not be unfamiliar with the processing of Chinese resource files. For example, the following two files, one is in English, one is Chinese. For English, everything is as usual; for Chinese, it is generally necessary to use the JDK's NATIVE2ASCII tool for conversion.

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
# The resource file used by the Hello instance

hello.title= First Struts Application
hello.prompt.user= Please enter your user name
Hello, hello.page.hello=.
Hello.page.wellocome= Welcome to JAVAMXJ Blog


I am currently using the Eclipse version is 3.1M6, if you use Eclipse to edit the application_zh_cn.properties file, after saving, close the file, and then open it again, you will find the file in the Chinese characters have become????.

Why is this happening? under the Chinese operating system, the default setting for encoding Java-class files in Eclipse is GBK, but the default setting for encoding properties resource files is ISO-8859-1. So there is no problem in editing the Chinese in the Java file, but there is a problem editing the Chinese in the Properties resource file.

under the 3.1M6 version, we can directly modify the properties resource file's default encoding (the Eclipse3.0 version does not seem to support). Under the Eclipse main screen, open Window->perferences,General ->editors->context Types:


after modifying to GBK, open the Application_zh_cn.properties file again, save it after editing, and then open it, the Chinese characters in the file can be displayed normally.

That's not going to work , and you have to use NATIVE2ASCII to convert.

native2ascii-Local code-to-ASCII code converter

will contain locally encoded characters (both non-Latin1 and non-Unicodecharacters) to a file that is converted to Unicode encoded characters.

structure

Native2ascii [Options] [inputfile [outputfile]]

Description

JavaCompilerand other Java tools can only handle files that contain Latin-1 and/or Unicode encoding (\UDDDD notation) characters. Native2ascii converts a file containing other character encodings to a file containing Latin-1 and/or Unicode encoded characters.

if OutputFile is omitted, the output of the standard output device is used. Also, if Inputfile is omitted, the standard input device input is used.

Options

-reverse

do the opposite: convert a file with Latin-1 and/or Unicode encoded characters to a file with locally encoded characters:

-EncodingEncoding_name

Specifies the encoding name used by the conversion process. The default encoding is obtained from the system attribute file.encoding. The Encoding_name string must be a string that meets the requirements.

For example, you can use NATIVE2ASCII:

native2ascii-encoding GBK application_zh_cn.propertiesTemp. Properties

This will output a application_zh_cn.properties based on theTemporary filestemp.properties, and then replace the contents of the file in the application_zh_cn.properties with the contents of the temporary file.

Application_zh_cn.properties after the conversion

# 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

Such an operation is OK for one or two resource files, which is a bit cumbersome if you have multiple resource files. You can create a batch file (.Bat) or create an ant file that takes advantage of the Native2ascii tasks it contains and can also be batched.

still feel the trouble!

but since the use of eclipse, there are a lot of benefits, under Eclipse, there are so two plug-ins, can be used to work with resource files, they are very useful.

Jinto 0.8.0

Home PageHttp://www.guh-software.de/jinto.html

DownloadHttp://www.guh-software.de/jinto/de.guhsoft.jinto_0.8.0.zip

DocumentHttp://www.guh-software.de/jinto/JIntoGettingStarted.pdf(English)

Take a closer look at the documentation and it provides a good feature.

Properties Editor

Home PageHttp://propedit.sourceforge.jp/index_en.html

It offers three versions, a standalone version, an Eclipse version,JBuilderversion, just look at the version under ECLIPSE3.

  propertieseditor_eclipseplugin_for_3.0V4.4.0

Install the two plugins in links mode, start Eclipse, switch to Perferences,

• Jinto plug-in


• Properties Editor plugin


· First use the Properties Editor plugin to edit the resource file, select Application_zh_cn.properties, right click, select the corresponding plug-in, Operation:


· Properties Editor plug-in editing effect


· Then use the Jinto plugin to edit the resource file:


Incidentally, XML file

corresponding XML file type, the default encoding is UTF-8, but if its encoding can be specified in the XML declaration, such as "<?xmlversion=" 1.0 "encoding=" GBK "?>" Specifies that it is encoded as GBK. Thus, when eclipse reads an XML file, it first sets the encoding of the file according to its declaration, and if it does not specify its encoding in the declaration, it is read with UTF-8 encoding. Therefore, for an XML file, you do not need to force the default encoding type to be set.

original link: http://dev.yesky.com/454/2014454.shtml(JAVAMXJ Editor: Ark)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.