runtime.cjs.prod.js 4.1 KB

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