Why don't JSF property files accept non-ASCII characters in Eclipse?

I wonder why JSF property files do not accept non-ASCII characters in Eclipse?

I have a properties file called "messages.properties", I have to write uncode characters for escapped for non-ASCII characters, for example:

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

This means that clients cannot edit these property files using regular text editors.

Is there any solution?

+4
source share
3 answers

This is because java.util.Properties.load(InputStream) uses ISO-8859-1.

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

This has been a problem for ages. I solved this with struts by creating custom tags that do encoding conversions, but this is usually a pain.

Java 6 introduces Properties.load(Writer) , which works great with UTF-8, but doesn't seem to be widely used yet.

I would suggest using AnyEdit tools to convert to and from Unicode.

As for the end users - if they have to edit the properties files (which sounds strange), then you can let them write any characters they like and later convert the files using native2acii (or its wrapper)

+3
source

This is no longer necessary since Eclipse 4.2 (Juno, 2012). Eclipse will take care of this transparently when you use the built-in property file editor. It will display and accept values ​​in UTF-8, but it will silently convert under the cover to the format \uXXXX . It should be noted that this has some strange side effects in combination with the Git plugin (for example, old lines deleted during merge), it works best if you close all property files before pulling / clicking.

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

You can save the "original" property files (for example, give them the extension .utf8 ) and use the batch / shell file to convert them as follows:

  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 simply edit the .utf8 files and run the batch / shell script once to convert your own characters to \uXXXX . Also see this blog post .

See also:

+1
source

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

-2
source

Source: https://habr.com/ru/post/1306384/


All Articles