Firefox拡張入門第8回(ローカライズ)

XULローカライズにはdtdファイルを使い、Javascriptローカライズにはpropertiesファイルを使います。

今回は、ローカライズした"hello,world."ボタン、つまり、"こんにちは、世界。"ボタンを作ってみましょう。
マッチFOXは"Strings.jsm"ライブラリをご用意していますので、簡単にpropertiesファイルを扱えます。
以下のファイルたちに手を加えてください。

<button oncommand="Sample.hello();" label="&Sample.sidebar.hello;"/>
const EXPORT = ["hello"];

function hello() {
  var strings = new Strings("chrome://sample/locale/sidebar.properties");
  alert(strings.get("hello"));
}
<!ENTITY Sample.sidebar.hello "Don't push me! Don't you dare push me!!">
<!ENTITY Sample.sidebar.hello "押すなよ!絶対に押すなよ!!">
hello = hello,world.
hello = こんにちは、世界。

完成です。
http://eva-lu-ator.net/~gemma/geocities/matchfox/stringsja.png

英語版Firefoxを起動して確認してみましょう。

firefox -UILocale "en-US" -no-remote

http://eva-lu-ator.net/~gemma/geocities/matchfox/strings.png

すばらしい。

Strings.jsm リファレンスマニュアル

function: Strings aPropertiesFile
新しいインスタンスを作ります。
aPropertiesFile StringBundleに使うpropertiesファイルのURI。文字列。

function: get aName &optional aArgs
ローカライズ文字列を取得します。
aName 項目名。文字列。
aArgs printfのような文字列フォーマットに渡す引数。文字列の配列。