「ローカライズ」の編集履歴(バックアップ)一覧はこちら
「ローカライズ」(2014/01/23 (木) 14:43:41) の最新版変更点
追加された行は緑色になります。
削除された行は赤色になります。
#contents()
*XULのローカライズ
localeフォルダにsample.dtdファイルを作成し、実体宣言を行う。
#highlight(xml){
<!ENTITY sample.label "サンプル">
<!ENTITY sample.key "S">}
contentフォルダのsample.xulに、dtdファイルの読み込み宣言と実体参照を記述する。
#highlight(xml){
<!DOCTYPE window SYSTEM "chrome://sample/locale/sample.dtd">}
#highlight(xml){
<menuitem label="&sample.label;" accesskey="&sample.key;"/>}
-[[Localization - XUL Tutorial | MDN>https://developer.mozilla.org/en-US/docs/XUL_Tutorial/Localization]]
実態が無いと例外エラーを吐くので注意
*JavaScriptのローカライズ
まず、localeフォルダにsample.propertiesファイルを用意する。
#highlight(){
SAMPLE = サンプル
SAMPLE_REPLACE_1 = 数字%Sと数字%S
SAMPLE_REPLACE_2 = 数字%2$Sと数字%1$S}
で、この後だが、XUL、JSM、XPCOM の3つの方法がある。
**XULを仲介する
XULに以下のように記述。
#highlight(xml){
<stringbundleset id="stringbundleset">
<stringbundle id="strings_sample" src="chrome://sample/locale/sample.properties"/>
</stringbundleset>}
あとはJavaScript内で以下のように使える。
#highlight(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}
ローカライズ文字列を取得できないと例外エラーを吐くので注意
-[[Property Files - XUL Tutorial | MDN>https://developer.mozilla.org/en-US/docs/XUL_Tutorial/Property_Files]]
-[[stringbundle - XUL | MDN>https://developer.mozilla.org/en-US/docs/XUL/stringbundle]]
**JavaScriptコードモジュールを使用
sample.propertiesを用意した後は以下のように使う。
#highlight(javascript){
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も使える。
-[[Labs/JS Modules - MozillaWiki>https://wiki.mozilla.org/Labs/JS_Modules#StringBundle]]
**XPCOMを直接使う
#highlight(javascript){
Components.utils.import("resource://gre/modules/Services.jsm");
var strings = Services.strings.createBundle("chrome://sample/locale/sample.properties");
var text1 = strings.GetStringFromName("SAMPLE"); // → サンプル}
-[[nsIStringBundleService - XPCOM Interface Reference | MDN>https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIStringBundleService]]
-[[nsIStringBundle - XPCOM Interface Reference | MDN>https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIStringBundle]]
#contents()
*XULのローカライズ
localeフォルダにsample.dtdファイルを作成し、実体宣言を行う。
#highlight(xml){
<!ENTITY sample.label "サンプル">
<!ENTITY sample.key "S">}
contentフォルダのsample.xulに、dtdファイルの読み込み宣言と実体参照を記述する。
#highlight(xml){
<!DOCTYPE window SYSTEM "chrome://sample/locale/sample.dtd">}
#highlight(xml){
<menuitem label="&sample.label;" accesskey="&sample.key;"/>}
-[[Localization - XUL Tutorial | MDN>https://developer.mozilla.org/en-US/docs/XUL_Tutorial/Localization]]
実体が無いと例外エラーを吐くので注意
*JavaScriptのローカライズ
まず、localeフォルダにsample.propertiesファイルを用意する。
#highlight(){
SAMPLE = サンプル
SAMPLE_REPLACE_1 = 数字%Sと数字%S
SAMPLE_REPLACE_2 = 数字%2$Sと数字%1$S}
で、この後だが、XUL、JSM、XPCOM の3つの方法がある。
**XULを仲介する
XULに以下のように記述。
#highlight(xml){
<stringbundleset id="stringbundleset">
<stringbundle id="strings_sample" src="chrome://sample/locale/sample.properties"/>
</stringbundleset>}
あとはJavaScript内で以下のように使える。
#highlight(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}
ローカライズ文字列を取得できないと例外エラーを吐くので注意
-[[Property Files - XUL Tutorial | MDN>https://developer.mozilla.org/en-US/docs/XUL_Tutorial/Property_Files]]
-[[stringbundle - XUL | MDN>https://developer.mozilla.org/en-US/docs/XUL/stringbundle]]
**JavaScriptコードモジュールを使用
sample.propertiesを用意した後は以下のように使う。
#highlight(javascript){
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も使える。
-[[Labs/JS Modules - MozillaWiki>https://wiki.mozilla.org/Labs/JS_Modules#StringBundle]]
**XPCOMを直接使う
#highlight(javascript){
Components.utils.import("resource://gre/modules/Services.jsm");
var strings = Services.strings.createBundle("chrome://sample/locale/sample.properties");
var text1 = strings.GetStringFromName("SAMPLE"); // → サンプル}
-[[nsIStringBundleService - XPCOM Interface Reference | MDN>https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIStringBundleService]]
-[[nsIStringBundle - XPCOM Interface Reference | MDN>https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIStringBundle]]