Tuesday February 12, 2008 | Just my 2¢ Naoto Sato's Weblog |
|
All Languages |
English Only |
日本語のみ JavaFX Scriptの「より簡単な」ローカリゼーション機能 前回のエントリーで、JavaFX Script言語の文字列リテラルの簡単なローカリゼーションの提案について説明しました。現時点でその実装が OpenJFXコンパイラリポジトリにコミットされたので、実際にどう動いているかをみてみることができます。例としてこのリポジトリにあるJavaFXBallsというデモを日本語にローカライズしてみました(このローカリゼーション自体もリポジトリに入っています)。
左下のボタンのテキスト、「ストップ」がソースコード上どうなっているかというと、ボタンのオブジェクトリテラルを作っているところのコードは:
text: bind if (not test._is_running) then ##"Start" else ##"Stop" で、それに対応するJavaFXプロパティファイルのエントリーは:
"Stop" = "ストップ" これだけ、です。 さて、今現在考えているのは、この機能をソースコードの文字列リテラルのローカリゼーションだけではなく、より一般的なローカリゼーション機能として使えないかということ。例えばJavaFXのランタイムライブラリとして、javafx.util.StringLocalizerというクラスを作り、その APIで、
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;
}
としたらどうでしょう。これらのAPIを使うことにより、次のようなことが可能になるはずです。
// ローカリゼーションオブジェクトの作成。
var localizer = StringLocalizer{ key: "Hello, World!" };
// "Hello, World!"のデフォルトロケール用の翻訳を出力。
System.out.println(localizer.localizedString());
// "Duke"のデフォルトロケール用の翻訳を出力。
localizer.key = "Duke";
System.out.println(localizer.localizedString());
// "Duke"のフランス語の翻訳を出力。
localizer.locale = Locale.FRENCH;
System.out.println(localizer.localizedString());
// "Duke"のフランス語の翻訳を、"foo/bar/MyBundle_fr.fxproperties"
// から探してきて出力。
localizer.packageName = "foo.bar";
localizer.propertiesName = "MyBundle";
System.out.println(localizer.localizedString());
また、"associate()"スタティック関数を使うことにより、JavaFX Scriptのソースファイル(またはJavaFXのパッケージごと)をJavaFXプロパティにマップすることができます。これにより文字列リテラルのローカリゼーションを探すプロパティファイルを指定できるようになります(デフォルトではソースのJavaFX Scriptファイルと同じ名前・場所のJavaFXプロパティファイルから探します)。このスタティック関数を使うことで、アプリケーションのプロパティファイルをどのようにパッケージするかを柔軟に指定できるようになるのです。一つのプロパティファイルで全アプリケーションのローカリゼーションを保持するか、ソースファイルごとにプロパティファイルを持つか、あるいはその中間か、など。 これはまだまだアイディアの段階で、実際にコミットするときにはもっと洗練されていると思いますが、この機能によって「より簡単に」JavaFX Scriptのローカリゼーションが可能になれば、と思っています。 (2008-02-12 12:53:41.0) Permalink*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. (2008-02-12 12:13:39.0) Permalink Comments [2]またまたしばらく間が空いてしまいましたが、ここ最近はずっとJavaFX Scriptの国際化についての仕事をしています。JavaFX ScriptはJavaをベースにしたスクリプト言語なわけで、もちろんJavaに本来備わっている国際化に関する機能はすべて使えるわけですが、JavaFX ScriptからいちいちJavaの機能を呼び出して国際化関連のことを行うのはスクリプト言語らしくないし、さらにJava言語とJavaFX言語が混在してバグの元になりかねません。そこでJavaFX言語の仕様策定中ということもあり、その中に入れてしまいたいのがリテラル文字列を簡単にローカライズできる仕組みです。ある文字列をローカライズする場合、Javaでは普通、
import java.util.ResourceBundle;
static final String GREETINGS_KEY = "HELLO_WORLD";
String greetings;
try {
ResourceBundle rb = ResourceBundle.getBundle("foo.bar.resources.My_Resource");
greetings = rb.getString(GREETINGS_KEY);
} catch (MissingResourceException mre) {
greetings = "Hello, World!";
}
の様に書くと思いますが、いま模索中なのがGNUのgettext()関数のように、引数となる文字列がそのままデフォルトの翻訳になるような仕組みです。上のコードをそのシンタックスで表記すると、
var greetings = ##"Hello, World!";
たったの一行。シンプルでしょ?ここで"##"はリテラル文字列に働く単項演算子のような役割をします。この演算子がつくと実行時にこのリテラル文字列"Hello, World!"をキーとして適当な翻訳を探します。お気づきの方もいると思いますが、PropertyResourceBundleはホワイトスペース' 'をデリミタとして解釈するため、実は"Hello, World!"はキーとはなり得ないんです。そこで提案したいのがJavaFX Script用の新しいリソースバンドルのフォーマット。このフォーマットは単純に次のようなフォームを取ります。
<JavaFX string literal> = <JavaFX string literal>
e.g.,
"Hello, World!" = "こんにちは、世界!"
さらにボーナスとして考えているのは、このFX propertiesのデフォルトのエンコーディングを"UTF-8"にしようというもの。これで今まで頭を痛めてきた"ISO-8859-1" & native2asciiの呪縛から解放されるでしょう。他のエンコーディングを指定したい場合は、ファイルの頭にCSS形式のエンコーディング宣言(@charset "<IANA defined charset name>";)を入れてあげればそれに従うようにします。 ただこの方式には問題が一つあり、スクリプト内に同じリテラル文字列があり、それぞれ別の翻訳をさせたい場合(例えば2つの"File"というリテラル文字列をそれぞれ名詞、動詞として翻訳したい場合)に困ってしまいます。この問題を解決するためにオプションとしてキーを明示的に指定できるようにしました。#[FILE_NOUN]"File"のように、大括弧でくくって明示的なキーを指定します。 まあこんな所ですが、プロポーザル(英語です)とプロトタイプ実装をPlanet JFX wikiに置いてみたんで、どうぞおためしください。それとコメント、感想もよろしく! (2008-01-10 15:55:55.0) Permalink Comments [3]Easy localization in JavaFX Script I've been recently working on the internationalization of the JavaFX Script, which is a scripting language based on Java. Although all the Java internationalization features are definitely available from JavaFX Script program as it runs on top of a Java runtime, it would not be very scripting language like, and could be error prone because of the complexity of mixing Java and JavaFX. One of the features that I would like to have in the JavaFX Script is an easy way to localize strings in the JavaFX source. In Java, it's like:
import java.util.ResourceBundle;
static final String GREETINGS_KEY = "HELLO_WORLD";
String greetings;
try {
ResourceBundle rb = ResourceBundle.getBundle("foo.bar.resources.My_Resource");
greetings = rb.getString(GREETINGS_KEY);
} catch (MissingResourceException mre) {
greetings = "Hello, World!";
}
Instead, we are thinking of a new easier way to localize strings in the JavaFX Script. It would be pretty much like GNU's gettext() function where the key itself becomes the default translation. So the above code would be written in JavaFX Script like:
var greetings = ##"Hello, World!";
Yep, just one line. How simple is that! Here "##" works like a unary operator to the string literal, in which it looks for the proper localized string with the key of "Hello, World!". Some of you might notice that PropertyResourceBundle recognizes a white space ' ' as the delimiter, so "Hello, World!" cannot be a key in it. You are correct. So what we would like to provide is a new resource bundle format for the JavaFX script. It simply accepts the form of property as
<JavaFX string literal> = <JavaFX string literal>
e.g.,
"Hello, World!" = "こんにちは、世界!"
In addition as a bonus, this FX properties file's default encoding is "UTF-8" (yay!) and yet also accepts the CSS style encoding declaration (@charset "<IANA defined charset name>";) There could possibly be an issue of ambiguous meaning for short words. For example, "File" could both mean "to file" and "file (as a noun)". To address this situation, you can optionally insert an explicit key which will be used for resource look up. This optional key can be specified within a pair of square brackets, e.g., ##[FILE_NOUN]"File". I wrote a proposal and prototype implementation at Planet JFX wiki, so you might want to try it out. Comments are welcome as I'd like to know what you think! (2008-01-10 15:12:14.0) Permalink Comments [1]JDKのアプリケーションで通貨に関連したことを行おうとした場合、通常はjava.util.Currencyクラスを使います。このクラスを使うと、ある国で使われている通貨や、その通貨のローカライズされたシンボルがなんであるかを調べることが出来ます。このような情報を提供するためにJDKは内部にISO 4217標準に基づいた通貨のデータを持っています。ところがこのISO 4217のデータ、いろんな理由(たとえばデノミネーションとか)でしばしば管理機関によってアップデートがかかるんですが、JDKで持っている通貨のデータを常にこのISO 4217のデータに合わせておくことは通貨を扱うアプリケーションには非常に重要です。もし実際の通貨とJDKの通貨データに整合性がない場合、これらの金融アプリケーションのトランザクションに重大な問題が生じるからです。(前に書いたトルコのお菓子のエントリーが良い例(すいませんが英語です)かも) さて、現在のJDKの通貨サポートの問題点の一つに「通貨データが"lib/rt.jar"の中にクラスファイルとして存在している」というのがあります。なぜこれが問題か。というのもISO 4217のデータに変更があった場合(例えばスロベニアがユーロに移行したとか)、常にrt.jarをアップデートしなければなりません。この事実はたとえばお客さんが通貨データだけをアップデートしたい時でも、常にJDK全体をアップグレードしなければならない事を示しています。TimeZoneのデータも同様な位置づけにあるんですが、TimeZoneのデータは既に隔離されていて、個別にアップデート可能になっています。 で、このブログエントリー、何を言いたいかというとJDK7ではこの問題はもう起こらなくなります。というのも必要な変更のチェックインをつい最近完了したんで、もうしばらくするとOpenJDK経由でアクセスできるようになるはずです。変更後では通貨データは"<JAVA_HOME>/lib"の下に"currency.data"というバイナリファイルとして個別に置かれます。なのでJDKアップデートリリースとは別のサイクルでこの通貨データをアップデートすることが可能になるはずです。さらにボーナス機能として、ユーザーがプロパティファイルでこの通貨データの内容を上書きすることも出来ます。"<JAVA_HOME>/lib/currency.properties"というファイルを作って、その中に"key=value"というペア("key"はISO 3166国コード、"value"は3文字通貨コード、3桁数値コード、それにマイナー・ユニットをカンマで区切った物)を書いておけば、"key"で指定した国の通貨のデータを上書きできます。たとえば"JP=JPZ,999,2"というエントリーは、日本の通貨データを上書きします。 この通貨の拡張に加えて、いくつかのAPIも実装しました。まず初めはJDKで定義されているすべてのCurrencyインスタンスを列挙する機能: public static Set<Currency> getAvailableCurrencies() それからISO 4217の数値通貨コードを取得する機能: public int getNumericCode() 最後にCurrencyインスタンスのディスプレイ用の名称取得機能。例えば日本の通貨は、getSymbol()すると「¥」ですが、このAPIは「日本円」を返します(ロケールが日本の場合)。 public String getDisplayName() public String getDisplayName(Locale displayLocale) このAPIの最初の実装は英語、フランス語、ドイツ語、イタリア語、日本語、韓国語、中国語(簡体字)、スペイン語、スウェーデン語、それに中国語(繁体字)の10のロケール用にローカライズされた通貨のディスプレイ用名称を返すことが出来ます。 さらにこれに応じてjava.util.spi.CurrencyNameProviderにも新たなSPIを追加しました。上記10言語以外に対応したい場合に、このSPIを実装することでどんな言語にもローカライズ可能です。
以上が今回実装した拡張の概要です。これで開発者の皆さんの苦労が少しでも減ると良いんですけどね。今回実はデモも一緒に載せたかったんですが、さすがにまだ出回ってないビルド用のデモは載せられないんでパスです。あしからず。 (2007-06-28 15:55:11.0) PermalinkIn the JDK, applications can use java.util.Currency class to deal with currencies. They can query what currency is used in a given country, or what localized symbol is used for that currency in a particular locale. To provide applications with those currency information, the JDK contains the currency data that is based on the ISO 4217 standard. From time to time, due to a variety of reasons, the ISO 4217 data are often updated by the maintenance agency. Keeping up with these updates in the JDK is critical for applications that deal with currencies, such as a banking application. Otherwise, those applications would result in a wrong financial transaction (cf. see my old entry regarding the Turkish pastry story here) Now, one of the issues in the current JDK's currency support is that the currency data is embedded in "lib/rt.jar" as a class file. So every time the ISO 4217 maintenance agency releases a currency update, such as Slovenia switching to Euro currency, the rt.jar file needs to be updated. This makes it impossible for customers to replace just the currency data portion when needed, without upgrading the whole JDK. This issue is analogous to the TimeZone data being separated from the JDK class files, and they can be upgraded separately. The purpose of this blog entry is to announce that this will no longer be an issue in the JDK7, as I have just checked-in the necessary changes for this issue (woohoo!), which will soon be public in the OpenJDK build. In the new structure, the currency data is separately placed in "<JAVA_HOME>/lib" directory as a binary file "currency.data", which can be replaced not in sync with the JDK update release schedule. One added feature to this is that you could even provide your own properties file to override the contents in the currency data file. If you create a properties file as "<JAVA_HOME>/lib/currency.properties", which contains "key=value" pairs where the "key" is a ISO 3166 country code, and the "value" consists of three-letter currency code, three-digit numeric code, and the minor unit (separeted by a comma), it would override the currency data for that country designated by the "key". For example if it has "JP=JPZ,999,2", this would override the Japanese currency data. Also, along with this Currency enhancement, I added a couple of new APIs. First one is to acquire all the available currencies in the JDK as follows: public static Set<Currency> getAvailableCurrencies() Next one is to return a numeric ISO 4217 code for a Currency instance: public int getNumericCode() The last one is to return the display name for a Currency instance. Instead of getSymbol() returning "$" for the US Dollar ("USD"), this API returns "US Dollar" in the "US" locale. public String getDisplayName() public String getDisplayName(Locale displayLocale) The initial release of this API contains localized currency names for 10 locales, i.e., English, French, German, Italian, Japanese, Korean, Simplified Chinese, Spanish, Swedish, and Traditional Chinese. I also added an SPI in java.util.spi.CurrencyNameProvider for the languages other than the above, so that one could provide localized display names in any language:
These are the changes that I have checked in, and I hope this is useful for developers. I would have liked to provide a working demo, but it's not possible to do it with a yet released build :-) (2007-06-28 14:55:06.0) Permalink Comments [15]Ubuntuの新しいリリース、7.04 「Feisty Fawn」にJava 6が入っていると聞いていたんで、国際化関係はどうなっているのか興味がありました。ということで早速手持ちのEdgy EftをFeisty Fawnにアップグレード。そして下のエントリーにもあるLocale Demoを走らせてみました。
お見事! 日中韓のフォントはそのまま表示されるようです! Ubuntu開発者の皆様ありがとうございます。ところでこのLocale Demoですがちょっと変更を加えてみました。来月開催されるJavaOneのデモ用にぱっと見でJDKに含まれているロケールか、あるいはCLDRロケールかがわかるように先頭に小さいアイコンを付けてみました。今年のJavaOneのパビリオンに設置するデモマシンにインストールする予定ですので、よかったらお立ちよりください。 (2007-04-22 21:16:19.0) PermalinkI know that Java 6 is included in the recently released Ubuntu 7.04 (Feisty Fawn), and am curious how they modify Sun's JDK6 in terms of internationalization. So I upgraded my Ubuntu from Edgy Eft to Feisty Fawn, grabbed the sun-java-6 package, and ran my locale demo...
Voila! CJK fonts seem to be supported out-of-the-box! Kudos to Ubuntu developers! BTW, I slightly modified the demo to display a small icon in front of each installed locale, so that it can identify itself as JDK bundled one or CLDR one. I am preparing to include this demo in one of the machines at this year's JavaOne Pavilion. (2007-04-22 21:02:53.0) Permalink今現在はテクニカル・ライターとして活躍しているJohn O'Conner、以前一緒にJavaの国際化チームで働いていたチーム・メイトでしたが、このたびJDK6での新しい国際化機能についての記事を書いてくれました。その記事はつい最近ここにアップロードされましたが、いろいろなサンプルとともに国際化の新機能を詳しく・わかりやすく紹介しています。 (2007-03-21 13:33:31.0) PermalinkNew article about the new internationalization features in JDK 6 John O'Conner, who used to work with me, wrote a really nice article about the new internationalization features in JDK 6. The article is just released here, which includes lots of examples demonstrating the new features. Kudos to John! (2007-03-21 13:25:04.0) Permalink”百聞は一見にしかず”ということわざがありますが、前のエントリーで取り上げたロケール・デモをWebStartアプリケーションにして、実行可能にしてみました。 このデモはJava 6の機能を使うのでJava 6がマシンにインストールされていることが前提条件です。もしまだの場合はこの際ここからインストールしましょう。Java 6のインストールが完了したら下のボタンを押してみて下さい。 デモが立ち上がったでしょうか?この状態だとこのデモに表示されているロケールのデータはJava 6に同梱されているそのままです。"Locale Names"のタブへ移動すると実際にいくつのロケールがインストールされているかを見ることが出来ます。全部で"150"と表示されるはずです。 次に例のCLDRアダプターをインストールしてみましょう。CLDRアダプターはここにあるので、この"cldradapter.jar"ファイルをJavaランタイムの拡張ディレクトリに保存します。ウィンドウズですと通常、"C:\Program Files\Java\jre1.6.0\lib\ext"というディレクトリがあるはずですのでここに保存します。そして再び上の"Launch"ボタンを押してみましょう。どうですか?今度は少し立ち上がりに時間がかかったと思いますが、これはロケールデータをユニコード・コンソーシアムのウェブサイトから動的にダウンロードしているためです。先ほどのように"Locale Names"のタブに移動すると、"365"個のロケールが表示されていると思います。 このデモの面白いところは、ロケールデータを"オン・デマンド”で引っ張ってきてくれるところです。Javaランタイムにもともと組み込まれていなくても、必要になった時に初めてインストールすることによって、ダウンロードの最適化及び拡張性を同時に得ることが出来ます。次期Javaのリリース(えっと、Java 6の次の話をしています :-)のプランの一つにロケールデータを如何に効率よくデプロイするかというフィーチャーがありますが、このデモはそれに対する一つの方向性を示していると思います。 (2006-11-14 19:19:55.0) PermalinkLocale Demo on your own machine "Seeing is believing", so I made the locale demo webstartable. First, please check that your machine has Java 6 installed, if not, you can install it from here. Then just hit the following button: The demo started? At this moment, it only displays locales that are provided by the Java runtime only. If you go to the "Locale Names" tab, it shows the total number of locales as "150". Now the fun part begins. Download the CLDR Adapter into the extension directory of your Java runtime. For example on Windows, the path to the extension directory is typically "C:\Program Files\Java\jre1.6.0\lib\ext". Then hit the above "Launch" button again. It would take a bit longer time to start, because this time the CLDR adapter retrieves the locale data from the Unicode Consortium's web site on the fly. Take a look at the "Locale Names" tab again and you will see the locale count jumps to "365"! This demo also showcases an interesting aspect, i.e., "locale on demand". Locales can be installed into the runtime only when it is really needed. One of the features that we are planning to do in the next release, um, that means after Java 6 :-), is how we can deploy locale data to the clients, and this demonstrates an interesting point. (2006-11-14 18:56:57.0) Permalink Comments [2]来週ワシントンD.C.で開催されるInternationalization and Unicode Conferenceで近々公開予定のJava 6の国際化機能についてプレゼンテーションをしに行ってきます。そこで使うデモを現在準備中なんですが、その中の一つに前のエントリーで紹介したLocale Sensitive Services SPI(長い名前だ)という機能の紹介をするデモがあり、ここでちょっと紹介したいと思います。 このデモは2つのコンポーネントから出来ていて、そのうちの一つは通常のSwingアプリケーションです。Locale.getAvailableLocales()で得られるロケールのリストを表示し、ユーザーがその中からロケールを選ぶと、そのロケールに従ってロケール依存のデータ(例えば日付・時刻、通貨、及びローカライズされた名前など)をフォーマットして表示します。 もう一つのコンポーネントは目には見えないですが、このデモの核心部分となる物です。Unicode Consortiumが公開しているCLDRというXMLで記述されたロケールデータがあるのですが、このコンポーネントはCLDRのデータをLocale Sensitive Services SPIを使ってJavaランタイムに組み込む働きをします。現時点ではJavaはISO 639-2で定められた3文字の言語コードをサポートしていないので、それらの言語コードを使っているロケールはプラグインすることはできませんが、それでも300以上のロケールがこのCLDRアダプターによってJavaのテキストプロセッシングクラスで利用可能になるんです! 下のイメージがこのデモのプレビューですが、ここではJavaランタイム単体では未だサポートされていないインドのタミール語ロケールでフォーマットされた日付・時刻が表示できています。 タミール語はJavaでは未サポートなので、表示に必要なフォントをどこかから持ってくる必要があります。このデモはWindowsXP上で動いているので、XPのタミール語フォントである"latha.ttf"をJava5.0でサポートされた"フォントフォールバック機能"を使ってJavaに組み込む(といってもコピーするだけ:-)だけで、まるで魔法のようCLDRのロケールデータが表示できてしまうんです。 下のスクリーンショットはアムハラ語(エチオピア)ロケールでのデモです。上と同じように日付・時刻がアムハラ語でフォーマットされて表示されています。 このデモではタミール語のようにWindows XPのフォントを持ってくることができない(Windows XPでも未サポートのため)ので、アムハラ語のオープンソースのTrueTypeフォントである"Jiret"というフォント(ここでダウンロード可能)を使いました。 もし来週開催のUnicode Conferenceに来られる方がいらっしゃいましたら是非声をかけてください。いろんな言語でのデモをお見せできると思います。 (2006-11-08 16:14:01.0) PermalinkMore than 300 locales in Java! I am going to give a presentation at the upcoming Internationalization and Unicode Conference held in Washington D.C. next month. The main topic of the presentation is the new i18n features in the upcoming Java 6, and now I am preparing a demo to showcase the potential of the Locale Sensitive Services SPI feature (I know. Kinda long name), which I wrote in the previous entry (wow, it was 8 months ago :-) The demo consists of two components. One is just a simple Swing application which displays a list of available locales in the platform (precisely, a list returned from Locale.getAvailableLocales()), and displays formatted dates, times, numbers, currencies, or language/country/timezone names in a particular locale which is selected in the locale list. The other component is invisible, but more interesting one. That is, an adapter which reads the CLDR's locale data in XML format, then it transforms the locale data and plugs them into the Java runtime through the Locale Sensitive Services SPI. Although Java currently do not support three letter language codes defined in ISO 639-2 so some locales may not be plugged in, but still we have more than 300 locales available in the Java text processing classes with this CLDR adapter! Here is a preview of the demo. This demonstrates the date and time formatting in the Tamil (India) locale, which the Java runtime does not yet support out of the box: Note that since Java does not support Tamil, it does not contain the suitable font either. However, the OS I used for running this demo was Windows XP, which contains fonts that are capable for displaying Tamil such as "Latha" font. And with the "font fallback" mechanism we introduced in Java 5.0, the demo worked like a charm with the formatted date and time, which are derived from the CLDR data. Here is another screen shot of the demo, which displays date and time in Amharic (Ethiopia) locale. For this demo, since Windows XP does not support Amharic, I used an open source TrueType font for Amharic called "Jiret", which is available here. If you are interested, and planning to be at the next Unicode conference, please come to our presentation. I am sure I can show you more interesting bits! (2006-10-30 12:46:24.0) Permalink Comments [5]About the Locale Sensitive Services SPI in Mustang Beta Now that the Mustang (a.k.a. Java SE 6) beta is out (yay!), I think it is about time to describe one of the new i18n features so that you can ride the Mustang beta with pleasure. The feature I want to mention here has the name "Locale Sensitive Services SPI". Sounds complicated? No, it's not! OK, let me introduce it to you. With this Mustang Beta release, we support more than 100 locales in java.text and java.util packages. Although this covers lots of areas in this Globe, there are areas that are yet to be supported by the Java runtime, and some customers do want them supported. Believe it or not, supporting a locale and its data requires a lot of investigation, such as the most popular date format or translation of a country name in that language. It's getting more difficult for smaller countries/regions. Sometimes even political ramifications come in. To resolve such a difficulty, there is an interesting project going on, which is called "Common Locale Data Repository (CLDR)" at the Unicode Consortium and standardizes the commonly used locale data. However, we cannot include all of the defined data into the Java runtime as it would bloat the Java runtime size (We introduced a couple of new locales in Mustang from the CLDR, though). So, the idea we came up with was to open up the interface with which the developers can plug in their own locale data and related services. I guess that the question in your mind now is, how do I use them, right? To provide locale data/services, you first need to decide which functionality you want to provide for your own locale. With Mustang, you can provide the following functionalities by your locale data implementation.
Once you decide which functionality you want to provide with your locale, then you will need to implement the corresponding SPI (stands for the "Service Provider Interface"), which resides either in java.text.spi or java.util.spi packages. Let's say you want to provide a DateFormat object for a new locale, then you need to implement java.text.spi.DateFormatProvider. Since java.text.spi.DateFormatProvider is an abstract class, you will need to extend it and implement the following 4 methods.
You'll notice that "getAvailableLocales()" is actually derived from the parent class, i.e., LocaleServiceProvider, so all the SPI providers need to implement it so that the providers can "declare" which locales they claim "supported". And you will also notice that the other three methods are mirrored factory methods from the corresponding API class, i.e., java.text.DateFormat class. This means that the object which your implementation returns is passed down to the application as it is. After implementing those required methods, then you need to package your implementations, so that you can deploy it with the Java runtime. Since the Locale Sensitive Services SPIs are based on the standard Java Extension Mechanism, you can package them as a JAR file (with a few tricks in its MANIFEST file, which can be found in here) and place it in the extension directory. That's it folks. If an application requests a DateFormat object for your locale, your providers object is now created and used. Happy locale-adding! (2006-02-15 09:07:17.0) Permalink |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||