StringBundle.js 7.0 KB

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