/*! * @intlify/runtime v9.1.9 * (c) 2021 kazuya kawaguchi * Released under the MIT License. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var shared = require('@intlify/shared'); const DEFAULT_MODIFIER = (str) => str; const DEFAULT_MESSAGE = (ctx) => ''; // eslint-disable-line const DEFAULT_MESSAGE_DATA_TYPE = 'text'; const DEFAULT_NORMALIZE = (values) => values.length === 0 ? '' : values.join(''); const DEFAULT_INTERPOLATE = shared.toDisplayString; function pluralDefault(choice, choicesLength) { choice = Math.abs(choice); if (choicesLength === 2) { // prettier-ignore return choice ? choice > 1 ? 1 : 0 : 1; } return choice ? Math.min(choice, 2) : 0; } function getPluralIndex(options) { // prettier-ignore const index = shared.isNumber(options.pluralIndex) ? options.pluralIndex : -1; // prettier-ignore return options.named && (shared.isNumber(options.named.count) || shared.isNumber(options.named.n)) ? shared.isNumber(options.named.count) ? options.named.count : shared.isNumber(options.named.n) ? options.named.n : index : index; } function normalizeNamed(pluralIndex, props) { if (!props.count) { props.count = pluralIndex; } if (!props.n) { props.n = pluralIndex; } } function createMessageContext(options = {}) { const locale = options.locale; const pluralIndex = getPluralIndex(options); const pluralRule = shared.isObject(options.pluralRules) && shared.isString(locale) && shared.isFunction(options.pluralRules[locale]) ? options.pluralRules[locale] : pluralDefault; const orgPluralRule = shared.isObject(options.pluralRules) && shared.isString(locale) && shared.isFunction(options.pluralRules[locale]) ? pluralDefault : undefined; const plural = (messages) => messages[pluralRule(pluralIndex, messages.length, orgPluralRule)]; const _list = options.list || []; const list = (index) => _list[index]; // eslint-disable-next-line @typescript-eslint/no-explicit-any const _named = options.named || {}; shared.isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named); const named = (key) => _named[key]; // TODO: need to design resolve message function? function message(key) { // prettier-ignore const msg = shared.isFunction(options.messages) ? options.messages(key) : shared.isObject(options.messages) ? options.messages[key] : false; return !msg ? options.parent ? options.parent.message(key) // resolve from parent messages : DEFAULT_MESSAGE : msg; } const _modifier = (name) => options.modifiers ? options.modifiers[name] : DEFAULT_MODIFIER; const normalize = shared.isPlainObject(options.processor) && shared.isFunction(options.processor.normalize) ? options.processor.normalize : DEFAULT_NORMALIZE; const interpolate = shared.isPlainObject(options.processor) && shared.isFunction(options.processor.interpolate) ? options.processor.interpolate : DEFAULT_INTERPOLATE; const type = shared.isPlainObject(options.processor) && shared.isString(options.processor.type) ? options.processor.type : DEFAULT_MESSAGE_DATA_TYPE; const ctx = { ["list" /* LIST */]: list, ["named" /* NAMED */]: named, ["plural" /* PLURAL */]: plural, ["linked" /* LINKED */]: (key, modifier) => { // TODO: should check `key` const msg = message(key)(ctx); return shared.isString(modifier) ? _modifier(modifier)(msg) : msg; }, ["message" /* MESSAGE */]: message, ["type" /* TYPE */]: type, ["interpolate" /* INTERPOLATE */]: interpolate, ["normalize" /* NORMALIZE */]: normalize }; return ctx; } exports.DEFAULT_MESSAGE_DATA_TYPE = DEFAULT_MESSAGE_DATA_TYPE; exports.createMessageContext = createMessageContext;