Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | this.EXPORTED_SYMBOLS = ["StringBundle"]; |
michael@0 | 6 | |
michael@0 | 7 | const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components; |
michael@0 | 8 | |
michael@0 | 9 | /** |
michael@0 | 10 | * A string bundle. |
michael@0 | 11 | * |
michael@0 | 12 | * This object presents two APIs: a deprecated one that is equivalent to the API |
michael@0 | 13 | * for the stringbundle XBL binding, to make it easy to switch from that binding |
michael@0 | 14 | * to this module, and a new one that is simpler and easier to use. |
michael@0 | 15 | * |
michael@0 | 16 | * The benefit of this module over the XBL binding is that it can also be used |
michael@0 | 17 | * in JavaScript modules and components, not only in chrome JS. |
michael@0 | 18 | * |
michael@0 | 19 | * To use this module, import it, create a new instance of StringBundle, |
michael@0 | 20 | * and then use the instance's |get| and |getAll| methods to retrieve strings |
michael@0 | 21 | * (you can get both plain and formatted strings with |get|): |
michael@0 | 22 | * |
michael@0 | 23 | * let strings = |
michael@0 | 24 | * new StringBundle("chrome://example/locale/strings.properties"); |
michael@0 | 25 | * let foo = strings.get("foo"); |
michael@0 | 26 | * let barFormatted = strings.get("bar", [arg1, arg2]); |
michael@0 | 27 | * for each (let string in strings.getAll()) |
michael@0 | 28 | * dump (string.key + " = " + string.value + "\n"); |
michael@0 | 29 | * |
michael@0 | 30 | * @param url {String} |
michael@0 | 31 | * the URL of the string bundle |
michael@0 | 32 | */ |
michael@0 | 33 | this.StringBundle = function StringBundle(url) { |
michael@0 | 34 | this.url = url; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | StringBundle.prototype = { |
michael@0 | 38 | /** |
michael@0 | 39 | * the locale associated with the application |
michael@0 | 40 | * @type nsILocale |
michael@0 | 41 | * @private |
michael@0 | 42 | */ |
michael@0 | 43 | get _appLocale() { |
michael@0 | 44 | try { |
michael@0 | 45 | return Cc["@mozilla.org/intl/nslocaleservice;1"]. |
michael@0 | 46 | getService(Ci.nsILocaleService). |
michael@0 | 47 | getApplicationLocale(); |
michael@0 | 48 | } |
michael@0 | 49 | catch(ex) { |
michael@0 | 50 | return null; |
michael@0 | 51 | } |
michael@0 | 52 | }, |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * the wrapped nsIStringBundle |
michael@0 | 56 | * @type nsIStringBundle |
michael@0 | 57 | * @private |
michael@0 | 58 | */ |
michael@0 | 59 | get _stringBundle() { |
michael@0 | 60 | let stringBundle = Cc["@mozilla.org/intl/stringbundle;1"]. |
michael@0 | 61 | getService(Ci.nsIStringBundleService). |
michael@0 | 62 | createBundle(this.url, this._appLocale); |
michael@0 | 63 | this.__defineGetter__("_stringBundle", function() stringBundle); |
michael@0 | 64 | return this._stringBundle; |
michael@0 | 65 | }, |
michael@0 | 66 | |
michael@0 | 67 | |
michael@0 | 68 | // the new API |
michael@0 | 69 | |
michael@0 | 70 | /** |
michael@0 | 71 | * the URL of the string bundle |
michael@0 | 72 | * @type String |
michael@0 | 73 | */ |
michael@0 | 74 | _url: null, |
michael@0 | 75 | get url() { |
michael@0 | 76 | return this._url; |
michael@0 | 77 | }, |
michael@0 | 78 | set url(newVal) { |
michael@0 | 79 | this._url = newVal; |
michael@0 | 80 | delete this._stringBundle; |
michael@0 | 81 | }, |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Get a string from the bundle. |
michael@0 | 85 | * |
michael@0 | 86 | * @param key {String} |
michael@0 | 87 | * the identifier of the string to get |
michael@0 | 88 | * @param args {array} [optional] |
michael@0 | 89 | * an array of arguments that replace occurrences of %S in the string |
michael@0 | 90 | * |
michael@0 | 91 | * @returns {String} the value of the string |
michael@0 | 92 | */ |
michael@0 | 93 | get: function(key, args) { |
michael@0 | 94 | if (args) |
michael@0 | 95 | return this.stringBundle.formatStringFromName(key, args, args.length); |
michael@0 | 96 | else |
michael@0 | 97 | return this.stringBundle.GetStringFromName(key); |
michael@0 | 98 | }, |
michael@0 | 99 | |
michael@0 | 100 | /** |
michael@0 | 101 | * Get all the strings in the bundle. |
michael@0 | 102 | * |
michael@0 | 103 | * @returns {Array} |
michael@0 | 104 | * an array of objects with key and value properties |
michael@0 | 105 | */ |
michael@0 | 106 | getAll: function() { |
michael@0 | 107 | let strings = []; |
michael@0 | 108 | |
michael@0 | 109 | // FIXME: for performance, return an enumerable array that wraps the string |
michael@0 | 110 | // bundle's nsISimpleEnumerator (does JavaScript already support this?). |
michael@0 | 111 | |
michael@0 | 112 | let enumerator = this.stringBundle.getSimpleEnumeration(); |
michael@0 | 113 | |
michael@0 | 114 | while (enumerator.hasMoreElements()) { |
michael@0 | 115 | // We could simply return the nsIPropertyElement objects, but I think |
michael@0 | 116 | // it's better to return standard JS objects that behave as consumers |
michael@0 | 117 | // expect JS objects to behave (f.e. you can modify them dynamically). |
michael@0 | 118 | let string = enumerator.getNext().QueryInterface(Ci.nsIPropertyElement); |
michael@0 | 119 | strings.push({ key: string.key, value: string.value }); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | return strings; |
michael@0 | 123 | }, |
michael@0 | 124 | |
michael@0 | 125 | |
michael@0 | 126 | // the deprecated XBL binding-compatible API |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * the URL of the string bundle |
michael@0 | 130 | * @deprecated because its name doesn't make sense outside of an XBL binding |
michael@0 | 131 | * @type String |
michael@0 | 132 | */ |
michael@0 | 133 | get src() { |
michael@0 | 134 | return this.url; |
michael@0 | 135 | }, |
michael@0 | 136 | set src(newVal) { |
michael@0 | 137 | this.url = newVal; |
michael@0 | 138 | }, |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * the locale associated with the application |
michael@0 | 142 | * @deprecated because it has never been used outside the XBL binding itself, |
michael@0 | 143 | * and consumers should obtain it directly from the locale service anyway. |
michael@0 | 144 | * @type nsILocale |
michael@0 | 145 | */ |
michael@0 | 146 | get appLocale() { |
michael@0 | 147 | return this._appLocale; |
michael@0 | 148 | }, |
michael@0 | 149 | |
michael@0 | 150 | /** |
michael@0 | 151 | * the wrapped nsIStringBundle |
michael@0 | 152 | * @deprecated because this module should provide all necessary functionality |
michael@0 | 153 | * @type nsIStringBundle |
michael@0 | 154 | * |
michael@0 | 155 | * If you do ever need to use this, let the authors of this module know why |
michael@0 | 156 | * so they can surface functionality for your use case in the module itself |
michael@0 | 157 | * and you don't have to access this underlying XPCOM component. |
michael@0 | 158 | */ |
michael@0 | 159 | get stringBundle() { |
michael@0 | 160 | return this._stringBundle; |
michael@0 | 161 | }, |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * Get a string from the bundle. |
michael@0 | 165 | * @deprecated use |get| instead |
michael@0 | 166 | * |
michael@0 | 167 | * @param key {String} |
michael@0 | 168 | * the identifier of the string to get |
michael@0 | 169 | * |
michael@0 | 170 | * @returns {String} |
michael@0 | 171 | * the value of the string |
michael@0 | 172 | */ |
michael@0 | 173 | getString: function(key) { |
michael@0 | 174 | return this.get(key); |
michael@0 | 175 | }, |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * Get a formatted string from the bundle. |
michael@0 | 179 | * @deprecated use |get| instead |
michael@0 | 180 | * |
michael@0 | 181 | * @param key {string} |
michael@0 | 182 | * the identifier of the string to get |
michael@0 | 183 | * @param args {array} |
michael@0 | 184 | * an array of arguments that replace occurrences of %S in the string |
michael@0 | 185 | * |
michael@0 | 186 | * @returns {String} |
michael@0 | 187 | * the formatted value of the string |
michael@0 | 188 | */ |
michael@0 | 189 | getFormattedString: function(key, args) { |
michael@0 | 190 | return this.get(key, args); |
michael@0 | 191 | }, |
michael@0 | 192 | |
michael@0 | 193 | /** |
michael@0 | 194 | * Get an enumeration of the strings in the bundle. |
michael@0 | 195 | * @deprecated use |getAll| instead |
michael@0 | 196 | * |
michael@0 | 197 | * @returns {nsISimpleEnumerator} |
michael@0 | 198 | * a enumeration of the strings in the bundle |
michael@0 | 199 | */ |
michael@0 | 200 | get strings() { |
michael@0 | 201 | return this.stringBundle.getSimpleEnumeration(); |
michael@0 | 202 | } |
michael@0 | 203 | } |