Naoko Hamamoto's Weblog

« Previous day (Dec 18, 2008) | Main | Next day (Dec 20, 2008) »

20081219 Friday December 19, 2008

JavaFX Localization Part 3: Specifying fxproperties File Location

So far, I have always used the default search mechanism to locate fxproperties file - JavaFX script picks up the fxproperties file that has the same name as the script itself and also locates in the same directory as the script itself.

Sometimes we may want to specify which fxproperties file to associate with the script. In this blog, I explain how to programatically accomplish it.

Turns out there is an easy and simple way to do it.

All we need is the following API.

==============================================
package: javafx.util.StringLocalizer

public associate(properties: java.lang.String, packageName: java.lang.String) : Void 
public associate(properties: java.lang.String, packageName: java.lang.String, scriptFileName: java.lang.String) : Void
==============================================

The first associate function allows me to specify an fxproperties file for all the scripts in a package, and the second associate function allows me to specify an fxproperties file for a particular script in a package.

So let's see it in action.

Here is my directory structure.

==============================================
/home/naoko/myPackage
   test.fx (this file has package declaration at the top of the file)
/home/naoko/myPackage/resources
   myResources_ja.fxproperties
==============================================

In test.fx, I add the following line.

==============================================
StringLocalizer.associate("myPackage.resources.myResources", "myPackage");
==============================================

Then I compile and execute it.

==============================================
# /home/naoko/myPackage/javafxc test.fx
# cd ..
# /home/naoko/javafx myPackage.test
==============================================

It should be picking up the strings from myResources_ja.fxproperties file.

Note: Make sure to include this line BEFORE you use ## to define strings to be translated.

The API doc explains this function as well as dissociate function very well. http://java.sun.com/javafx/1/docs/api/javafx.util/javafx.util.StringLocalizer.html

I had a difficult time at first, because I initially tried to use the class loader to load the fxproperties file (just like how we can do it in Java). I guess that approach does not work with JavaFX. Learning something new everyday!

Posted by naoko ( Dec 19 2008, 03:45:55 PM PST ) Permalink Comments [1]


Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.