@intlify/core 12.0.0-alpha.5 → 12.0.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.esm-browser.js +177 -144
- package/dist/core.esm-browser.prod.js +467 -429
- package/dist/core.global.js +178 -142
- package/dist/core.global.prod.js +294 -256
- package/dist/core.js +1 -1
- package/dist/core.node.js +1 -1
- package/dist/core.runtime.esm-browser.js +177 -149
- package/dist/core.runtime.esm-browser.prod.js +327 -290
- package/dist/core.runtime.global.js +178 -147
- package/dist/core.runtime.global.prod.js +309 -271
- package/dist/core.runtime.js +1 -5
- package/dist/core.runtime.node.js +1 -5
- package/package.json +4 -4
package/dist/core.esm-browser.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @intlify/core v12.0.0-alpha.
|
|
2
|
+
* @intlify/core v12.0.0-alpha.6
|
|
3
3
|
* (c) 2016-present kazuya kawaguchi and contributors
|
|
4
4
|
* @license MIT
|
|
5
5
|
**/
|
|
@@ -1385,7 +1385,7 @@ function compile(message, context) {
|
|
|
1385
1385
|
return !detectError ? compileCache[cacheKey] = msg : msg;
|
|
1386
1386
|
} else {
|
|
1387
1387
|
if (!isMessageAST(message)) {
|
|
1388
|
-
warn(`the message
|
|
1388
|
+
warn(`Cannot compile the message of '${context.key}' key, because the message compiler is not included in this build. Pre-compile the message. So translate function returns the message as is.`);
|
|
1389
1389
|
return (() => message);
|
|
1390
1390
|
}
|
|
1391
1391
|
const cacheKey = message.cacheKey;
|
|
@@ -1716,7 +1716,7 @@ const cache = /* @__PURE__ */ new Map();
|
|
|
1716
1716
|
* @VueI18nGeneral
|
|
1717
1717
|
*/
|
|
1718
1718
|
function resolveWithKeyValue(obj, path) {
|
|
1719
|
-
return isObject(obj) ? obj
|
|
1719
|
+
return isObject(obj) ? getOwn(obj, path) : null;
|
|
1720
1720
|
}
|
|
1721
1721
|
/**
|
|
1722
1722
|
* message resolver
|
|
@@ -1771,12 +1771,15 @@ const CoreWarnCodes = {
|
|
|
1771
1771
|
FALLBACK_TO_DATE_FORMAT: 6,
|
|
1772
1772
|
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
|
|
1773
1773
|
INVALID_NUMBER_ARGUMENT: 8,
|
|
1774
|
-
INVALID_DATE_ARGUMENT: 9
|
|
1774
|
+
INVALID_DATE_ARGUMENT: 9,
|
|
1775
|
+
IGNORE_UNSAFE_LOCALE: 10,
|
|
1776
|
+
FALLBACK_TO_DEFAULT_PLURAL_RULE: 11,
|
|
1777
|
+
FALLBACK_FROM_INVALID_LOCALE: 12
|
|
1775
1778
|
};
|
|
1776
|
-
const CORE_WARN_CODES_EXTEND_POINT =
|
|
1779
|
+
const CORE_WARN_CODES_EXTEND_POINT = 13;
|
|
1777
1780
|
/** @internal */
|
|
1778
1781
|
const warnMessages = {
|
|
1779
|
-
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale
|
|
1782
|
+
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale {resource}.`,
|
|
1780
1783
|
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
1781
1784
|
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
1782
1785
|
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
@@ -1784,7 +1787,10 @@ const warnMessages = {
|
|
|
1784
1787
|
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
1785
1788
|
[CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER]: `This project is using Custom Message Compiler, which is an experimental feature. It may receive breaking changes or be removed in the future.`,
|
|
1786
1789
|
[CoreWarnCodes.INVALID_NUMBER_ARGUMENT]: `Invalid argument for number formatting: expected a number but received '{value}'.`,
|
|
1787
|
-
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'
|
|
1790
|
+
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'.`,
|
|
1791
|
+
[CoreWarnCodes.IGNORE_UNSAFE_LOCALE]: `Ignore '{locale}' locale: it is a property name of Object.prototype`,
|
|
1792
|
+
[CoreWarnCodes.FALLBACK_TO_DEFAULT_PLURAL_RULE]: `Fall back to the default plural rule for '{locale}' locale: Intl.PluralRules does not support it`,
|
|
1793
|
+
[CoreWarnCodes.FALLBACK_FROM_INVALID_LOCALE]: `Fall back to '{target}' locale for {type} formatting with '{locale}' locale: it is not a BCP 47 language tag`
|
|
1788
1794
|
};
|
|
1789
1795
|
function getWarnMessage(code, ...args) {
|
|
1790
1796
|
return format$1(warnMessages[code], ...args);
|
|
@@ -1795,7 +1801,7 @@ function getWarnMessage(code, ...args) {
|
|
|
1795
1801
|
* Intlify core-base version
|
|
1796
1802
|
* @internal
|
|
1797
1803
|
*/
|
|
1798
|
-
const VERSION = "12.0.0-alpha.
|
|
1804
|
+
const VERSION = "12.0.0-alpha.6";
|
|
1799
1805
|
const NOT_RESOLVED = -1;
|
|
1800
1806
|
const DEFAULT_LOCALE = "en-US";
|
|
1801
1807
|
const MISSING_RESOLVE_VALUE = "";
|
|
@@ -1861,15 +1867,15 @@ function createCoreContext(options = {}) {
|
|
|
1861
1867
|
const version = isString(options.version) ? options.version : VERSION;
|
|
1862
1868
|
const locale = isString(options.locale) || isFunction(options.locale) ? options.locale : DEFAULT_LOCALE;
|
|
1863
1869
|
const _locale = isFunction(locale) ? DEFAULT_LOCALE : locale;
|
|
1864
|
-
const fallbackLocale =
|
|
1870
|
+
const fallbackLocale = isFallbackLocale(options.fallbackLocale) ? options.fallbackLocale : _locale;
|
|
1865
1871
|
const messages = isPlainObject(options.messages) ? options.messages : createResources(_locale);
|
|
1866
1872
|
const datetimeFormats = isPlainObject(options.datetimeFormats) ? options.datetimeFormats : createResources(_locale);
|
|
1867
1873
|
const numberFormats = isPlainObject(options.numberFormats) ? options.numberFormats : createResources(_locale);
|
|
1868
1874
|
const modifiers = assign(create(), options.modifiers, getDefaultLinkedModifiers());
|
|
1869
1875
|
const pluralRules = options.pluralRules || create();
|
|
1870
1876
|
const missing = isFunction(options.missing) ? options.missing : null;
|
|
1871
|
-
const missingWarn =
|
|
1872
|
-
const fallbackWarn =
|
|
1877
|
+
const missingWarn = isWarnOption(options.missingWarn) ? options.missingWarn : true;
|
|
1878
|
+
const fallbackWarn = isWarnOption(options.fallbackWarn) ? options.fallbackWarn : true;
|
|
1873
1879
|
const fallbackFormat = !!options.fallbackFormat;
|
|
1874
1880
|
const unresolving = !!options.unresolving;
|
|
1875
1881
|
const postTranslation = isFunction(options.postTranslation) ? options.postTranslation : null;
|
|
@@ -1877,13 +1883,9 @@ function createCoreContext(options = {}) {
|
|
|
1877
1883
|
const warnHtmlMessage = isBoolean(options.warnHtmlMessage) ? options.warnHtmlMessage : true;
|
|
1878
1884
|
const escapeParameter = !!options.escapeParameter;
|
|
1879
1885
|
const messageCompiler = isFunction(options.messageCompiler) ? options.messageCompiler : _compiler || null;
|
|
1880
|
-
if (isFunction(options.messageCompiler)) warnOnce(getWarnMessage(CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER));
|
|
1881
1886
|
const messageResolver = isFunction(options.messageResolver) ? options.messageResolver : _resolver || resolveWithKeyValue;
|
|
1882
1887
|
const localeFallbacker = isFunction(options.localeFallbacker) ? options.localeFallbacker : _fallbacker || fallbackWithSimple;
|
|
1883
1888
|
const fallbackContext = isObject(options.fallbackContext) ? options.fallbackContext : void 0;
|
|
1884
|
-
const internalOptions = options;
|
|
1885
|
-
const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters) ? internalOptions.__datetimeFormatters : /* @__PURE__ */ new Map();
|
|
1886
|
-
const __numberFormatters = isObject(internalOptions.__numberFormatters) ? internalOptions.__numberFormatters : /* @__PURE__ */ new Map();
|
|
1887
1889
|
_cid++;
|
|
1888
1890
|
const context = {
|
|
1889
1891
|
version,
|
|
@@ -1910,9 +1912,8 @@ function createCoreContext(options = {}) {
|
|
|
1910
1912
|
};
|
|
1911
1913
|
context.datetimeFormats = datetimeFormats;
|
|
1912
1914
|
context.numberFormats = numberFormats;
|
|
1913
|
-
context.__datetimeFormatters =
|
|
1914
|
-
context.__numberFormatters =
|
|
1915
|
-
context.__v_emitter = internalOptions.__v_emitter != null ? internalOptions.__v_emitter : void 0;
|
|
1915
|
+
context.__datetimeFormatters = /* @__PURE__ */ new Map();
|
|
1916
|
+
context.__numberFormatters = /* @__PURE__ */ new Map();
|
|
1916
1917
|
return context;
|
|
1917
1918
|
}
|
|
1918
1919
|
const createResources = (locale) => ({ [locale]: create() });
|
|
@@ -1930,10 +1931,21 @@ function getLocaleMessage(ctx, locale) {
|
|
|
1930
1931
|
* @version 12.0.0
|
|
1931
1932
|
*/
|
|
1932
1933
|
function setLocaleMessage(ctx, locale, messages) {
|
|
1933
|
-
if (isObjectPrototypeKey(locale))
|
|
1934
|
+
if (isObjectPrototypeKey(locale)) {
|
|
1935
|
+
ctx.onWarn(getWarnMessage(CoreWarnCodes.IGNORE_UNSAFE_LOCALE, { locale }));
|
|
1936
|
+
return;
|
|
1937
|
+
}
|
|
1934
1938
|
ctx.messages[locale] = messages;
|
|
1935
1939
|
}
|
|
1936
1940
|
/** @internal */
|
|
1941
|
+
function isFallbackLocale(val) {
|
|
1942
|
+
return isString(val) || isArray(val) || isPlainObject(val) || val === false;
|
|
1943
|
+
}
|
|
1944
|
+
/** @internal */
|
|
1945
|
+
function isWarnOption(val) {
|
|
1946
|
+
return isBoolean(val) || isRegExp(val);
|
|
1947
|
+
}
|
|
1948
|
+
/** @internal */
|
|
1937
1949
|
function isTranslateFallbackWarn(fallback, key) {
|
|
1938
1950
|
return isRegExp(fallback) ? fallback.test(key) : fallback;
|
|
1939
1951
|
}
|
|
@@ -1959,7 +1971,8 @@ function handleMissing(context, key, locale, missingWarn, type) {
|
|
|
1959
1971
|
} else {
|
|
1960
1972
|
if (isTranslateMissingWarn(missingWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, {
|
|
1961
1973
|
key,
|
|
1962
|
-
locale
|
|
1974
|
+
locale,
|
|
1975
|
+
resource: type === "datetime format" ? "datetime formats" : type === "number format" ? "number formats" : "messages"
|
|
1963
1976
|
}));
|
|
1964
1977
|
return key;
|
|
1965
1978
|
}
|
|
@@ -1973,6 +1986,11 @@ function invalidateLocaleChainCache(ctx, fallback) {
|
|
|
1973
1986
|
function updateFallbackLocale(ctx, locale, fallback) {
|
|
1974
1987
|
invalidateLocaleChainCache(ctx, fallback);
|
|
1975
1988
|
if (ctx.fallbackContext) invalidateLocaleChainCache(ctx.fallbackContext, fallback);
|
|
1989
|
+
{
|
|
1990
|
+
const { __datetimeFormatters, __numberFormatters } = ctx;
|
|
1991
|
+
__datetimeFormatters.clear();
|
|
1992
|
+
__numberFormatters.clear();
|
|
1993
|
+
}
|
|
1976
1994
|
ctx.localeFallbacker(ctx, fallback, locale);
|
|
1977
1995
|
}
|
|
1978
1996
|
/** @internal */
|
|
@@ -2009,13 +2027,69 @@ function resolveFormatLocale(context, key, locale, formats, missingWarn, fallbac
|
|
|
2009
2027
|
groupId: toDevtoolsGroupId(type, key)
|
|
2010
2028
|
});
|
|
2011
2029
|
}
|
|
2012
|
-
const format = (formats
|
|
2030
|
+
const format = getOwn(getOwn(formats, targetLocale) || {}, key);
|
|
2013
2031
|
if (isPlainObject(format) && isString(targetLocale)) return targetLocale;
|
|
2014
2032
|
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
2015
2033
|
from = targetLocale;
|
|
2016
2034
|
}
|
|
2017
2035
|
return null;
|
|
2018
2036
|
}
|
|
2037
|
+
function isLanguageTag(locale) {
|
|
2038
|
+
try {
|
|
2039
|
+
Intl.getCanonicalLocales(locale);
|
|
2040
|
+
return true;
|
|
2041
|
+
} catch {
|
|
2042
|
+
return false;
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
/**
|
|
2046
|
+
* Create an `Intl.DateTimeFormat` or `Intl.NumberFormat` formatter
|
|
2047
|
+
*
|
|
2048
|
+
* @remarks
|
|
2049
|
+
* `Intl` throws a RangeError for a locale that is not a BCP 47 language tag (e.g. `pt_BR`),
|
|
2050
|
+
* so fall back to the first language tag in the locale chain, or the default locale
|
|
2051
|
+
*/
|
|
2052
|
+
function createFormatter(context, IntlFormatter, locale, options, type) {
|
|
2053
|
+
try {
|
|
2054
|
+
return new IntlFormatter(locale, options);
|
|
2055
|
+
} catch (error) {
|
|
2056
|
+
if (isLanguageTag(locale)) throw error;
|
|
2057
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
2058
|
+
const target = localeFallbacker(context, fallbackLocale, locale).find(isLanguageTag) || "en-US";
|
|
2059
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_FROM_INVALID_LOCALE, {
|
|
2060
|
+
locale,
|
|
2061
|
+
target,
|
|
2062
|
+
type: type === "datetime format" ? "datetime" : "number"
|
|
2063
|
+
}));
|
|
2064
|
+
return new IntlFormatter(target, options);
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
/**
|
|
2068
|
+
* Format a value with the format of a key, or only with the overrides when no key is given
|
|
2069
|
+
*
|
|
2070
|
+
* @remarks
|
|
2071
|
+
* The common part of `datetime()` and `number()`, after they check and parse the arguments
|
|
2072
|
+
*/
|
|
2073
|
+
function formatValue(context, [key, value, options, overrides], IntlFormatter, formatters, formats, type) {
|
|
2074
|
+
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
2075
|
+
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
2076
|
+
const part = !!options.part;
|
|
2077
|
+
const locale = getLocale(context, options);
|
|
2078
|
+
if (!isString(key) || key === "") {
|
|
2079
|
+
const formatter = createFormatter(context, IntlFormatter, locale.replace(/!/g, ""), overrides, type);
|
|
2080
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2081
|
+
}
|
|
2082
|
+
const targetLocale = resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type);
|
|
2083
|
+
if (!isString(targetLocale)) return context.unresolving ? -1 : key;
|
|
2084
|
+
const format = formats[targetLocale][key];
|
|
2085
|
+
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
2086
|
+
let formatter = formatters.get(id);
|
|
2087
|
+
if (!formatter) {
|
|
2088
|
+
formatter = createFormatter(context, IntlFormatter, targetLocale, assign({}, format, overrides), type);
|
|
2089
|
+
formatters.set(id, formatter);
|
|
2090
|
+
}
|
|
2091
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2092
|
+
}
|
|
2019
2093
|
function getFormatterCacheKey(locale, key, overrides) {
|
|
2020
2094
|
let id = `${locale}__${key}`;
|
|
2021
2095
|
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
@@ -2050,7 +2124,7 @@ const Availabilities = {
|
|
|
2050
2124
|
//#endregion
|
|
2051
2125
|
//#region packages/core-base/src/datetime.ts
|
|
2052
2126
|
function datetime(context, ...args) {
|
|
2053
|
-
const { datetimeFormats,
|
|
2127
|
+
const { datetimeFormats, onWarn } = context;
|
|
2054
2128
|
const { __datetimeFormatters } = context;
|
|
2055
2129
|
if (!Availabilities.dateTimeFormat) {
|
|
2056
2130
|
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
@@ -2060,25 +2134,7 @@ function datetime(context, ...args) {
|
|
|
2060
2134
|
onWarn(getWarnMessage(CoreWarnCodes.INVALID_DATE_ARGUMENT, { value: String(args[0]) }));
|
|
2061
2135
|
return "";
|
|
2062
2136
|
}
|
|
2063
|
-
|
|
2064
|
-
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
2065
|
-
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
2066
|
-
const part = !!options.part;
|
|
2067
|
-
const locale = getLocale(context, options);
|
|
2068
|
-
if (!isString(key) || key === "") {
|
|
2069
|
-
const formatter = new Intl.DateTimeFormat(locale.replace(/!/g, ""), overrides);
|
|
2070
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2071
|
-
}
|
|
2072
|
-
const targetLocale = resolveFormatLocale(context, key, locale, datetimeFormats, missingWarn, fallbackWarn, "datetime format");
|
|
2073
|
-
if (!isString(targetLocale)) return unresolving ? -1 : key;
|
|
2074
|
-
const format = datetimeFormats[targetLocale][key];
|
|
2075
|
-
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
2076
|
-
let formatter = __datetimeFormatters.get(id);
|
|
2077
|
-
if (!formatter) {
|
|
2078
|
-
formatter = new Intl.DateTimeFormat(targetLocale, assign({}, format, overrides));
|
|
2079
|
-
__datetimeFormatters.set(id, formatter);
|
|
2080
|
-
}
|
|
2081
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2137
|
+
return formatValue(context, parseDateTimeArgs(...args), Intl.DateTimeFormat, __datetimeFormatters, datetimeFormats, "datetime format");
|
|
2082
2138
|
}
|
|
2083
2139
|
/** @internal */
|
|
2084
2140
|
const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
@@ -2139,7 +2195,7 @@ function clearDateTimeFormat(ctx, locale, format) {
|
|
|
2139
2195
|
//#endregion
|
|
2140
2196
|
//#region packages/core-base/src/number.ts
|
|
2141
2197
|
function number(context, ...args) {
|
|
2142
|
-
const { numberFormats,
|
|
2198
|
+
const { numberFormats, onWarn } = context;
|
|
2143
2199
|
const { __numberFormatters } = context;
|
|
2144
2200
|
if (!Availabilities.numberFormat) {
|
|
2145
2201
|
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
|
|
@@ -2149,25 +2205,7 @@ function number(context, ...args) {
|
|
|
2149
2205
|
onWarn(getWarnMessage(CoreWarnCodes.INVALID_NUMBER_ARGUMENT, { value: String(args[0]) }));
|
|
2150
2206
|
return "";
|
|
2151
2207
|
}
|
|
2152
|
-
|
|
2153
|
-
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
2154
|
-
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
2155
|
-
const part = !!options.part;
|
|
2156
|
-
const locale = getLocale(context, options);
|
|
2157
|
-
if (!isString(key) || key === "") {
|
|
2158
|
-
const formatter = new Intl.NumberFormat(locale.replace(/!/g, ""), overrides);
|
|
2159
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2160
|
-
}
|
|
2161
|
-
const targetLocale = resolveFormatLocale(context, key, locale, numberFormats, missingWarn, fallbackWarn, "number format");
|
|
2162
|
-
if (!isString(targetLocale)) return unresolving ? -1 : key;
|
|
2163
|
-
const format = numberFormats[targetLocale][key];
|
|
2164
|
-
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
2165
|
-
let formatter = __numberFormatters.get(id);
|
|
2166
|
-
if (!formatter) {
|
|
2167
|
-
formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
|
|
2168
|
-
__numberFormatters.set(id, formatter);
|
|
2169
|
-
}
|
|
2170
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2208
|
+
return formatValue(context, parseNumberArgs(...args), Intl.NumberFormat, __numberFormatters, numberFormats, "number format");
|
|
2171
2209
|
}
|
|
2172
2210
|
/** @internal */
|
|
2173
2211
|
const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
@@ -2232,14 +2270,25 @@ const PLURAL_CATEGORY_ORDER = [
|
|
|
2232
2270
|
"other"
|
|
2233
2271
|
];
|
|
2234
2272
|
const intlPluralRulesCache = /* @__PURE__ */ new Map();
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2273
|
+
const INTL_PLURAL_RULES_CACHE_SIZE = 1e3;
|
|
2274
|
+
function createIntlPluralRules(locale) {
|
|
2275
|
+
try {
|
|
2276
|
+
if (Intl.PluralRules.supportedLocalesOf(locale).length === 0) return null;
|
|
2238
2277
|
const rules = new Intl.PluralRules(locale);
|
|
2239
|
-
|
|
2278
|
+
return {
|
|
2240
2279
|
rules,
|
|
2241
2280
|
categories: rules.resolvedOptions().pluralCategories.slice().sort((a, b) => PLURAL_CATEGORY_ORDER.indexOf(a) - PLURAL_CATEGORY_ORDER.indexOf(b))
|
|
2242
2281
|
};
|
|
2282
|
+
} catch {
|
|
2283
|
+
return null;
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
function getIntlPluralRules(locale) {
|
|
2287
|
+
let cached = intlPluralRulesCache.get(locale);
|
|
2288
|
+
if (cached === void 0) {
|
|
2289
|
+
cached = createIntlPluralRules(locale);
|
|
2290
|
+
if (cached === null) warn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DEFAULT_PLURAL_RULE, { locale }));
|
|
2291
|
+
if (intlPluralRulesCache.size >= INTL_PLURAL_RULES_CACHE_SIZE) intlPluralRulesCache.delete(intlPluralRulesCache.keys().next().value);
|
|
2243
2292
|
intlPluralRulesCache.set(locale, cached);
|
|
2244
2293
|
}
|
|
2245
2294
|
return cached;
|
|
@@ -2252,10 +2301,11 @@ function createMessageContext(options = {}) {
|
|
|
2252
2301
|
const locale = options.locale;
|
|
2253
2302
|
const pluralIndex = getPluralIndex(options);
|
|
2254
2303
|
let resolvedPluralRule;
|
|
2255
|
-
if (isString(locale) && isFunction(options.pluralRules
|
|
2304
|
+
if (isString(locale) && options.pluralRules && isFunction(getOwn(options.pluralRules, locale))) resolvedPluralRule = options.pluralRules[locale];
|
|
2256
2305
|
else if (isString(locale) && typeof Intl !== "undefined" && typeof Intl.PluralRules !== "undefined") resolvedPluralRule = (choice, choicesLength) => {
|
|
2257
|
-
const
|
|
2258
|
-
if (choicesLength > categories.length) return pluralDefault(choice, choicesLength);
|
|
2306
|
+
const intlPluralRules = getIntlPluralRules(locale);
|
|
2307
|
+
if (intlPluralRules === null || choicesLength > intlPluralRules.categories.length) return pluralDefault(choice, choicesLength);
|
|
2308
|
+
const { rules, categories } = intlPluralRules;
|
|
2259
2309
|
const category = rules.select(Math.abs(choice));
|
|
2260
2310
|
const index = categories.indexOf(category);
|
|
2261
2311
|
return Math.min(index === -1 ? choicesLength - 1 : index, choicesLength - 1);
|
|
@@ -2266,16 +2316,16 @@ function createMessageContext(options = {}) {
|
|
|
2266
2316
|
const _list = options.list || [];
|
|
2267
2317
|
const list = (index) => _list[index];
|
|
2268
2318
|
const _named = options.named || create();
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
_named
|
|
2272
|
-
|
|
2273
|
-
|
|
2319
|
+
const hasPluralIndex = isNumber(options.pluralIndex);
|
|
2320
|
+
const named = (key) => {
|
|
2321
|
+
const value = isObjectPrototypeKey(key) ? getOwn(_named, key) : _named[key];
|
|
2322
|
+
return hasPluralIndex && value == null && (key === "count" || key === "n") ? options.pluralIndex : value;
|
|
2323
|
+
};
|
|
2274
2324
|
function message(key, useLinked) {
|
|
2275
2325
|
const msg = isFunction(options.messages) ? options.messages(key, !!useLinked) : isObject(options.messages) ? options.messages[key] : false;
|
|
2276
2326
|
return !msg ? options.parent ? options.parent.message(key) : DEFAULT_MESSAGE : msg;
|
|
2277
2327
|
}
|
|
2278
|
-
const _modifier = (name) => options.modifiers ? options.modifiers
|
|
2328
|
+
const _modifier = (name) => options.modifiers ? getOwn(options.modifiers, name) : DEFAULT_MODIFIER;
|
|
2279
2329
|
const normalize = isFunction(options.processor?.normalize) ? options.processor.normalize : DEFAULT_NORMALIZE;
|
|
2280
2330
|
const interpolate = isFunction(options.processor?.interpolate) ? options.processor.interpolate : DEFAULT_INTERPOLATE;
|
|
2281
2331
|
const type = isString(options.processor?.type) ? options.processor.type : DEFAULT_MESSAGE_DATA_TYPE;
|
|
@@ -2295,7 +2345,11 @@ function createMessageContext(options = {}) {
|
|
|
2295
2345
|
const ret = message(key, true)(ctx);
|
|
2296
2346
|
const resolved = ret === "" || ret === void 0 ? key : ret;
|
|
2297
2347
|
const msg = type === "vnode" && isArray(resolved) && modifier ? resolved[0] : resolved;
|
|
2298
|
-
|
|
2348
|
+
if (!modifier) return msg;
|
|
2349
|
+
const modify = _modifier(modifier);
|
|
2350
|
+
if (isFunction(modify)) return modify(msg, type);
|
|
2351
|
+
warnOnce(`Not found '${modifier}' modifier for the linked message of '${key}' key. Use a built-in modifier, or set a custom modifier with 'modifiers' option. So the linked message is used as is.`);
|
|
2352
|
+
return msg;
|
|
2299
2353
|
};
|
|
2300
2354
|
const ctx = {
|
|
2301
2355
|
["list"]: list,
|
|
@@ -2306,7 +2360,10 @@ function createMessageContext(options = {}) {
|
|
|
2306
2360
|
["type"]: type,
|
|
2307
2361
|
["interpolate"]: interpolate,
|
|
2308
2362
|
["normalize"]: normalize,
|
|
2309
|
-
["values"]: assign(create(), _list, _named
|
|
2363
|
+
["values"]: assign(create(), _list, _named, hasPluralIndex ? {
|
|
2364
|
+
count: named("count"),
|
|
2365
|
+
n: named("n")
|
|
2366
|
+
} : {})
|
|
2310
2367
|
};
|
|
2311
2368
|
return ctx;
|
|
2312
2369
|
}
|
|
@@ -2325,7 +2382,7 @@ function translate(context, ...args) {
|
|
|
2325
2382
|
let [format, targetLocale, message] = !resolvedMessage ? resolveMessageFormat(context, key, locale, context.fallbackLocale, isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn, isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn) : [
|
|
2326
2383
|
key,
|
|
2327
2384
|
locale,
|
|
2328
|
-
context.messages
|
|
2385
|
+
getOwn(context.messages, locale) || create()
|
|
2329
2386
|
];
|
|
2330
2387
|
let cacheBaseKey = key;
|
|
2331
2388
|
if (!resolvedMessage && !isMessageFormat(format)) {
|
|
@@ -2336,10 +2393,7 @@ function translate(context, ...args) {
|
|
|
2336
2393
|
}
|
|
2337
2394
|
}
|
|
2338
2395
|
if (!resolvedMessage && (!isMessageFormat(format) || !isString(targetLocale))) return context.unresolving ? -1 : key;
|
|
2339
|
-
if (isString(format) && context.messageCompiler == null) {
|
|
2340
|
-
warn(`The message format compilation is not supported in this build. Because message compiler isn't included. You need to pre-compilation all message format. So translate function return '${stringifyKey(key)}'.`);
|
|
2341
|
-
return key;
|
|
2342
|
-
}
|
|
2396
|
+
if (isString(format) && context.messageCompiler == null) warn(`Cannot compile the message of '${stringifyKey(key)}' key, because no message compiler is set. Use 'messageCompiler' option of 'createCoreContext', or pre-compile the message. So translate function returns the message as is.`);
|
|
2343
2397
|
let occurred = false;
|
|
2344
2398
|
const onError = () => {
|
|
2345
2399
|
occurred = true;
|
|
@@ -2347,7 +2401,14 @@ function translate(context, ...args) {
|
|
|
2347
2401
|
const msg = !isMessageFunction(format) ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) : format;
|
|
2348
2402
|
if (occurred) return format;
|
|
2349
2403
|
const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options));
|
|
2350
|
-
const
|
|
2404
|
+
const timeline = startTimelineMeasure(context, "evaluation");
|
|
2405
|
+
const messaged = msg(msgContext);
|
|
2406
|
+
if (timeline) timeline.emitter.emit("message-evaluation", {
|
|
2407
|
+
type: "message-evaluation",
|
|
2408
|
+
value: messaged,
|
|
2409
|
+
time: endTimelineMeasure(timeline),
|
|
2410
|
+
groupId: toDevtoolsGroupId("translate", msg.key)
|
|
2411
|
+
});
|
|
2351
2412
|
let ret = context.postTranslation ? context.postTranslation(messaged, key) : messaged;
|
|
2352
2413
|
if (escapeParameter && isString(ret)) ret = sanitizeTranslatedHtml(ret);
|
|
2353
2414
|
return ret;
|
|
@@ -2388,31 +2449,18 @@ function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn
|
|
|
2388
2449
|
groupId: toDevtoolsGroupId(type, key)
|
|
2389
2450
|
});
|
|
2390
2451
|
}
|
|
2391
|
-
message = messages
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
startTag = "intlify-message-resolve-start";
|
|
2398
|
-
endTag = "intlify-message-resolve-end";
|
|
2399
|
-
mark?.(startTag);
|
|
2400
|
-
}
|
|
2401
|
-
if ((format = resolveValue(message, key)) === null) format = message[key];
|
|
2402
|
-
if (inBrowser) {
|
|
2403
|
-
const end = window.performance.now();
|
|
2404
|
-
const emitter = context.__v_emitter;
|
|
2405
|
-
if (emitter && start && format) emitter.emit("message-resolve", {
|
|
2452
|
+
message = getOwn(messages, targetLocale) || create();
|
|
2453
|
+
const timeline = startTimelineMeasure(context, "resolve");
|
|
2454
|
+
if ((format = resolveValue(message, key)) === null) format = getOwn(message, key);
|
|
2455
|
+
if (timeline) {
|
|
2456
|
+
const time = endTimelineMeasure(timeline);
|
|
2457
|
+
if (format) timeline.emitter.emit("message-resolve", {
|
|
2406
2458
|
type: "message-resolve",
|
|
2407
2459
|
key,
|
|
2408
2460
|
message: format,
|
|
2409
|
-
time
|
|
2461
|
+
time,
|
|
2410
2462
|
groupId: toDevtoolsGroupId(type, key)
|
|
2411
2463
|
});
|
|
2412
|
-
if (startTag && endTag && mark && measure) {
|
|
2413
|
-
mark(endTag);
|
|
2414
|
-
measure("intlify message resolve", startTag, endTag);
|
|
2415
|
-
}
|
|
2416
2464
|
}
|
|
2417
2465
|
if (isMessageFormat(format)) break;
|
|
2418
2466
|
if (!isImplicitFallback(targetLocale, locales)) {
|
|
@@ -2443,55 +2491,40 @@ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
|
|
|
2443
2491
|
msg.key = key;
|
|
2444
2492
|
return msg;
|
|
2445
2493
|
}
|
|
2446
|
-
|
|
2447
|
-
let startTag;
|
|
2448
|
-
let endTag;
|
|
2449
|
-
if (inBrowser && mark) {
|
|
2450
|
-
start = window.performance.now();
|
|
2451
|
-
startTag = "intlify-message-compilation-start";
|
|
2452
|
-
endTag = "intlify-message-compilation-end";
|
|
2453
|
-
mark?.(startTag);
|
|
2454
|
-
}
|
|
2494
|
+
const timeline = startTimelineMeasure(context, "compilation");
|
|
2455
2495
|
const msg = context.messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, context.warnHtmlMessage, onError));
|
|
2456
|
-
if (
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
time: end - start,
|
|
2463
|
-
groupId: toDevtoolsGroupId("translate", key)
|
|
2464
|
-
});
|
|
2465
|
-
if (startTag && endTag && mark && measure) {
|
|
2466
|
-
mark(endTag);
|
|
2467
|
-
measure("intlify message compilation", startTag, endTag);
|
|
2468
|
-
}
|
|
2469
|
-
}
|
|
2496
|
+
if (timeline) timeline.emitter.emit("message-compilation", {
|
|
2497
|
+
type: "message-compilation",
|
|
2498
|
+
message: format,
|
|
2499
|
+
time: endTimelineMeasure(timeline),
|
|
2500
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
2501
|
+
});
|
|
2470
2502
|
msg.locale = targetLocale;
|
|
2471
2503
|
msg.key = key;
|
|
2472
2504
|
msg.source = format;
|
|
2473
2505
|
return msg;
|
|
2474
2506
|
}
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
const
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
context.__v_emitter?.emit("message-evaluation", {
|
|
2485
|
-
type: "message-evaluation",
|
|
2486
|
-
value: messaged,
|
|
2487
|
-
time: end - start,
|
|
2488
|
-
groupId: toDevtoolsGroupId("translate", msg.key)
|
|
2489
|
-
});
|
|
2490
|
-
mark(endTag);
|
|
2491
|
-
measure("intlify message evaluation", startTag, endTag);
|
|
2492
|
-
return messaged;
|
|
2507
|
+
function startTimelineMeasure(context, name) {
|
|
2508
|
+
const emitter = context.__v_emitter;
|
|
2509
|
+
if (!inBrowser || !emitter) return null;
|
|
2510
|
+
const start = window.performance.now();
|
|
2511
|
+
mark?.(`intlify-message-${name}-start`);
|
|
2512
|
+
return {
|
|
2513
|
+
emitter,
|
|
2514
|
+
name,
|
|
2515
|
+
start
|
|
2493
2516
|
};
|
|
2494
2517
|
}
|
|
2518
|
+
function endTimelineMeasure({ name, start }) {
|
|
2519
|
+
const time = window.performance.now() - start;
|
|
2520
|
+
if (mark && measure) {
|
|
2521
|
+
const startTag = `intlify-message-${name}-start`;
|
|
2522
|
+
const endTag = `intlify-message-${name}-end`;
|
|
2523
|
+
mark(endTag);
|
|
2524
|
+
measure(`intlify message ${name}`, startTag, endTag);
|
|
2525
|
+
}
|
|
2526
|
+
return time;
|
|
2527
|
+
}
|
|
2495
2528
|
/** @internal */
|
|
2496
2529
|
function parseTranslateArgs(...args) {
|
|
2497
2530
|
const [arg1, arg2, arg3] = args;
|
|
@@ -2566,4 +2599,4 @@ function getMessageContextOptions(context, locale, message, options) {
|
|
|
2566
2599
|
};
|
|
2567
2600
|
}
|
|
2568
2601
|
//#endregion
|
|
2569
|
-
export { AST_NODE_PROPS_KEYS, CORE_ERROR_CODES_EXTEND_POINT, CORE_WARN_CODES_EXTEND_POINT, CoreWarnCodes, DATETIME_FORMAT_OPTIONS_KEYS, DEFAULT_LOCALE, DEFAULT_MESSAGE_DATA_TYPE, MISSING_RESOLVE_VALUE, NOT_RESOLVED, NUMBER_FORMAT_OPTIONS_KEYS, VERSION, clearCompileCache, clearDateTimeFormat, clearNumberFormat, compile, createCompileError, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple, getLocale, getLocaleMessage, getWarnMessage, handleMissing, isAlmostSameLocale, isImplicitFallback, isMessageAST, isMessageFormat, isMessageFunction, isTranslateFallbackWarn, isTranslateMissingWarn, number, parse, parseDateTimeArgs, parseNumberArgs, parseTranslateArgs, registerLocaleFallbacker, registerMessageCompiler, registerMessageResolver, resolveLocale, resolveValue, resolveWithKeyValue, setLocaleMessage, translate, updateFallbackLocale };
|
|
2602
|
+
export { AST_NODE_PROPS_KEYS, CORE_ERROR_CODES_EXTEND_POINT, CORE_WARN_CODES_EXTEND_POINT, CoreWarnCodes, DATETIME_FORMAT_OPTIONS_KEYS, DEFAULT_LOCALE, DEFAULT_MESSAGE_DATA_TYPE, MISSING_RESOLVE_VALUE, NOT_RESOLVED, NUMBER_FORMAT_OPTIONS_KEYS, VERSION, clearCompileCache, clearDateTimeFormat, clearNumberFormat, compile, createCompileError, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple, getLocale, getLocaleMessage, getWarnMessage, handleMissing, isAlmostSameLocale, isFallbackLocale, isImplicitFallback, isMessageAST, isMessageFormat, isMessageFunction, isTranslateFallbackWarn, isTranslateMissingWarn, isWarnOption, number, parse, parseDateTimeArgs, parseNumberArgs, parseTranslateArgs, registerLocaleFallbacker, registerMessageCompiler, registerMessageResolver, resolveLocale, resolveValue, resolveWithKeyValue, setLocaleMessage, translate, updateFallbackLocale };
|