compiler.d.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { FunctionArg, Select, Token } from '@messageformat/parser';
  2. import { MessageFormatOptions } from './messageformat';
  3. import { PluralObject } from './plurals';
  4. type RuntimeType = 'formatter' | 'locale' | 'runtime';
  5. interface RuntimeEntry {
  6. (...args: any[]): unknown;
  7. id?: string | null;
  8. module?: string | null;
  9. toString?: () => string;
  10. type?: RuntimeType;
  11. }
  12. export interface RuntimeMap {
  13. [key: string]: Required<RuntimeEntry>;
  14. }
  15. /**
  16. * A hierarchical structure of ICU MessageFormat strings
  17. *
  18. * @public
  19. * @remarks
  20. * Used in {@link compileModule} arguments
  21. */
  22. export interface StringStructure {
  23. [key: string]: StringStructure | string;
  24. }
  25. export default class Compiler {
  26. arguments: string[];
  27. options: Required<MessageFormatOptions>;
  28. plural: PluralObject;
  29. runtime: RuntimeMap;
  30. constructor(options: Required<MessageFormatOptions>);
  31. /**
  32. * Recursively compile a string or a tree of strings to JavaScript function
  33. * sources
  34. *
  35. * If `src` is an object with a key that is also present in `plurals`, the key
  36. * in question will be used as the locale identifier for its value. To disable
  37. * the compile-time checks for plural & selectordinal keys while maintaining
  38. * multi-locale support, use falsy values in `plurals`.
  39. *
  40. * @param src - The source for which the JS code should be generated
  41. * @param plural - The default locale
  42. * @param plurals - A map of pluralization keys for all available locales
  43. */
  44. compile(src: string | StringStructure, plural: PluralObject, plurals?: {
  45. [key: string]: PluralObject;
  46. }): string | StringStructure;
  47. cases(token: Select, pluralToken: Select | null): string;
  48. concatenate(tokens: string[], root: boolean): string;
  49. token(token: Token, pluralToken: Select | null): string;
  50. runtimeIncludes(key: string, type: RuntimeType): Required<RuntimeEntry>;
  51. setLocale(key: string, ord: boolean): void;
  52. setRuntimeFn(key: 'number' | 'plural' | 'select' | 'strictNumber' | 'reqArgs'): void;
  53. getFormatterArg({ key, param }: FunctionArg, pluralToken: Select | null): string | null;
  54. setFormatter(key: string): void;
  55. setDateFormatter({ param }: FunctionArg, args: (number | string)[], plural: Select | null): string;
  56. setNumberFormatter({ param }: FunctionArg, args: (number | string)[], plural: Select | null): string;
  57. }
  58. export {};