Solving the garbled problem in the text of properties file

Source: Internet
Author: User
If you are a frequent user of struts and international operations, you should not be unfamiliar with the handling of Chinese resource files. For example, the following two files, one is in English, one is Chinese. For English, everything goes as usual; for Chinese, it is generally necessary to use JDK's native2ascii tools for conversion.
  
Application_en.properties
# Resources for the Hello sample application
Hello.title=a-A-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 instance Use resource file
  
hello.title= First Struts Application
hello.prompt.user= Please enter user name
hello.page.hello=, hello.
Hello.page.wellocome= Welcome to JAVAMXJ's Blog
  
The eclipse version I'm currently using is 3.1M6, if you use Eclipse to edit the application_zh_cn.properties file, save it, close the file, and then open it again, you'll find that the Chinese characters in the file become ...
  
Why is this happening? Under the Chinese operating system, the default setting for encoding Java type files in Eclipse is GBK, but the default setting for the encoding of the properties resource files is iso-8859-1. So editing the Chinese in the Java file will not cause problems, but editing the properties resource file will cause problems in Chinese.
  
Under the 3.1M6 version, we can directly modify the default encoding of the properties resource file (the Eclipse3.0 version does not seem to support). Under the Eclipse main interface, open Window >perferences >general >editors >context Types:
   
After modifying to GBK, open the Application_zh_cn.properties file again, edit and save, and then open, the Chinese characters in the file can be displayed normally.
  
This is not enough, you must also use NATIVE2ASCII to convert.
  
native2ascii-Local code-to-ASCII code converter
  
A file that converts files containing locally encoded characters (both Latin1 and non-Unicode characters) to Unicode encoded characters.
  
Structure
  
native2ascii [Options] [Inputfile [OutputFile]]
  
Description
  
The Java compiler and other Java tools can only handle files that contain Latin-1 and/or Unicode encoding (/UDDDD notation) characters. Native2ascii converts files containing other character encodings into files containing Latin-1 and/or Unicode encoded characters.
  
If OutputFile is omitted, the standard output device output is used. In addition, if Inputfile is omitted, the standard input device input is used.
  
Options
  
-reverse
  
To do the opposite: convert a file with Latin-1 and/or Unicode encoded characters to a file that contains local encoded characters:
  
-encoding Encoding_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 in this way:
  
NATIVE2ASCII encoding GBK application_zh_cn.properties temp.properties
  
Here will be based on application_zh_cn.properties output a temporary file temp.properties, and then put Application_zh_ The contents of the file in the cn.properties are replaced with the contents of the temporary file.
  
Application_zh_cn.properties after 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, and if multiple resource files, this is a bit too much trouble. You can create a batch file (. bat), or you can create an ant file, take advantage of the Native2ascii tasks it contains, or batch.
  
But since Eclipse has a lot of benefits, with Eclipse, there are two plug-ins that can be used to process resource files, and they are all the same.
  
Jinto 0.8.0
  
Home Http://www.guh software.de/jinto.html
  
Download Http://www.guh-software.de/jinto/de.guhsoft.jinto_0.8.0.zip
  
Document Http://www.guh-software.de/jinto/JIntoGettingStarted.pdf (English)
  
Take a closer look at the document and it offers a nice feature.
  
Properties Editor
  
Home http://propedit.sourceforge.jp/index_en.html
  
It offers three versions, a stand-alone version, an Eclipse version, a jbuilder version, and only a look at the ECLIPSE3 version.
  
propertieseditor_eclipseplugin_for_3.0 V4.4.0
  
Install the two plug-ins in links, start Eclipse, and switch to Perferences, as shown:
  
? Jinto Plugin
   
? Properties Editor Plugin
   
? First Use Properties Editor Plug-ins edit the resource file, select Application_zh_cn.properties, right-click, select the corresponding plug-ins, such as figure operation:
   
? Effects of Properties editor plug-in editing
   
? Then use the Jinto plug-in to edit the resource file:
  
corresponding to the XML file type, the default encoding is UTF-8, but if you can specify its encoding in the XML declaration, such as "", specify its encoding as GBK. This way, when eclipse reads an XML file, it first sets the encoding of the file according to its declaration, and if the code is not specified in the declaration, then the file is read using the UTF-8 encoding. Therefore, you do not need to force the default encoding type for the XML file.

Only try the Native2ascii code conversion This method, it is very useful. Feel no need to install too many plug-ins.

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.