3

I need to create i18n properties files for non-Latin languages (simplified Chinese, Japanese Kanji, etc.) With the Swing portion of our product, we use Java properties files with the raw UTF-8 characters in them, which Netbeans automatically converts to 8859-1 for us, and it works fine. With JavaFX, this strategy isn't working. Our strategy matches this answer precisely which doesn't seem to be working in this case.

In my investigation into the problem, I discovered this old article indicating that I need to use native2ascii to convert the characters in the properties file; still doesn't work.

In order to eliminate as many variables as possible, I created a sample FXML project to illustrate the problem. There are three internationalized labels in Japanese Kanji. The first label has the text in the FXML document. The second loads the raw unescaped character from the properties file. The third loads the escaped Unicode (matching native2ascii output).

jp_test.properties

btn.one=閉じる
btn.two=\u00e9\u2013\u2030\u00e3\ufffd\u02dc\u00e3\u201a\u2039

jp_test.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?scenebuilder-preview-i18n-resource jp_test.properties?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="147.0" prefWidth="306.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <Label layoutX="36.0" layoutY="33.0" text="閉じる" />
    <Label layoutX="36.0" layoutY="65.0" text="%btn.one" />
    <Label layoutX="36.0" layoutY="97.0" text="%btn.two" />
    <Label layoutX="132.0" layoutY="33.0" text="Static Label" textFill="RED" />
    <Label layoutX="132.0" layoutY="65.0" text="Properties File Unescaped" textFill="RED" />
    <Label layoutX="132.0" layoutY="97.0" text="Properties File Escaped" textFill="RED" />
  </children>
</AnchorPane>

Result

Help me fix the third label

As you can see, the third label is not rendered correctly.

Environment:

Java 7 u21, u27, u45, u51, 32-bit and 64-bit. (JavaFX 2.2.3-2.2.45)

Windows 7 Enterprise, Professional 64-bit.

UPDATE

I've verified that the properties files is ISO 8859-1

enter image description here

Community
  • 1
  • 1
mawcsco
  • 624
  • 6
  • 18

2 Answers2

1

Most IDEs (NetBeans at least) handle the files in unicode encoding by default. If you are creating the properties files in NetBeans and entering the Japanese text in it, then the entered text will be automatically encoded to utf. To see this open the properties file with notepad(++), you will see that the Japanese characters are escaped.
The utf escaped equivalent of "閉じる" is "\u9589\u3058\u308b", whereas "\u00e9\u2013\u2030\u00e3\ufffd\u02dc\u00e3\u201a\u2039" is "é–‰ã�˜ã‚‹" on reverse side. So the program output in the picture is correct. Additionally, if you reopen the jp_test.properties file in NetBeans, you will see the escaped utf encoded texts will be seen as decoded.

EDIT: as per comment,
Why does it do this?
It maybe because you are omitting the -encoding parameter of native2ascii, then the default charset of your system may not be UTF. This maybe the reason of that output.

Also, why is it that Java and Swing have no problems with our properties files as they are,
but FXML can't handle it?

It cannot be the case, because the "FXML is a Java". The only difference may also be the "usage of system charset" vs "overriding the charset in some configuration place".

Anyway, I suggest using right encoding parameter of native2ascii according to the input files encoding. More specifically, convert the properties files to utf-8 encoding first then do the rest. If you are using NetBeans as IDE, then no need for native2ascii.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • So, `\u00e9\u2013\u2030\u00e3\ufffd\u02dc\u00e3\u201a\u2039` is from Java's `native2ascii` tool executed on `閉じる`. Why does it do this? Also, why is it that Java and Swing have no problems with our properties files as they are, but FXML can't handle it? – mawcsco Mar 14 '14 at 13:56
  • So, you got me pointed in the right direction. The files we got from our translator were missing a BOM. After adding the BOM, both Netbeans and native2ascii generate the correct escaped characters. – mawcsco Mar 14 '14 at 17:57
0

Properties files should be ISO 8859-1 encoded, not UTF-8.

Characters can be escaped using \uXXXX.

Tools such as NetBeans are doing this by default, AFAIK.

http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html

http://docs.oracle.com/javase/tutorial/i18n/text/convertintro.html

Puce
  • 37,247
  • 13
  • 80
  • 152