\*Easier\* localization in JavaFX Script

In the last blog entry, I explained the proposal for an easy way to localize string literals in JavaFX Script language. Now the implementation for it is in the OpenJFX Compiler's repository, let's take a look at this feature in action. As an example, I localized the JavaFXBalls demo into Japanese (it's in the repository as well):

You see the text in the button at the bottom left corner reading "ストップ", which means "Stop". In this button's object literal creation, the source code looks like:

text: bind if (not test._is_running) then ##"Start" else ##"Stop"

And it's JavaFX properties file entry is simply:

"Stop" = "ストップ"

Now, we are planning to extend this string literal localization proposal into a generic string localization runtime library function. Suppose that there is a JavaFX runtime library class, e.g., javafx.util.StringLocalizer, and it has the following APIs:

package javafx.util;

public class StringLocalizer {

    public attribute key: String;
    public attribute locale: java.util.Locale;
    public attribute packageName: String;
    public attribute propertiesName: String;
    public attribute defaultString: String;

    // this is lazily bound to the above attributes.
    public function localizedString(): String;

    public static function associate(packageName: String, 
                                     scriptFileName: String,
                                     properties: String): Void;
}

With these utility APIs, you could do something like:

// Object creation
var localizer = StringLocalizer{ key: "Hello, World!" };

// This prints localized text for "Hello, World!" for the default locale
System.out.println(localizer.localizedString()); 

// This prints localized text for "Duke" for the default locale
localizer.key = "Duke";
System.out.println(localizer.localizedString()); 

// This prints localized text for "Duke" for the French locale
localizer.locale = Locale.FRENCH;
System.out.println(localizer.localizedString()); 

// This prints localized text for "Duke", from 
// the FX properties file "foo/bar/MyBundle_fr.fxproperties
localizer.packageName = "foo.bar";
localizer.propertiesName = "MyBundle";
System.out.println(localizer.localizedString()); 

Also, with the "associate()" static function, you could associate a JavaFX Script source file (or a javafx package) to a JavaFX properties file so that the string literal localization consults the associated JavaFX properties file for searching the localized text. (By default, the JavaFX properties file with the same name/location as the source file is searched). This static function would provide developers with the granularity of how the JavaFX properties files are packaged. You could provide one single JavaFX properties for your entire application, or one properties file for each source script file, or in between.

This is still a rough idea and needs to be refined. But I'd hope this would contribute to an \*easier\* localization in JavaFX.

Comments:

In general, I like this idea. However, I think it is still a little too difficult to work with keys. Although setting the key before calling localizedString is ok, I would also like to see this:

public function localizedString(key: String): String

So, if I pass in a key, the function should use that key instead. If the key doesn't exist, then maybe it should return the key back as the localized string (for the default).

What do you think?

Posted by John O'Conner on February 15, 2008 at 02:02 PM PST #

Hi John, thank you for the comments. Initially I thought the same thing, passing a key and getting the localized text. Then I'm inclined to a more declarative approach as this, which I think is more aligned to the whole JavaFX language concept.

Another merit would be that, you would not need to passing around the 'key' with your code path as an argument.

Posted by Naoto Sato on February 19, 2008 at 02:11 AM PST #

Post a Comment:
Comments are closed for this entry.
About

naotoj

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
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
News

No bookmarks in folder

Blogroll