1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
// Copyright (c) 2011 by Kris Maglione <maglione.k@gmail.com>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";
try {
Components.utils.import("resource://dactyl/bootstrap.jsm");
defineModule("messages", {
exports: ["Messages", "messages", "_"],
require: ["services", "util"]
}, this);
// TODO: Lazy instantiation
var Messages = Module("messages", {
init: function init(name) {
let self = this;
name = name || "messages";
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, {
init: function _(message) {
this.args = arguments;
},
message: Class.memoize(function () {
let message = this.args[0];
if (this.args.length > 1) {
let args = Array.slice(this.args, 1);
return self.format(message + "-" + args.length, args, null) || self.format(message, args);
}
return self.get(message);
}),
valueOf: function valueOf() this.message,
toString: function toString() this.message
});
let seen = {};
for (let { key } in this.iterate()) {
if (!Set.add(seen, key))
this._[key] = this[key] = {
__noSuchMethod__: function __(prop, args) self._.apply(self, [prop].concat(args))
};
}
},
iterate: function () let (bundle = this.bundles[0])
iter(prop.QueryInterface(Ci.nsIPropertyElement) for (prop in iter(bundle.getSimpleEnumeration()))),
cleanup: function cleanup() {
services.stringBundle.flushBundles();
},
get: function get(value, default_) {
for (let bundle in values(this.bundles))
try {
return bundle.GetStringFromName(value);
}
catch (e) {}
// Report error so tests fail, but don't throw
if (arguments.length < 2) // Do *not* localize these strings
util.reportError(Error("Invalid locale string: " + value));
return arguments.length > 1 ? default_ : value;
},
format: function format(value, args, default_) {
for (let bundle in values(this.bundles))
try {
return bundle.formatStringFromName(value, args, args.length);
}
catch (e) {}
// Report error so tests fail, but don't throw
if (arguments.length < 3) // Do *not* localize these strings
util.reportError(Error("Invalid locale string: " + value));
return arguments.length > 2 ? default_ : value;
}
}, {
Localized: Class("Localized", Class.Property, {
init: function init(prop, obj) {
let _prop = "unlocalized_" + prop;
if (this.initialized) {
/*
if (config.locale === "en-US")
return { configurable: true, enumerable: true, value: this.default, writable: true };
*/
if (!Set.has(obj, "localizedProperties"))
obj.localizedProperties = { __proto__: obj.localizedProperties };
obj.localizedProperties[prop] = true;
obj[_prop] = this.default;
return {
get: function get() {
let self = this;
let value = this[_prop];
function getter(key, default_) function getter() messages.get([name, key].join("."), default_);
if (value != null) {
let name = [this.constructor.className.toLowerCase(), this.identifier || this.name, prop].join(".");
if (!isObject(value))
value = messages.get(name, value);
else if (isArray(value))
// Deprecated
iter(value).forEach(function ([k, v]) {
if (isArray(v))
memoize(v, 1, getter(v[0], v[1]));
else
memoize(value, k, getter(k, v));
});
else
iter(value).forEach(function ([k, v]) {
memoize(value, k, function () messages.get([name, k].join("."), v));
});
}
return Class.replaceProperty(this, prop, value);
},
set: function set(val) this[_prop] = val
};
}
this.default = prop;
this.initialized = true;
}
})
}, {
javascript: function initJavascript(dactyl, modules, window) {
modules.JavaScript.setCompleter([this._, this.get, this.format], [
function (context) {
context.keys = { text: "key", description: "value" };
return messages.iterate();
}
]);
}
});
var { _ } = messages;
endModule();
} catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
|