stringbundle.js 5.5 KB

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