XULのローカライズ

localeフォルダにsample.dtdファイルを作成し、実体宣言を行う。
<!ENTITY sample.label "サンプル">
<!ENTITY sample.key   "S">
contentフォルダのsample.xulに、dtdファイルの読み込み宣言と実体参照を記述する。
<!DOCTYPE window SYSTEM "chrome://sample/locale/sample.dtd">
<menuitem label="&sample.label;" accesskey="&sample.key;"/>
実体が無いと例外エラーを吐くので注意

JavaScriptのローカライズ

まず、localeフォルダにsample.propertiesファイルを用意する。
SAMPLE = サンプル
SAMPLE_REPLACE_1 = 数字%Sと数字%S
SAMPLE_REPLACE_2 = 数字%2$Sと数字%1$S
で、この後だが、XUL、JSM、XPCOM の3つの方法がある。

XULを仲介する

XULに以下のように記述。
<stringbundleset id="stringbundleset">
  <stringbundle id="strings_sample" src="chrome://sample/locale/sample.properties"/>
</stringbundleset>
あとはJavaScript内で以下のように使える。
var strings = document.getElementById("strings_sample");
var text1 = strings.getString("SAMPLE");  // → サンプル
var num1 = 5;
var num2 = 10;
var text2 = strings.getFormattedString("SAMPLE_REPLACE_1", [num1, num2]);  // → 数字5と数字10
var text3 = strings.getFormattedString("SAMPLE_REPLACE_2", [num1, num2]);  // → 数字10と数字5
ローカライズ文字列を取得できないと例外エラーを吐くので注意

JavaScriptコードモジュールを使用

sample.propertiesを用意した後は以下のように使う。
Components.utils.import("resource://gre/modules/services-common/stringbundle.js");
var strings = new StringBundle("chrome://sample/locale/sample.properties");
var text1 = strings.get("SAMPLE");  // → サンプル
var num1 = 5;
var num2 = 10;
var text2 = strings.get("SAMPLE_REPLACE_1", [num1, num2]);  // → 数字5と数字10
var text3 = strings.get("SAMPLE_REPLACE_2", [num1, num2]);  // → 数字10と数字5
XULを仲介するより楽な気がする…
getStringやgetFormattedStringも使える。

XPCOMを直接使う

Components.utils.import("resource://gre/modules/Services.jsm");
var strings = Services.strings.createBundle("chrome://sample/locale/sample.properties");
var text1 = strings.GetStringFromName("SAMPLE");  // → サンプル

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2014年01月23日 14:43