1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-04-10 16:13:33 +02:00

Implement string bundle cascading.

This commit is contained in:
Kris Maglione
2011-03-07 22:24:55 -05:00
parent cd4c0a7722
commit b5447ae736

View File

@@ -15,10 +15,16 @@ defineModule("messages", {
// TODO: Lazy instantiation // TODO: Lazy instantiation
var Messages = Module("messages", { var Messages = Module("messages", {
init: function init() { init: function init(name) {
let self = this; let self = this;
name = name || "messages";
this.bundle = services.stringBundle.createBundle(JSMLoader.getTarget("dactyl://locale/messages.properties")); this.bundles = array.uniq([JSMLoader.getTarget("dactyl://locale/" + name + ".properties"),
JSMLoader.getTarget("dactyl://locale-local/" + name + ".properties"),
"resource://dactyl-locale/en-US/" + name + ".properties",
"resource://dactyl-locale-local/en-US/" + name + ".properties"])
.map(services.stringBundle.createBundle)
.filter(function (bundle) { try { bundle.getSimpleEnumeration(); return true; } catch (e) { return false; } });
this._ = Class("_", String, { this._ = Class("_", String, {
init: function _(message) { init: function _(message) {
@@ -46,7 +52,7 @@ var Messages = Module("messages", {
} }
}, },
iterate: function () let (bundle = this.bundle) iterate: function () let (bundle = this.bundles[0])
iter(prop.QueryInterface(Ci.nsIPropertyElement) for (prop in iter(bundle.getSimpleEnumeration()))), iter(prop.QueryInterface(Ci.nsIPropertyElement) for (prop in iter(bundle.getSimpleEnumeration()))),
cleanup: function cleanup() { cleanup: function cleanup() {
@@ -54,28 +60,30 @@ var Messages = Module("messages", {
}, },
get: function get(value, default_) { get: function get(value, default_) {
for (let bundle in values(this.bundles))
try { try {
return this.bundle.GetStringFromName(value); return bundle.GetStringFromName(value);
} }
catch (e) { catch (e) {}
// Report error so tests fail, but don't throw // Report error so tests fail, but don't throw
if (arguments.length < 2) if (arguments.length < 2)
util.reportError(Error("Invalid locale string: " + value + ": " + e)); util.reportError(Error("Invalid locale string: " + value));
return arguments.length > 1 ? default_ : value; return arguments.length > 1 ? default_ : value;
}
}, },
format: function format(value, args, default_) { format: function format(value, args, default_) {
for (let bundle in values(this.bundles))
try { try {
return this.bundle.formatStringFromName(value, args, args.length); return bundle.formatStringFromName(value, args, args.length);
} }
catch (e) { catch (e) {}
// Report error so tests fail, but don't throw // Report error so tests fail, but don't throw
if (arguments.length < 3) if (arguments.length < 3)
util.reportError(Error("Invalid locale string: " + value + ": " + e)); util.reportError(Error("Invalid locale string: " + value));
return arguments.length > 2 ? default_ : value; return arguments.length > 2 ? default_ : value;
} }
}
}, { }, {
Localized: Class("Localized", Class.Property, { Localized: Class("Localized", Class.Property, {