summaryrefslogtreecommitdiff
path: root/common/modules/messages.jsm
blob: 836bc0fd64a9093c0b3bc54dc7d9ac1a6f8a2a54 (plain)
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
// 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() {
        let self = this;

        this.bundle = services.stringBundle.createBundle(JSMLoader.getTarget("dactyl://locale/messages.properties"));

        let seen = {};
        for (let prop in iter(this.bundle.getSimpleEnumeration())) {
            let key = prop.QueryInterface(Ci.nsIPropertyElement).key.split(".")[0];
            if (!set.add(seen, key))
                this[key] = {
                    __noSuchMethod__: function __(prop, args) self._.apply(self, [prop].concat(args))
                };
        }
    },

    cleanup: function cleanup() {
        services.stringBundle.flushBundles();
    },

    _: function _(message) {
        if (arguments.length > 1) {
            let args = Array.slice(arguments, 1);
            return this.format(message + "-", args, null) || this.format(message, args);
        }
        return this.get(message);
    },

    get: function get(value, default_) {
        try {
            return this.bundle.GetStringFromName(value);
        }
        catch (e) {
            return arguments.length > 1 ? default_ : value;
        }
    },

    format: function format(value, args, default_) {
        try {
            return this.bundle.formatStringFromName(value, args, args.length);
        }
        catch (e) {
            return arguments.length > 2 ? default_ : value;
        }
    }

}, {
}, {
});

var { _ } = messages.closure;

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: