messages.d.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * A message function, as generated by {@link @messageformat/core#MessageFormat.compile}
  17. *
  18. * @public
  19. */
  20. export declare type MessageFunction = (param?: Record<string, unknown>) => string | unknown[];
  21. /**
  22. * Hierarchical message object
  23. *
  24. * @public
  25. */
  26. export interface MessageData {
  27. [key: string]: MessageData | MessageFunction | string;
  28. }
  29. /**
  30. * Accessor class for compiled message functions generated by
  31. * {@link @messageformat/core#compileModule}
  32. *
  33. * @public
  34. * @remarks
  35. * ```js
  36. * import Messages from '@messageformat/runtime/messages'
  37. * ```
  38. *
  39. * @example
  40. * ```js
  41. * // build.js
  42. * import { writeFileSync } from 'fs';
  43. * import MessageFormat from '@messageformat/core';
  44. * import compileModule from '@messageformat/core/compile-module'
  45. *
  46. * const mf = new MessageFormat(['en', 'fi']);
  47. * const msgSet = {
  48. * en: {
  49. * a: 'A {TYPE} example.',
  50. * b: 'This has {COUNT, plural, one{one user} other{# users}}.',
  51. * c: {
  52. * d: 'We have {P, number, percent} code coverage.'
  53. * }
  54. * },
  55. * fi: {
  56. * b: 'Tällä on {COUNT, plural, one{yksi käyttäjä} other{# käyttäjää}}.',
  57. * e: 'Minä puhun vain suomea.'
  58. * }
  59. * };
  60. * writeFileSync('messages.js', compileModule(mf, msgSet));
  61. * ```
  62. *
  63. * ```js
  64. * // runtime.js
  65. * import Messages from '@messageformat/runtime/messages';
  66. * import msgData from './messages';
  67. *
  68. * const messages = new Messages(msgData, 'en');
  69. *
  70. * messages.hasMessage('a') // true
  71. * messages.hasObject('c') // true
  72. * messages.get('b', { COUNT: 3 }) // 'This has 3 users.'
  73. * messages.get(['c', 'd'], { P: 0.314 }) // 'We have 31% code coverage.'
  74. *
  75. * messages.get('e') // 'e'
  76. * messages.setFallback('en', ['foo', 'fi'])
  77. * messages.get('e') // 'Minä puhun vain suomea.'
  78. *
  79. * messages.locale = 'fi'
  80. * messages.hasMessage('a') // false
  81. * messages.hasMessage('a', 'en') // true
  82. * messages.hasMessage('a', null, true) // true
  83. * messages.hasObject('c') // false
  84. * messages.get('b', { COUNT: 3 }) // 'Tällä on 3 käyttäjää.'
  85. * messages.get('c').d({ P: 0.628 }) // 'We have 63% code coverage.'
  86. * ```
  87. */
  88. export default class Messages {
  89. /** @internal */
  90. _data: {
  91. [key: string]: MessageData;
  92. };
  93. /** @internal */
  94. _fallback: {
  95. [key: string]: string[] | null;
  96. };
  97. /** @internal */
  98. _defaultLocale: string | null;
  99. /** @internal */
  100. _locale: string | null;
  101. /**
  102. * @param msgData - A map of locale codes to their function objects
  103. * @param defaultLocale - If not defined, default and initial locale is the first key of `msgData`
  104. */
  105. constructor(msgData: {
  106. [key: string]: MessageData;
  107. }, defaultLocale?: string);
  108. /** Read-only list of available locales */
  109. get availableLocales(): string[];
  110. /**
  111. * Current locale
  112. *
  113. * @remarks
  114. * One of {@link Messages.availableLocales} or `null`.
  115. * 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.
  116. */
  117. get locale(): string | null;
  118. set locale(locale: string | null);
  119. /**
  120. * Default fallback locale
  121. *
  122. * @remarks
  123. * One of {@link Messages.availableLocales} or `null`.
  124. * 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.
  125. */
  126. get defaultLocale(): string | null;
  127. set defaultLocale(locale: string | null);
  128. /**
  129. * Add new messages to the accessor; useful if loading data dynamically
  130. *
  131. * @remarks
  132. * The locale code `lc` should be an exact match for the locale being updated, or empty to default to the current locale.
  133. * Use {@link Messages.resolveLocale} for resolving partial locale strings.
  134. *
  135. * If `keypath` is empty, adds or sets the complete message object for the corresponding locale.
  136. * If any keys in `keypath` do not exist, a new object will be created at that key.
  137. *
  138. * @param data - Hierarchical map of keys to functions, or a single message function
  139. * @param locale - If empty or undefined, defaults to `this.locale`
  140. * @param keypath - The keypath being added
  141. */
  142. addMessages(data: MessageData | MessageFunction, locale?: string, keypath?: string[]): this;
  143. /**
  144. * Resolve `lc` to the key of an available locale or `null`, allowing for partial matches.
  145. *
  146. * @remarks
  147. * For example, with an `en` locale defined, it will be selected by `messages.defaultLocale = 'en-US'` and vice versa.
  148. */
  149. resolveLocale(locale: string | null): string | null;
  150. /**
  151. * Get the list of fallback locales
  152. *
  153. * @param locale - If empty or undefined, defaults to `this.locale`
  154. */
  155. getFallback(locale?: string | null): string[];
  156. /**
  157. * Set the fallback locale or locales for `lc`
  158. *
  159. * @remarks
  160. * To disable fallback for the locale, use `setFallback(lc, [])`.
  161. * To use the default fallback, use `setFallback(lc, null)`.
  162. */
  163. setFallback(lc: string, fallback: string[] | null): this;
  164. /**
  165. * Check if `key` is a message function for the locale
  166. *
  167. * @remarks
  168. * `key` may be a `string` for functions at the root level, or `string[]` for
  169. * accessing hierarchical objects. If an exact match is not found and
  170. * `fallback` is true, the fallback locales are checked for the first match.
  171. *
  172. * @param key - The key or keypath being sought
  173. * @param locale - If empty or undefined, defaults to `this.locale`
  174. * @param fallback - If true, also checks fallback locales
  175. */
  176. hasMessage(key: string | string[], locale?: string, fallback?: boolean): boolean;
  177. /**
  178. * Check if `key` is a message object for the locale
  179. *
  180. * @remarks
  181. * `key` may be a `string` for functions at the root level, or `string[]` for
  182. * accessing hierarchical objects. If an exact match is not found and
  183. * `fallback` is true, the fallback locales are checked for the first match.
  184. *
  185. * @param key - The key or keypath being sought
  186. * @param locale - If empty or undefined, defaults to `this.locale`
  187. * @param fallback - If true, also checks fallback locales
  188. */
  189. hasObject(key: string | string[], locale?: string, fallback?: boolean): boolean;
  190. /**
  191. * Get the message or object corresponding to `key`
  192. *
  193. * @remarks
  194. * `key` may be a `string` for functions at the root level, or `string[]` for accessing hierarchical objects.
  195. * If an exact match is not found, the fallback locales are checked for the first match.
  196. *
  197. * If `key` maps to a message function, the returned value will be the result of calling it with `props`.
  198. * If it maps to an object, the object is returned directly.
  199. * If nothing is found, `key` is returned.
  200. *
  201. * @param key - The key or keypath being sought
  202. * @param props - Optional properties passed to the function
  203. * @param lc - If empty or undefined, defaults to `this.locale`
  204. */
  205. get(key: string | string[], props?: Record<string, unknown>, locale?: string): string | unknown[] | MessageData;
  206. }