messages.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /**
  2. * A collection of runtime utility functions
  3. *
  4. * @remarks
  5. * This package should be marked as a dependency for any package that publishes the output of {@link @messageformat/core#compileModule},
  6. * as it may be included in its ES module source output as a dependency.
  7. *
  8. * For applications that bundle their output using e.g. Webpack this is not necessary.
  9. *
  10. * The `Messages` accessor class is a completely optional addition.
  11. * See also {@link @messageformat/react# | @messageformat/react} for a React-specific solution.
  12. *
  13. * @packageDocumentation
  14. */
  15. /**
  16. * Accessor class for compiled message functions generated by
  17. * {@link @messageformat/core#compileModule}
  18. *
  19. * @public
  20. * @remarks
  21. * ```js
  22. * import Messages from '@messageformat/runtime/messages'
  23. * ```
  24. *
  25. * @example
  26. * ```js
  27. * // build.js
  28. * import { writeFileSync } from 'fs';
  29. * import MessageFormat from '@messageformat/core';
  30. * import compileModule from '@messageformat/core/compile-module'
  31. *
  32. * const mf = new MessageFormat(['en', 'fi']);
  33. * const msgSet = {
  34. * en: {
  35. * a: 'A {TYPE} example.',
  36. * b: 'This has {COUNT, plural, one{one user} other{# users}}.',
  37. * c: {
  38. * d: 'We have {P, number, percent} code coverage.'
  39. * }
  40. * },
  41. * fi: {
  42. * b: 'Tällä on {COUNT, plural, one{yksi käyttäjä} other{# käyttäjää}}.',
  43. * e: 'Minä puhun vain suomea.'
  44. * }
  45. * };
  46. * writeFileSync('messages.js', compileModule(mf, msgSet));
  47. * ```
  48. *
  49. * ```js
  50. * // runtime.js
  51. * import Messages from '@messageformat/runtime/messages';
  52. * import msgData from './messages';
  53. *
  54. * const messages = new Messages(msgData, 'en');
  55. *
  56. * messages.hasMessage('a') // true
  57. * messages.hasObject('c') // true
  58. * messages.get('b', { COUNT: 3 }) // 'This has 3 users.'
  59. * messages.get(['c', 'd'], { P: 0.314 }) // 'We have 31% code coverage.'
  60. *
  61. * messages.get('e') // 'e'
  62. * messages.setFallback('en', ['foo', 'fi'])
  63. * messages.get('e') // 'Minä puhun vain suomea.'
  64. *
  65. * messages.locale = 'fi'
  66. * messages.hasMessage('a') // false
  67. * messages.hasMessage('a', 'en') // true
  68. * messages.hasMessage('a', null, true) // true
  69. * messages.hasObject('c') // false
  70. * messages.get('b', { COUNT: 3 }) // 'Tällä on 3 käyttäjää.'
  71. * messages.get('c').d({ P: 0.628 }) // 'We have 63% code coverage.'
  72. * ```
  73. */
  74. var Messages = /** @class */ (function () {
  75. /**
  76. * @param msgData - A map of locale codes to their function objects
  77. * @param defaultLocale - If not defined, default and initial locale is the first key of `msgData`
  78. */
  79. function Messages(msgData, defaultLocale) {
  80. var _this = this;
  81. /** @internal */
  82. this._data = {};
  83. /** @internal */
  84. this._fallback = {};
  85. /** @internal */
  86. this._defaultLocale = null;
  87. /** @internal */
  88. this._locale = null;
  89. Object.keys(msgData).forEach(function (lc) {
  90. if (lc !== 'toString') {
  91. _this._data[lc] = msgData[lc];
  92. if (defaultLocale === undefined)
  93. defaultLocale = lc;
  94. }
  95. });
  96. this.locale = defaultLocale || null;
  97. this._defaultLocale = this.locale;
  98. }
  99. Object.defineProperty(Messages.prototype, "availableLocales", {
  100. /** Read-only list of available locales */
  101. get: function () {
  102. return Object.keys(this._data);
  103. },
  104. enumerable: false,
  105. configurable: true
  106. });
  107. Object.defineProperty(Messages.prototype, "locale", {
  108. /**
  109. * Current locale
  110. *
  111. * @remarks
  112. * One of {@link Messages.availableLocales} or `null`.
  113. * Partial matches of language tags are supported, so e.g. with an `en` locale defined, it will be selected by `messages.locale = 'en-US'` and vice versa.
  114. */
  115. get: function () {
  116. return this._locale;
  117. },
  118. set: function (locale) {
  119. this._locale = this.resolveLocale(locale);
  120. },
  121. enumerable: false,
  122. configurable: true
  123. });
  124. Object.defineProperty(Messages.prototype, "defaultLocale", {
  125. /**
  126. * Default fallback locale
  127. *
  128. * @remarks
  129. * One of {@link Messages.availableLocales} or `null`.
  130. * Partial matches of language tags are supported, so e.g. with an `en` locale defined, it will be selected by `messages.defaultLocale = 'en-US'` and vice versa.
  131. */
  132. get: function () {
  133. return this._defaultLocale;
  134. },
  135. set: function (locale) {
  136. this._defaultLocale = this.resolveLocale(locale);
  137. },
  138. enumerable: false,
  139. configurable: true
  140. });
  141. /**
  142. * Add new messages to the accessor; useful if loading data dynamically
  143. *
  144. * @remarks
  145. * The locale code `lc` should be an exact match for the locale being updated, or empty to default to the current locale.
  146. * Use {@link Messages.resolveLocale} for resolving partial locale strings.
  147. *
  148. * If `keypath` is empty, adds or sets the complete message object for the corresponding locale.
  149. * If any keys in `keypath` do not exist, a new object will be created at that key.
  150. *
  151. * @param data - Hierarchical map of keys to functions, or a single message function
  152. * @param locale - If empty or undefined, defaults to `this.locale`
  153. * @param keypath - The keypath being added
  154. */
  155. Messages.prototype.addMessages = function (data, locale, keypath) {
  156. var lc = locale || String(this.locale);
  157. if (typeof data !== 'function') {
  158. data = Object.keys(data).reduce(function (map, key) {
  159. if (key !== 'toString')
  160. map[key] = data[key];
  161. return map;
  162. }, {});
  163. }
  164. if (Array.isArray(keypath) && keypath.length > 0) {
  165. var parent_1 = this._data[lc];
  166. for (var i = 0; i < keypath.length - 1; ++i) {
  167. var key = keypath[i];
  168. if (!parent_1[key])
  169. parent_1[key] = {};
  170. parent_1 = parent_1[key];
  171. }
  172. parent_1[keypath[keypath.length - 1]] = data;
  173. }
  174. else {
  175. this._data[lc] = data;
  176. }
  177. return this;
  178. };
  179. /**
  180. * Resolve `lc` to the key of an available locale or `null`, allowing for partial matches.
  181. *
  182. * @remarks
  183. * For example, with an `en` locale defined, it will be selected by `messages.defaultLocale = 'en-US'` and vice versa.
  184. */
  185. Messages.prototype.resolveLocale = function (locale) {
  186. var lc = String(locale);
  187. if (this._data[lc])
  188. return locale;
  189. if (locale) {
  190. while ((lc = lc.replace(/[-_]?[^-_]*$/, ''))) {
  191. if (this._data[lc])
  192. return lc;
  193. }
  194. var ll = this.availableLocales;
  195. var re = new RegExp('^' + locale + '[-_]');
  196. for (var i = 0; i < ll.length; ++i) {
  197. if (re.test(ll[i]))
  198. return ll[i];
  199. }
  200. }
  201. return null;
  202. };
  203. /**
  204. * Get the list of fallback locales
  205. *
  206. * @param locale - If empty or undefined, defaults to `this.locale`
  207. */
  208. Messages.prototype.getFallback = function (locale) {
  209. var lc = locale || String(this.locale);
  210. return (this._fallback[lc] ||
  211. (lc === this.defaultLocale || !this.defaultLocale
  212. ? []
  213. : [this.defaultLocale]));
  214. };
  215. /**
  216. * Set the fallback locale or locales for `lc`
  217. *
  218. * @remarks
  219. * To disable fallback for the locale, use `setFallback(lc, [])`.
  220. * To use the default fallback, use `setFallback(lc, null)`.
  221. */
  222. Messages.prototype.setFallback = function (lc, fallback) {
  223. this._fallback[lc] = Array.isArray(fallback) ? fallback : null;
  224. return this;
  225. };
  226. /**
  227. * Check if `key` is a message function for the locale
  228. *
  229. * @remarks
  230. * `key` may be a `string` for functions at the root level, or `string[]` for
  231. * accessing hierarchical objects. If an exact match is not found and
  232. * `fallback` is true, the fallback locales are checked for the first match.
  233. *
  234. * @param key - The key or keypath being sought
  235. * @param locale - If empty or undefined, defaults to `this.locale`
  236. * @param fallback - If true, also checks fallback locales
  237. */
  238. Messages.prototype.hasMessage = function (key, locale, fallback) {
  239. var lc = locale || String(this.locale);
  240. var fb = fallback ? this.getFallback(lc) : null;
  241. return _has(this._data, lc, key, fb, 'function');
  242. };
  243. /**
  244. * Check if `key` is a message object for the locale
  245. *
  246. * @remarks
  247. * `key` may be a `string` for functions at the root level, or `string[]` for
  248. * accessing hierarchical objects. If an exact match is not found and
  249. * `fallback` is true, the fallback locales are checked for the first match.
  250. *
  251. * @param key - The key or keypath being sought
  252. * @param locale - If empty or undefined, defaults to `this.locale`
  253. * @param fallback - If true, also checks fallback locales
  254. */
  255. Messages.prototype.hasObject = function (key, locale, fallback) {
  256. var lc = locale || String(this.locale);
  257. var fb = fallback ? this.getFallback(lc) : null;
  258. return _has(this._data, lc, key, fb, 'object');
  259. };
  260. /**
  261. * Get the message or object corresponding to `key`
  262. *
  263. * @remarks
  264. * `key` may be a `string` for functions at the root level, or `string[]` for accessing hierarchical objects.
  265. * If an exact match is not found, the fallback locales are checked for the first match.
  266. *
  267. * If `key` maps to a message function, the returned value will be the result of calling it with `props`.
  268. * If it maps to an object, the object is returned directly.
  269. * If nothing is found, `key` is returned.
  270. *
  271. * @param key - The key or keypath being sought
  272. * @param props - Optional properties passed to the function
  273. * @param lc - If empty or undefined, defaults to `this.locale`
  274. */
  275. Messages.prototype.get = function (key, props, locale) {
  276. var lc = locale || String(this.locale);
  277. var msg = _get(this._data[lc], key);
  278. if (msg)
  279. return typeof msg == 'function' ? msg(props) : msg;
  280. var fb = this.getFallback(lc);
  281. for (var i = 0; i < fb.length; ++i) {
  282. msg = _get(this._data[fb[i]], key);
  283. if (msg)
  284. return typeof msg == 'function' ? msg(props) : msg;
  285. }
  286. return key;
  287. };
  288. return Messages;
  289. }());
  290. export default Messages;
  291. function _get(obj, key) {
  292. if (!obj)
  293. return null;
  294. var res = obj;
  295. if (Array.isArray(key)) {
  296. for (var i = 0; i < key.length; ++i) {
  297. if (typeof res !== 'object')
  298. return null;
  299. res = res[key[i]];
  300. if (!res)
  301. return null;
  302. }
  303. return res;
  304. }
  305. return typeof res === 'object' ? res[key] : null;
  306. }
  307. function _has(data, lc, key, fallback, type) {
  308. var msg = _get(data[lc], key);
  309. if (msg)
  310. return typeof msg === type;
  311. if (fallback) {
  312. for (var i = 0; i < fallback.length; ++i) {
  313. msg = _get(data[fallback[i]], key);
  314. if (msg)
  315. return typeof msg === type;
  316. }
  317. }
  318. return false;
  319. }