开发者

Why do JSF properties files not accept non-ASCII characters in Eclipse?

开发者 https://www.devze.com 2022-12-26 18:17 出处:网络
I wo开发者_高级运维nder, why do JSF properties files not accept non-ASCII characters in Eclipse?

I wo开发者_高级运维nder, why do JSF properties files not accept non-ASCII characters in Eclipse?

I have some properties file named "messages.properties", I've to write in unicode escapped characters for non-ASCII characters, example:

title=\u01af\u0020\u0a3f0
header=\u0ff0\u0020\u0ab1

This means that customers cannot edit these properties files using regular text editors.

Is there any solution?


This stems in the fact that java.util.Properties.load(InputStream) uses ISO-8859-1.

The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

It has been a problem for ages. I've solved it with struts by implementing custom tags that make encoding transformations, but it is generally a pain.

Java 6 introduces the Properties.load(Writer), which works fine with UTF-8, but it seems it isn't widely adopted yet.

I'd suggest using AnyEdit tools to convert to and from the unicode notation.

As for the end-users - if they are to edit properties files (which sounds strange), then you can let them write in whatever characters they like and later convert the files using native2acii (or a wrapper of it)


This is not necessary anymore since Eclipse 4.2 (Juno, 2012). Eclipse will take care of this transparently when you use the builtin properties file editor. It will present and accept the values in UTF-8, but it will under the covers silently convert to \uXXXX format. Noted should be that this has some strange side effects in combination with Git plugin (e.g. old lines deleted during merge), it works best if you close all properties files before pulling/pushing.

If you're not on Eclipse 4.x yet, consider using native2ascii tool found in /bin folder of the JDK installation directory to convert UTF-8 properties files to ASCII properties files, as described in javadoc of java.util.Properties class.

You can keep the "original" properties files (give them for example an .utf8 extension) and use a batch/shell file to convert them like this:

cd c:\path\to\properties\files
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_cs.properties.utf8 text_cs.properties
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_ja.properties.utf8 text_ja.properties
c:\path\to\jdk\bin\native2ascii.exe -encoding UTF-8 text_zh.properties.utf8 text_zh.properties
# You can add more properties files here.

This way you can just edit the .utf8 files and run the batch/shell script once to do the conversion of native characters to \uXXXX. Also see this blog entry.

See also:

  • How to use UTF-8 in resource properties with ResourceBundle


just save the .properties file as UTF-8 format and it works for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号