runtime.esm-bundler.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*!
  2. * @intlify/runtime v9.1.9
  3. * (c) 2021 kazuya kawaguchi
  4. * Released under the MIT License.
  5. */
  6. import { isNumber, isObject, isString, isFunction, isPlainObject, toDisplayString } from '@intlify/shared';
  7. const DEFAULT_MODIFIER = (str) => str;
  8. const DEFAULT_MESSAGE = (ctx) => ''; // eslint-disable-line
  9. const DEFAULT_MESSAGE_DATA_TYPE = 'text';
  10. const DEFAULT_NORMALIZE = (values) => values.length === 0 ? '' : values.join('');
  11. const DEFAULT_INTERPOLATE = toDisplayString;
  12. function pluralDefault(choice, choicesLength) {
  13. choice = Math.abs(choice);
  14. if (choicesLength === 2) {
  15. // prettier-ignore
  16. return choice
  17. ? choice > 1
  18. ? 1
  19. : 0
  20. : 1;
  21. }
  22. return choice ? Math.min(choice, 2) : 0;
  23. }
  24. function getPluralIndex(options) {
  25. // prettier-ignore
  26. const index = isNumber(options.pluralIndex)
  27. ? options.pluralIndex
  28. : -1;
  29. // prettier-ignore
  30. return options.named && (isNumber(options.named.count) || isNumber(options.named.n))
  31. ? isNumber(options.named.count)
  32. ? options.named.count
  33. : isNumber(options.named.n)
  34. ? options.named.n
  35. : index
  36. : index;
  37. }
  38. function normalizeNamed(pluralIndex, props) {
  39. if (!props.count) {
  40. props.count = pluralIndex;
  41. }
  42. if (!props.n) {
  43. props.n = pluralIndex;
  44. }
  45. }
  46. function createMessageContext(options = {}) {
  47. const locale = options.locale;
  48. const pluralIndex = getPluralIndex(options);
  49. const pluralRule = isObject(options.pluralRules) &&
  50. isString(locale) &&
  51. isFunction(options.pluralRules[locale])
  52. ? options.pluralRules[locale]
  53. : pluralDefault;
  54. const orgPluralRule = isObject(options.pluralRules) &&
  55. isString(locale) &&
  56. isFunction(options.pluralRules[locale])
  57. ? pluralDefault
  58. : undefined;
  59. const plural = (messages) => messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
  60. const _list = options.list || [];
  61. const list = (index) => _list[index];
  62. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  63. const _named = options.named || {};
  64. isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
  65. const named = (key) => _named[key];
  66. // TODO: need to design resolve message function?
  67. function message(key) {
  68. // prettier-ignore
  69. const msg = isFunction(options.messages)
  70. ? options.messages(key)
  71. : isObject(options.messages)
  72. ? options.messages[key]
  73. : false;
  74. return !msg
  75. ? options.parent
  76. ? options.parent.message(key) // resolve from parent messages
  77. : DEFAULT_MESSAGE
  78. : msg;
  79. }
  80. const _modifier = (name) => options.modifiers
  81. ? options.modifiers[name]
  82. : DEFAULT_MODIFIER;
  83. const normalize = isPlainObject(options.processor) && isFunction(options.processor.normalize)
  84. ? options.processor.normalize
  85. : DEFAULT_NORMALIZE;
  86. const interpolate = isPlainObject(options.processor) &&
  87. isFunction(options.processor.interpolate)
  88. ? options.processor.interpolate
  89. : DEFAULT_INTERPOLATE;
  90. const type = isPlainObject(options.processor) && isString(options.processor.type)
  91. ? options.processor.type
  92. : DEFAULT_MESSAGE_DATA_TYPE;
  93. const ctx = {
  94. ["list" /* LIST */]: list,
  95. ["named" /* NAMED */]: named,
  96. ["plural" /* PLURAL */]: plural,
  97. ["linked" /* LINKED */]: (key, modifier) => {
  98. // TODO: should check `key`
  99. const msg = message(key)(ctx);
  100. return isString(modifier) ? _modifier(modifier)(msg) : msg;
  101. },
  102. ["message" /* MESSAGE */]: message,
  103. ["type" /* TYPE */]: type,
  104. ["interpolate" /* INTERPOLATE */]: interpolate,
  105. ["normalize" /* NORMALIZE */]: normalize
  106. };
  107. return ctx;
  108. }
  109. export { DEFAULT_MESSAGE_DATA_TYPE, createMessageContext };