1 // Copyright (c) 2011 by Kris Maglione <maglione.k@gmail.com>
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
9 Components.utils.import("resource://dactyl/bootstrap.jsm");
10 defineModule("messages", {
11 exports: ["Messages", "messages", "_"],
12 require: ["services", "util"]
15 var Messages = Module("messages", {
17 init: function init(name) {
19 this.name = name || "messages";
21 this._ = Class("_", String, {
22 init: function _(message) {
23 this.args = arguments;
26 message: Class.Memoize(function () {
27 let message = this.args[0];
29 if (this.args.length > 1) {
30 let args = Array.slice(this.args, 1);
31 return self.format(message + "-" + args.length, args, null) || self.format(message, args);
33 return self.get(message);
35 valueOf: function valueOf() this.message,
36 toString: function toString() this.message
40 cleanup: function cleanup() {
41 services.stringBundle.flushBundles();
44 bundles: Class.Memoize(function ()
45 array.uniq([JSMLoader.getTarget("dactyl://locale/" + this.name + ".properties"),
46 JSMLoader.getTarget("dactyl://locale-local/" + this.name + ".properties"),
47 "resource://dactyl-locale/en-US/" + this.name + ".properties",
48 "resource://dactyl-locale-local/en-US/" + this.name + ".properties"],
50 .map(services.stringBundle.createBundle)
51 .filter(function (bundle) { try { bundle.getSimpleEnumeration(); return true; } catch (e) { return false; } })),
53 iterate: function () {
55 for (let bundle in values(this.bundles))
56 for (let { key, value } in iter(bundle.getSimpleEnumeration(), Ci.nsIPropertyElement))
57 if (!Set.add(seen, key))
61 get: function get(value, default_) {
62 for (let bundle in values(this.bundles))
64 let res = bundle.GetStringFromName(value);
65 if (res.slice(0, 2) == "+ ")
66 return res.slice(2).replace(/\s+/g, " ");
71 // Report error so tests fail, but don't throw
72 if (arguments.length < 2) // Do *not* localize these strings
73 util.reportError(Error("Invalid locale string: " + value));
74 return arguments.length > 1 ? default_ : value;
77 format: function format(value, args, default_) {
78 for (let bundle in values(this.bundles))
80 let res = bundle.formatStringFromName(value, args, args.length);
81 if (res.slice(0, 2) == "+ ")
82 return res.slice(2).replace(/\s+/g, " ");
87 // Report error so tests fail, but don't throw
88 if (arguments.length < 3) // Do *not* localize these strings
89 util.reportError(Error("Invalid locale string: " + value));
90 return arguments.length > 2 ? default_ : value;
94 * Exports known localizable strings to a properties file.
96 * @param {string|nsIFile} {file} The file to which to export
99 export: function export_(file) {
100 let { Buffer, commands, hints, io, mappings, modes, options, sanitizer } = overlay.activeModules;
101 file = io.File(file);
103 function properties(base, iter_, prop) iter(function _properties() {
104 function key() [base, obj.identifier || obj.name].concat(Array.slice(arguments)).join(".").replace(/[\\:=]/g, "\\$&");
106 prop = prop || "description";
107 for (var obj in iter_) {
108 if (!obj.hive || obj.hive.name !== "user") {
109 yield key(prop) + " = " + obj[prop];
112 for (let [k, v] in isArray(obj.values) ? array.iterValues(obj.values) : iter(obj.values))
113 yield key("values", k) + " = " + v;
115 for (let opt in values(obj.options))
116 yield key("options", opt.names[0]) + " = " + opt.description;
119 yield key("deprecated") + " = " + obj.deprecated;
125 array(commands.allHives.map(function (h) properties("command", h)))
126 .concat(modes.all.map(function (m)
127 properties("map", values(mappings.builtin.getStack(m)
128 .filter(function (map) map.modes[0] == m)))))
129 .concat(properties("mode", values(modes.all.filter(function (m) !m.hidden))))
130 .concat(properties("option", options))
131 .concat(properties("hintmode", values(hints.modes), "prompt"))
132 .concat(properties("pageinfo", values(Buffer.pageInfo), "title"))
133 .concat(properties("sanitizeitem", values(sanitizer.itemMap)))
134 .flatten().uniq().join("\n"));
137 Localized: Class("Localized", Class.Property, {
138 init: function init(prop, obj) {
139 let _prop = "unlocalized_" + prop;
140 if (this.initialized) {
142 if (config.locale === "en-US")
143 return { configurable: true, enumerable: true, value: this.default, writable: true };
146 if (!Set.has(obj, "localizedProperties"))
147 obj.localizedProperties = { __proto__: obj.localizedProperties };
148 obj.localizedProperties[prop] = true;
150 obj[_prop] = this.default;
152 get: function get() {
154 let value = this[_prop];
156 function getter(key, default_) function getter() messages.get([name, key].join("."), default_);
159 var name = [this.constructor.className.toLowerCase(),
160 this.identifier || this.name,
163 if (!isObject(value))
164 value = messages.get(name, value);
165 else if (isArray(value))
167 iter(value).forEach(function ([k, v]) {
169 memoize(v, 1, getter(v[0], v[1]));
171 memoize(value, k, getter(k, v));
174 iter(value).forEach(function ([k, v]) {
175 memoize(value, k, function () messages.get([name, k].join("."), v));
179 return Class.replaceProperty(this, prop, value);
182 set: function set(val) this[_prop] = val
186 this.initialized = true;
190 javascript: function initJavascript(dactyl, modules, window) {
191 let { JavaScript } = modules;
193 JavaScript.setCompleter([this._, this.get, this.format], [
194 function (context) messages.iterate()
197 JavaScript.setCompleter([this.export],
198 [function (context, obj, args) {
199 context.quote[2] = "";
200 modules.completion.file(context, true);
205 var { _ } = messages;
209 } catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
211 // vim: set fdm=marker sw=4 ts=4 et ft=javascript: