Friday December 12, 2008
JavaFX localization is easy and simple. If you know Java, it's very similar, but even better
Here I am going to walk you through some basic steps required to localize a very simple JavaFX application.
1. Create an application.
Here is my simple JavaFX GUI application in English. Note that the messages are externalized.
=================================================== import javafx.stage.Stage; import javafx.scene.*; import javafx.scene.text.Font;
import javafx.ext.swing.*; import javafx.scene.layout.VBox; var v1 = ##"My Label"; var v2 = ##"Your Label"; var v3 = ##"Our Label"; var v4 = ##"Hello Button"; var v5 = ##"Goodbye Button"; Stage { title: "JavaFX Localization Test" width: 300 height: 300 visible: true scene: Scene { content: VBox { spacing:5 content: [ SwingLabel {height: 30 width: 250 text: v1}, SwingLabel {height: 30 width: 250 text: v2}, SwingLabel {height: 30 width: 250 text: v3}, SwingButton {height: 30 width: 250 text: v4}, SwingButton {height: 30 width: 250 text: v5} ] } } } ===================================================
2. Compile and execute it.
# javafxc test.fx # javafx test
OK, everything looks good so far.
3. Create an fxproperty file.
Let's localize the strings. Here you can see my test_ja.fxproperties. Note that I can embed Japanese characters directly in the file - no more encrypted escape sequences!
=================================================== "My Label" = "私のラベル" "Your Label" = "あなたのラベル" "Our Label" = "私たちのラベル" "Hello Button" = "こんにちはボタン" "Goodbye Button" = "さようならボタン" ===================================================
4. Execute it.
Wow, the application is already localized! However, the fonts are kind of ugly here...
I will cover how I can fix this problem in my next blog. Stay tuned
Posted by naoko
( Dec 12 2008, 05:30:45 PM PST )
Permalink
Comments [6]
| « November 2009 | ||||||
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | |||||
| Today | ||||||
Today's Page Hits: 42
人过留名
Posted by 手机魔术卡 on December 12, 2008 at 10:02 PM PST #
Excellent! Thanks!
Posted by Eric Wendelin on December 15, 2008 at 08:58 PM PST #
Thanks Naoko
but I don't know what is the restriction
I are trying to create a Test.fx
and Test.properties in same package
and use a ## for extension
but it doesn't work
Posted by jyc5131 on December 16, 2008 at 04:51 PM PST #
Hi jyc5131,
Thank you for checking it out. I am not sure what you mean by "use ## for extension" - your fxproperties file should be named with a locale / country code suffix. In my case, it is test_ja.fxproperties, because it is in Japanese. It will be test_zh_CN.fxproperties for Simplified Chinese, to give you another example.
As for packaging, the following worked for me.
1. Add "package myPackage;" to test.fx and place it under myPackage directory.
2. Your fxproperties file does not need package declaration. Simply put the file under myPackage directory.
Hope this helps.
--Naoko
Posted by naoko on December 18, 2008 at 02:01 PM PST #
Naoto,
I had to set my JVM runtime argument to
-Duser.language="ja". Correct?
Cheers,
Inyoung
Posted by Inyoung on February 11, 2009 at 08:56 PM PST #
Hi Inyoung,
Are you running the application in a locale other than Japanese? If yes, then you have to set the locale to Japanese, like you did. Alternatively, you can set the default locale within the application itself, like this.
Locale.setDefault(Locale.JAPAN);
You can also refer to my previous posting:
http://blogs.sun.com/naoko/entry/how_to_internationalize_a_javafx
Hope this helps.
--Naoko
Posted by naoko on February 13, 2009 at 10:26 AM PST #