Files
roytam1 83a04cbfb3 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1203159 - Clean up various tests after DevTools resource move. r=me (80fea1c8f4)
- Bug 1203159 - Tweak environment test for appdir change. r=gfritzsche (bb60c915db)
- kill some more android (d4d899e640)
- Bug 1198352 - Handle cross-compartment WeakSets. r=Waldo (382327471b)
- Bug 1212390 - fix lingering bugs around oomAtAllocation. r=jonco (81b4e9d132)
- Bug 1214781 - Make oomTest() clear any previous OOM condition r=terrence (15e4ea435d)
- Bug 1208403 - Fix byteSizeOfScript shell function to check for scripted functions. r=jonco (c1d916fc6d)
- Bug 1219905 - Don't assume an exception is pending if the execution failed in oomTest() r=jandem (8fc0a260bb)
- Bug 1215814 - Small ThrowIfNotConstructing cleanup. r=efaust (51217af85f)
- Bug 1220610 - Fix the nsIDocument::GetDocumentURI and nsIDocument::GetOriginalURI comments to be correct and up to date. r=baku (5dc3d632e5)
- Bug 1212842 - part 1 - BroadcastChannel should not remove the document from bfcache when not used, r=smaug (55a70ef4f9)
- Bug 1212842 - part 2 - test for BroadcastChannel used when the document is bfcached, r=smaug (c3117e2bf8)
- var-let (9641a37391)
- Bug 1173074 - select-child.js needs to include XPCOMUtils, r=ehsan (3178041b77)
- var-let (a81ad136a5)
- cleanup (1036e060db)
- Bug 1163028 - stop escaping [ and ] in toFileURI r=yoric (9b883ea6a3)
- Bug 1218870 - Fix uses of typeof "foo" in OS.File. r=yoric (f78c0f73c7)
- Bug 1124472 - Add telemetry for the Saved Passwords dialog. r=dolske (b869c82d58)
- Bug 1197625 - delay sync on wake for 5 seconds in the hope the network is back up by then. r=rnewman (e20e63a8bc)
- Bug 643633 - Remove TTLs from form history records. r=markh,nalexander (7a47acaa79)
- const-var (e579cac720)
2022-12-23 21:51:31 +08:00

204 lines
5.5 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/. */
this.EXPORTED_SYMBOLS = ["StringBundle"];
var {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
/**
* A string bundle.
*
* This object presents two APIs: a deprecated one that is equivalent to the API
* for the stringbundle XBL binding, to make it easy to switch from that binding
* to this module, and a new one that is simpler and easier to use.
*
* The benefit of this module over the XBL binding is that it can also be used
* in JavaScript modules and components, not only in chrome JS.
*
* To use this module, import it, create a new instance of StringBundle,
* and then use the instance's |get| and |getAll| methods to retrieve strings
* (you can get both plain and formatted strings with |get|):
*
* let strings =
* new StringBundle("chrome://example/locale/strings.properties");
* let foo = strings.get("foo");
* let barFormatted = strings.get("bar", [arg1, arg2]);
* for (let string of strings.getAll())
* dump (string.key + " = " + string.value + "\n");
*
* @param url {String}
* the URL of the string bundle
*/
this.StringBundle = function StringBundle(url) {
this.url = url;
}
StringBundle.prototype = {
/**
* the locale associated with the application
* @type nsILocale
* @private
*/
get _appLocale() {
try {
return Cc["@mozilla.org/intl/nslocaleservice;1"].
getService(Ci.nsILocaleService).
getApplicationLocale();
}
catch(ex) {
return null;
}
},
/**
* the wrapped nsIStringBundle
* @type nsIStringBundle
* @private
*/
get _stringBundle() {
let stringBundle = Cc["@mozilla.org/intl/stringbundle;1"].
getService(Ci.nsIStringBundleService).
createBundle(this.url, this._appLocale);
this.__defineGetter__("_stringBundle", () => stringBundle);
return this._stringBundle;
},
// the new API
/**
* the URL of the string bundle
* @type String
*/
_url: null,
get url() {
return this._url;
},
set url(newVal) {
this._url = newVal;
delete this._stringBundle;
},
/**
* Get a string from the bundle.
*
* @param key {String}
* the identifier of the string to get
* @param args {array} [optional]
* an array of arguments that replace occurrences of %S in the string
*
* @returns {String} the value of the string
*/
get: function(key, args) {
if (args)
return this.stringBundle.formatStringFromName(key, args, args.length);
else
return this.stringBundle.GetStringFromName(key);
},
/**
* Get all the strings in the bundle.
*
* @returns {Array}
* an array of objects with key and value properties
*/
getAll: function() {
let strings = [];
// FIXME: for performance, return an enumerable array that wraps the string
// bundle's nsISimpleEnumerator (does JavaScript already support this?).
let enumerator = this.stringBundle.getSimpleEnumeration();
while (enumerator.hasMoreElements()) {
// We could simply return the nsIPropertyElement objects, but I think
// it's better to return standard JS objects that behave as consumers
// expect JS objects to behave (f.e. you can modify them dynamically).
let string = enumerator.getNext().QueryInterface(Ci.nsIPropertyElement);
strings.push({ key: string.key, value: string.value });
}
return strings;
},
// the deprecated XBL binding-compatible API
/**
* the URL of the string bundle
* @deprecated because its name doesn't make sense outside of an XBL binding
* @type String
*/
get src() {
return this.url;
},
set src(newVal) {
this.url = newVal;
},
/**
* the locale associated with the application
* @deprecated because it has never been used outside the XBL binding itself,
* and consumers should obtain it directly from the locale service anyway.
* @type nsILocale
*/
get appLocale() {
return this._appLocale;
},
/**
* the wrapped nsIStringBundle
* @deprecated because this module should provide all necessary functionality
* @type nsIStringBundle
*
* If you do ever need to use this, let the authors of this module know why
* so they can surface functionality for your use case in the module itself
* and you don't have to access this underlying XPCOM component.
*/
get stringBundle() {
return this._stringBundle;
},
/**
* Get a string from the bundle.
* @deprecated use |get| instead
*
* @param key {String}
* the identifier of the string to get
*
* @returns {String}
* the value of the string
*/
getString: function(key) {
return this.get(key);
},
/**
* Get a formatted string from the bundle.
* @deprecated use |get| instead
*
* @param key {string}
* the identifier of the string to get
* @param args {array}
* an array of arguments that replace occurrences of %S in the string
*
* @returns {String}
* the formatted value of the string
*/
getFormattedString: function(key, args) {
return this.get(key, args);
},
/**
* Get an enumeration of the strings in the bundle.
* @deprecated use |getAll| instead
*
* @returns {nsISimpleEnumerator}
* a enumeration of the strings in the bundle
*/
get strings() {
return this.stringBundle.getSimpleEnumeration();
}
}