fune/toolkit/content/widgets/stringbundle.js
Brian Grinstead 87f933ff63 Bug 1459245 - Migrate <stringbundle> from a XBL binding to a Custom Element;r=mossop
This loads a JS file to register the Custom Element whenever XUL documents
get created. This is a pattern we plan to follow for future elements as well -
stringbundle is just an easy target to prove it out with.

MozReview-Commit-ID: 7PlisCknrKW

--HG--
rename : toolkit/content/widgets/stringbundle.xml => toolkit/content/widgets/stringbundle.js
extra : rebase_source : 5bf94038556c9b956de3678cd4bc817f5d837f45
2017-10-25 13:51:13 -07:00

63 lines
1.6 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
// This is loaded into all XUL windows. Wrap in a block to prevent
// leaking to window scope.
{
ChromeUtils.import("resource://gre/modules/Services.jsm");
class MozStringbundle extends XULElement {
get stringBundle() {
if (!this._bundle) {
try {
this._bundle = Services.strings.createBundle(this.src);
} catch (e) {
dump("Failed to get stringbundle:\n");
dump(e + "\n");
}
}
return this._bundle;
}
set src(val) {
this._bundle = null;
this.setAttribute("src", val);
return val;
}
get src() {
return this.getAttribute("src");
}
get strings() {
// Note: this is a sucky method name! Should be:
// readonly attribute nsISimpleEnumerator strings;
return this.stringBundle.getSimpleEnumeration();
}
getString(aStringKey) {
try {
return this.stringBundle.GetStringFromName(aStringKey);
} catch (e) {
dump("*** Failed to get string " + aStringKey + " in bundle: " + this.src + "\n");
throw e;
}
}
getFormattedString(aStringKey, aStringsArray) {
try {
return this.stringBundle.formatStringFromName(aStringKey, aStringsArray, aStringsArray.length);
} catch (e) {
dump("*** Failed to format string " + aStringKey + " in bundle: " + this.src + "\n");
throw e;
}
}
}
customElements.define("stringbundle", MozStringbundle);
}