@intlify/core-base 10.0.0-beta.6 → 10.0.0
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-base.cjs +1233 -1233
- package/dist/core-base.d.ts +19 -4
- package/dist/core-base.esm-browser.js +1233 -1233
- package/dist/core-base.esm-browser.prod.js +2 -2
- package/dist/core-base.global.js +1233 -1233
- package/dist/core-base.global.prod.js +2 -2
- package/dist/core-base.mjs +1243 -1243
- package/dist/core-base.prod.cjs +1116 -1116
- package/package.json +4 -4
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* core-base v10.0.0
|
|
2
|
+
* core-base v10.0.0
|
|
3
3
|
* (c) 2024 kazuya kawaguchi
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -1654,402 +1654,146 @@ function baseCompile$1(source, options = {}) {
|
|
|
1654
1654
|
}
|
|
1655
1655
|
}
|
|
1656
1656
|
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
1661
|
-
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
|
|
1662
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
|
|
1663
|
-
};
|
|
1664
|
-
pathStateMachine[1 /* States.IN_PATH */] = {
|
|
1665
|
-
["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */],
|
|
1666
|
-
["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */],
|
|
1667
|
-
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
|
|
1668
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
|
|
1669
|
-
};
|
|
1670
|
-
pathStateMachine[2 /* States.BEFORE_IDENT */] = {
|
|
1671
|
-
["w" /* PathCharTypes.WORKSPACE */]: [2 /* States.BEFORE_IDENT */],
|
|
1672
|
-
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
1673
|
-
["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */]
|
|
1674
|
-
};
|
|
1675
|
-
pathStateMachine[3 /* States.IN_IDENT */] = {
|
|
1676
|
-
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
1677
|
-
["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
1678
|
-
["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */, 1 /* Actions.PUSH */],
|
|
1679
|
-
["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */, 1 /* Actions.PUSH */],
|
|
1680
|
-
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */, 1 /* Actions.PUSH */],
|
|
1681
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */, 1 /* Actions.PUSH */]
|
|
1682
|
-
};
|
|
1683
|
-
pathStateMachine[4 /* States.IN_SUB_PATH */] = {
|
|
1684
|
-
["'" /* PathCharTypes.SINGLE_QUOTE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */],
|
|
1685
|
-
["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */],
|
|
1686
|
-
["[" /* PathCharTypes.LEFT_BRACKET */]: [
|
|
1687
|
-
4 /* States.IN_SUB_PATH */,
|
|
1688
|
-
2 /* Actions.INC_SUB_PATH_DEPTH */
|
|
1689
|
-
],
|
|
1690
|
-
["]" /* PathCharTypes.RIGHT_BRACKET */]: [1 /* States.IN_PATH */, 3 /* Actions.PUSH_SUB_PATH */],
|
|
1691
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
1692
|
-
["l" /* PathCharTypes.ELSE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */]
|
|
1693
|
-
};
|
|
1694
|
-
pathStateMachine[5 /* States.IN_SINGLE_QUOTE */] = {
|
|
1695
|
-
["'" /* PathCharTypes.SINGLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
|
|
1696
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
1697
|
-
["l" /* PathCharTypes.ELSE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */]
|
|
1698
|
-
};
|
|
1699
|
-
pathStateMachine[6 /* States.IN_DOUBLE_QUOTE */] = {
|
|
1700
|
-
["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
|
|
1701
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
1702
|
-
["l" /* PathCharTypes.ELSE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */]
|
|
1703
|
-
};
|
|
1704
|
-
/**
|
|
1705
|
-
* Check if an expression is a literal value.
|
|
1706
|
-
*/
|
|
1707
|
-
const literalValueRE = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/;
|
|
1708
|
-
function isLiteral(exp) {
|
|
1709
|
-
return literalValueRE.test(exp);
|
|
1710
|
-
}
|
|
1711
|
-
/**
|
|
1712
|
-
* Strip quotes from a string
|
|
1713
|
-
*/
|
|
1714
|
-
function stripQuotes(str) {
|
|
1715
|
-
const a = str.charCodeAt(0);
|
|
1716
|
-
const b = str.charCodeAt(str.length - 1);
|
|
1717
|
-
return a === b && (a === 0x22 || a === 0x27) ? str.slice(1, -1) : str;
|
|
1657
|
+
function format(ast) {
|
|
1658
|
+
const msg = (ctx) => formatParts(ctx, ast);
|
|
1659
|
+
return msg;
|
|
1718
1660
|
}
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
*/
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
return
|
|
1661
|
+
function formatParts(ctx, ast) {
|
|
1662
|
+
const body = ast.b || ast.body;
|
|
1663
|
+
if ((body.t || body.type) === 1 /* NodeTypes.Plural */) {
|
|
1664
|
+
const plural = body;
|
|
1665
|
+
const cases = plural.c || plural.cases;
|
|
1666
|
+
return ctx.plural(cases.reduce((messages, c) => [
|
|
1667
|
+
...messages,
|
|
1668
|
+
formatMessageParts(ctx, c)
|
|
1669
|
+
], []));
|
|
1725
1670
|
}
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
case 0x5b: // [
|
|
1729
|
-
case 0x5d: // ]
|
|
1730
|
-
case 0x2e: // .
|
|
1731
|
-
case 0x22: // "
|
|
1732
|
-
case 0x27: // '
|
|
1733
|
-
return ch;
|
|
1734
|
-
case 0x5f: // _
|
|
1735
|
-
case 0x24: // $
|
|
1736
|
-
case 0x2d: // -
|
|
1737
|
-
return "i" /* PathCharTypes.IDENT */;
|
|
1738
|
-
case 0x09: // Tab (HT)
|
|
1739
|
-
case 0x0a: // Newline (LF)
|
|
1740
|
-
case 0x0d: // Return (CR)
|
|
1741
|
-
case 0xa0: // No-break space (NBSP)
|
|
1742
|
-
case 0xfeff: // Byte Order Mark (BOM)
|
|
1743
|
-
case 0x2028: // Line Separator (LS)
|
|
1744
|
-
case 0x2029: // Paragraph Separator (PS)
|
|
1745
|
-
return "w" /* PathCharTypes.WORKSPACE */;
|
|
1671
|
+
else {
|
|
1672
|
+
return formatMessageParts(ctx, body);
|
|
1746
1673
|
}
|
|
1747
|
-
return "i" /* PathCharTypes.IDENT */;
|
|
1748
1674
|
}
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
return
|
|
1675
|
+
function formatMessageParts(ctx, node) {
|
|
1676
|
+
const _static = node.s || node.static;
|
|
1677
|
+
if (_static) {
|
|
1678
|
+
return ctx.type === 'text'
|
|
1679
|
+
? _static
|
|
1680
|
+
: ctx.normalize([_static]);
|
|
1681
|
+
}
|
|
1682
|
+
else {
|
|
1683
|
+
const messages = (node.i || node.items).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
|
|
1684
|
+
return ctx.normalize(messages);
|
|
1759
1685
|
}
|
|
1760
|
-
return isLiteral(trimmed)
|
|
1761
|
-
? stripQuotes(trimmed)
|
|
1762
|
-
: "*" /* PathCharTypes.ASTARISK */ + trimmed;
|
|
1763
1686
|
}
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
let mode = 0 /* States.BEFORE_PATH */;
|
|
1771
|
-
let subPathDepth = 0;
|
|
1772
|
-
let c;
|
|
1773
|
-
let key; // eslint-disable-line
|
|
1774
|
-
let newChar;
|
|
1775
|
-
let type;
|
|
1776
|
-
let transition;
|
|
1777
|
-
let action;
|
|
1778
|
-
let typeMap;
|
|
1779
|
-
const actions = [];
|
|
1780
|
-
actions[0 /* Actions.APPEND */] = () => {
|
|
1781
|
-
if (key === undefined) {
|
|
1782
|
-
key = newChar;
|
|
1783
|
-
}
|
|
1784
|
-
else {
|
|
1785
|
-
key += newChar;
|
|
1786
|
-
}
|
|
1787
|
-
};
|
|
1788
|
-
actions[1 /* Actions.PUSH */] = () => {
|
|
1789
|
-
if (key !== undefined) {
|
|
1790
|
-
keys.push(key);
|
|
1791
|
-
key = undefined;
|
|
1792
|
-
}
|
|
1793
|
-
};
|
|
1794
|
-
actions[2 /* Actions.INC_SUB_PATH_DEPTH */] = () => {
|
|
1795
|
-
actions[0 /* Actions.APPEND */]();
|
|
1796
|
-
subPathDepth++;
|
|
1797
|
-
};
|
|
1798
|
-
actions[3 /* Actions.PUSH_SUB_PATH */] = () => {
|
|
1799
|
-
if (subPathDepth > 0) {
|
|
1800
|
-
subPathDepth--;
|
|
1801
|
-
mode = 4 /* States.IN_SUB_PATH */;
|
|
1802
|
-
actions[0 /* Actions.APPEND */]();
|
|
1687
|
+
function formatMessagePart(ctx, node) {
|
|
1688
|
+
const type = node.t || node.type;
|
|
1689
|
+
switch (type) {
|
|
1690
|
+
case 3 /* NodeTypes.Text */: {
|
|
1691
|
+
const text = node;
|
|
1692
|
+
return (text.v || text.value);
|
|
1803
1693
|
}
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
return false;
|
|
1808
|
-
}
|
|
1809
|
-
key = formatSubPath(key);
|
|
1810
|
-
if (key === false) {
|
|
1811
|
-
return false;
|
|
1812
|
-
}
|
|
1813
|
-
else {
|
|
1814
|
-
actions[1 /* Actions.PUSH */]();
|
|
1815
|
-
}
|
|
1694
|
+
case 9 /* NodeTypes.Literal */: {
|
|
1695
|
+
const literal = node;
|
|
1696
|
+
return (literal.v || literal.value);
|
|
1816
1697
|
}
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
if ((mode === 5 /* States.IN_SINGLE_QUOTE */ &&
|
|
1821
|
-
nextChar === "'" /* PathCharTypes.SINGLE_QUOTE */) ||
|
|
1822
|
-
(mode === 6 /* States.IN_DOUBLE_QUOTE */ &&
|
|
1823
|
-
nextChar === "\"" /* PathCharTypes.DOUBLE_QUOTE */)) {
|
|
1824
|
-
index++;
|
|
1825
|
-
newChar = '\\' + nextChar;
|
|
1826
|
-
actions[0 /* Actions.APPEND */]();
|
|
1827
|
-
return true;
|
|
1698
|
+
case 4 /* NodeTypes.Named */: {
|
|
1699
|
+
const named = node;
|
|
1700
|
+
return ctx.interpolate(ctx.named(named.k || named.key));
|
|
1828
1701
|
}
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
c = path[index];
|
|
1833
|
-
if (c === '\\' && maybeUnescapeQuote()) {
|
|
1834
|
-
continue;
|
|
1702
|
+
case 5 /* NodeTypes.List */: {
|
|
1703
|
+
const list = node;
|
|
1704
|
+
return ctx.interpolate(ctx.list(list.i != null ? list.i : list.index));
|
|
1835
1705
|
}
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
if (transition === 8 /* States.ERROR */) {
|
|
1841
|
-
return;
|
|
1706
|
+
case 6 /* NodeTypes.Linked */: {
|
|
1707
|
+
const linked = node;
|
|
1708
|
+
const modifier = linked.m || linked.modifier;
|
|
1709
|
+
return ctx.linked(formatMessagePart(ctx, linked.k || linked.key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
|
|
1842
1710
|
}
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
if (action) {
|
|
1847
|
-
newChar = c;
|
|
1848
|
-
if (action() === false) {
|
|
1849
|
-
return;
|
|
1850
|
-
}
|
|
1851
|
-
}
|
|
1711
|
+
case 7 /* NodeTypes.LinkedKey */: {
|
|
1712
|
+
const linkedKey = node;
|
|
1713
|
+
return (linkedKey.v || linkedKey.value);
|
|
1852
1714
|
}
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
return
|
|
1715
|
+
case 8 /* NodeTypes.LinkedModifier */: {
|
|
1716
|
+
const linkedModifier = node;
|
|
1717
|
+
return (linkedModifier.v || linkedModifier.value);
|
|
1856
1718
|
}
|
|
1719
|
+
default:
|
|
1720
|
+
throw new Error(`unhandled node type on format message part: ${type}`);
|
|
1857
1721
|
}
|
|
1858
1722
|
}
|
|
1859
|
-
// path token cache
|
|
1860
|
-
const cache = new Map();
|
|
1861
|
-
/**
|
|
1862
|
-
* key-value message resolver
|
|
1863
|
-
*
|
|
1864
|
-
* @remarks
|
|
1865
|
-
* Resolves messages with the key-value structure. Note that messages with a hierarchical structure such as objects cannot be resolved
|
|
1866
|
-
*
|
|
1867
|
-
* @param obj - A target object to be resolved with path
|
|
1868
|
-
* @param path - A {@link Path | path} to resolve the value of message
|
|
1869
|
-
*
|
|
1870
|
-
* @returns A resolved {@link PathValue | path value}
|
|
1871
|
-
*
|
|
1872
|
-
* @VueI18nGeneral
|
|
1873
|
-
*/
|
|
1874
|
-
function resolveWithKeyValue(obj, path) {
|
|
1875
|
-
return isObject(obj) ? obj[path] : null;
|
|
1876
|
-
}
|
|
1877
|
-
/**
|
|
1878
|
-
* message resolver
|
|
1879
|
-
*
|
|
1880
|
-
* @remarks
|
|
1881
|
-
* Resolves messages. messages with a hierarchical structure such as objects can be resolved. This resolver is used in VueI18n as default.
|
|
1882
|
-
*
|
|
1883
|
-
* @param obj - A target object to be resolved with path
|
|
1884
|
-
* @param path - A {@link Path | path} to resolve the value of message
|
|
1885
|
-
*
|
|
1886
|
-
* @returns A resolved {@link PathValue | path value}
|
|
1887
|
-
*
|
|
1888
|
-
* @VueI18nGeneral
|
|
1889
|
-
*/
|
|
1890
|
-
function resolveValue(obj, path) {
|
|
1891
|
-
// check object
|
|
1892
|
-
if (!isObject(obj)) {
|
|
1893
|
-
return null;
|
|
1894
|
-
}
|
|
1895
|
-
// parse path
|
|
1896
|
-
let hit = cache.get(path);
|
|
1897
|
-
if (!hit) {
|
|
1898
|
-
hit = parse(path);
|
|
1899
|
-
if (hit) {
|
|
1900
|
-
cache.set(path, hit);
|
|
1901
|
-
}
|
|
1902
|
-
}
|
|
1903
|
-
// check hit
|
|
1904
|
-
if (!hit) {
|
|
1905
|
-
return null;
|
|
1906
|
-
}
|
|
1907
|
-
// resolve path value
|
|
1908
|
-
const len = hit.length;
|
|
1909
|
-
let last = obj;
|
|
1910
|
-
let i = 0;
|
|
1911
|
-
while (i < len) {
|
|
1912
|
-
const val = last[hit[i]];
|
|
1913
|
-
if (val === undefined) {
|
|
1914
|
-
return null;
|
|
1915
|
-
}
|
|
1916
|
-
if (isFunction(last)) {
|
|
1917
|
-
return null;
|
|
1918
|
-
}
|
|
1919
|
-
last = val;
|
|
1920
|
-
i++;
|
|
1921
|
-
}
|
|
1922
|
-
return last;
|
|
1923
|
-
}
|
|
1924
1723
|
|
|
1925
|
-
const
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
const DEFAULT_INTERPOLATE = toDisplayString;
|
|
1930
|
-
function pluralDefault(choice, choicesLength) {
|
|
1931
|
-
choice = Math.abs(choice);
|
|
1932
|
-
if (choicesLength === 2) {
|
|
1933
|
-
// prettier-ignore
|
|
1934
|
-
return choice
|
|
1935
|
-
? choice > 1
|
|
1936
|
-
? 1
|
|
1937
|
-
: 0
|
|
1938
|
-
: 1;
|
|
1724
|
+
const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
|
|
1725
|
+
function checkHtmlMessage(source, warnHtmlMessage) {
|
|
1726
|
+
if (warnHtmlMessage && detectHtmlTag(source)) {
|
|
1727
|
+
warn(format$1(WARN_MESSAGE, { source }));
|
|
1939
1728
|
}
|
|
1940
|
-
return choice ? Math.min(choice, 2) : 0;
|
|
1941
|
-
}
|
|
1942
|
-
function getPluralIndex(options) {
|
|
1943
|
-
// prettier-ignore
|
|
1944
|
-
const index = isNumber(options.pluralIndex)
|
|
1945
|
-
? options.pluralIndex
|
|
1946
|
-
: -1;
|
|
1947
|
-
// prettier-ignore
|
|
1948
|
-
return options.named && (isNumber(options.named.count) || isNumber(options.named.n))
|
|
1949
|
-
? isNumber(options.named.count)
|
|
1950
|
-
? options.named.count
|
|
1951
|
-
: isNumber(options.named.n)
|
|
1952
|
-
? options.named.n
|
|
1953
|
-
: index
|
|
1954
|
-
: index;
|
|
1955
1729
|
}
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
if (!props.n) {
|
|
1961
|
-
props.n = pluralIndex;
|
|
1962
|
-
}
|
|
1730
|
+
const defaultOnCacheKey = (message) => message;
|
|
1731
|
+
let compileCache = Object.create(null);
|
|
1732
|
+
function clearCompileCache() {
|
|
1733
|
+
compileCache = Object.create(null);
|
|
1963
1734
|
}
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
isFunction(options.pluralRules[locale])
|
|
1975
|
-
? pluralDefault
|
|
1976
|
-
: undefined;
|
|
1977
|
-
const plural = (messages) => {
|
|
1978
|
-
return messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
|
|
1735
|
+
const isMessageAST = (val) => isObject(val) &&
|
|
1736
|
+
(val.t === 0 || val.type === 0) &&
|
|
1737
|
+
('b' in val || 'body' in val);
|
|
1738
|
+
function baseCompile(message, options = {}) {
|
|
1739
|
+
// error detecting on compile
|
|
1740
|
+
let detectError = false;
|
|
1741
|
+
const onError = options.onError || defaultOnError;
|
|
1742
|
+
options.onError = (err) => {
|
|
1743
|
+
detectError = true;
|
|
1744
|
+
onError(err);
|
|
1979
1745
|
};
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1746
|
+
// compile with mesasge-compiler
|
|
1747
|
+
return { ...baseCompile$1(message, options), detectError };
|
|
1748
|
+
}
|
|
1749
|
+
/* #__NO_SIDE_EFFECTS__ */
|
|
1750
|
+
function compile(message, context) {
|
|
1751
|
+
if (isString(message)) {
|
|
1752
|
+
// check HTML message
|
|
1753
|
+
const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
|
|
1754
|
+
? context.warnHtmlMessage
|
|
1755
|
+
: true;
|
|
1756
|
+
checkHtmlMessage(message, warnHtmlMessage);
|
|
1757
|
+
// check caches
|
|
1758
|
+
const onCacheKey = context.onCacheKey || defaultOnCacheKey;
|
|
1759
|
+
const cacheKey = onCacheKey(message);
|
|
1760
|
+
const cached = compileCache[cacheKey];
|
|
1761
|
+
if (cached) {
|
|
1762
|
+
return cached;
|
|
1763
|
+
}
|
|
1764
|
+
// compile with JIT mode
|
|
1765
|
+
const { ast, detectError } = baseCompile(message, {
|
|
1766
|
+
...context,
|
|
1767
|
+
location: true,
|
|
1768
|
+
jit: true
|
|
1769
|
+
});
|
|
1770
|
+
// compose message function from AST
|
|
1771
|
+
const msg = format(ast);
|
|
1772
|
+
// if occurred compile error, don't cache
|
|
1773
|
+
return !detectError
|
|
1774
|
+
? (compileCache[cacheKey] = msg)
|
|
1997
1775
|
: msg;
|
|
1998
1776
|
}
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
? options.processor.normalize
|
|
2004
|
-
: DEFAULT_NORMALIZE;
|
|
2005
|
-
const interpolate = isPlainObject(options.processor) &&
|
|
2006
|
-
isFunction(options.processor.interpolate)
|
|
2007
|
-
? options.processor.interpolate
|
|
2008
|
-
: DEFAULT_INTERPOLATE;
|
|
2009
|
-
const type = isPlainObject(options.processor) && isString(options.processor.type)
|
|
2010
|
-
? options.processor.type
|
|
2011
|
-
: DEFAULT_MESSAGE_DATA_TYPE;
|
|
2012
|
-
const linked = (key, ...args) => {
|
|
2013
|
-
const [arg1, arg2] = args;
|
|
2014
|
-
let type = 'text';
|
|
2015
|
-
let modifier = '';
|
|
2016
|
-
if (args.length === 1) {
|
|
2017
|
-
if (isObject(arg1)) {
|
|
2018
|
-
modifier = arg1.modifier || modifier;
|
|
2019
|
-
type = arg1.type || type;
|
|
2020
|
-
}
|
|
2021
|
-
else if (isString(arg1)) {
|
|
2022
|
-
modifier = arg1 || modifier;
|
|
2023
|
-
}
|
|
1777
|
+
else {
|
|
1778
|
+
if (!isMessageAST(message)) {
|
|
1779
|
+
warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
|
|
1780
|
+
return (() => message);
|
|
2024
1781
|
}
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
if (
|
|
2030
|
-
|
|
1782
|
+
// AST case (passed from bundler)
|
|
1783
|
+
const cacheKey = message.cacheKey;
|
|
1784
|
+
if (cacheKey) {
|
|
1785
|
+
const cached = compileCache[cacheKey];
|
|
1786
|
+
if (cached) {
|
|
1787
|
+
return cached;
|
|
2031
1788
|
}
|
|
1789
|
+
// compose message function from message (AST)
|
|
1790
|
+
return (compileCache[cacheKey] =
|
|
1791
|
+
format(message));
|
|
2032
1792
|
}
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
? ret[0]
|
|
2038
|
-
: ret;
|
|
2039
|
-
return modifier ? _modifier(modifier)(msg, type) : msg;
|
|
2040
|
-
};
|
|
2041
|
-
const ctx = {
|
|
2042
|
-
["list" /* HelperNameMap.LIST */]: list,
|
|
2043
|
-
["named" /* HelperNameMap.NAMED */]: named,
|
|
2044
|
-
["plural" /* HelperNameMap.PLURAL */]: plural,
|
|
2045
|
-
["linked" /* HelperNameMap.LINKED */]: linked,
|
|
2046
|
-
["message" /* HelperNameMap.MESSAGE */]: message,
|
|
2047
|
-
["type" /* HelperNameMap.TYPE */]: type,
|
|
2048
|
-
["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
|
|
2049
|
-
["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
|
|
2050
|
-
["values" /* HelperNameMap.VALUES */]: assign({}, _list, _named)
|
|
2051
|
-
};
|
|
2052
|
-
return ctx;
|
|
1793
|
+
else {
|
|
1794
|
+
return format(message);
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
2053
1797
|
}
|
|
2054
1798
|
|
|
2055
1799
|
let devtools = null;
|
|
@@ -2075,30 +1819,6 @@ function createDevToolsHook(hook) {
|
|
|
2075
1819
|
return (payloads) => devtools && devtools.emit(hook, payloads);
|
|
2076
1820
|
}
|
|
2077
1821
|
|
|
2078
|
-
const CoreWarnCodes = {
|
|
2079
|
-
NOT_FOUND_KEY: 1,
|
|
2080
|
-
FALLBACK_TO_TRANSLATE: 2,
|
|
2081
|
-
CANNOT_FORMAT_NUMBER: 3,
|
|
2082
|
-
FALLBACK_TO_NUMBER_FORMAT: 4,
|
|
2083
|
-
CANNOT_FORMAT_DATE: 5,
|
|
2084
|
-
FALLBACK_TO_DATE_FORMAT: 6,
|
|
2085
|
-
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7
|
|
2086
|
-
};
|
|
2087
|
-
const CORE_WARN_CODES_EXTEND_POINT = 8;
|
|
2088
|
-
/** @internal */
|
|
2089
|
-
const warnMessages = {
|
|
2090
|
-
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale messages.`,
|
|
2091
|
-
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
2092
|
-
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
2093
|
-
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
2094
|
-
[CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
|
|
2095
|
-
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
2096
|
-
[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.`
|
|
2097
|
-
};
|
|
2098
|
-
function getWarnMessage(code, ...args) {
|
|
2099
|
-
return format$1(warnMessages[code], ...args);
|
|
2100
|
-
}
|
|
2101
|
-
|
|
2102
1822
|
const CoreErrorCodes = {
|
|
2103
1823
|
INVALID_ARGUMENT: COMPILE_ERROR_CODES_EXTEND_POINT, // 17
|
|
2104
1824
|
INVALID_DATE_ARGUMENT: 18,
|
|
@@ -2272,64 +1992,356 @@ function appendItemToChain(chain, target, blocks) {
|
|
|
2272
1992
|
return follow;
|
|
2273
1993
|
}
|
|
2274
1994
|
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
1995
|
+
const pathStateMachine = [];
|
|
1996
|
+
pathStateMachine[0 /* States.BEFORE_PATH */] = {
|
|
1997
|
+
["w" /* PathCharTypes.WORKSPACE */]: [0 /* States.BEFORE_PATH */],
|
|
1998
|
+
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
1999
|
+
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
|
|
2000
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
|
|
2001
|
+
};
|
|
2002
|
+
pathStateMachine[1 /* States.IN_PATH */] = {
|
|
2003
|
+
["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */],
|
|
2004
|
+
["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */],
|
|
2005
|
+
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
|
|
2006
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
|
|
2007
|
+
};
|
|
2008
|
+
pathStateMachine[2 /* States.BEFORE_IDENT */] = {
|
|
2009
|
+
["w" /* PathCharTypes.WORKSPACE */]: [2 /* States.BEFORE_IDENT */],
|
|
2010
|
+
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
2011
|
+
["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */]
|
|
2012
|
+
};
|
|
2013
|
+
pathStateMachine[3 /* States.IN_IDENT */] = {
|
|
2014
|
+
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
2015
|
+
["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
2016
|
+
["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */, 1 /* Actions.PUSH */],
|
|
2017
|
+
["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */, 1 /* Actions.PUSH */],
|
|
2018
|
+
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */, 1 /* Actions.PUSH */],
|
|
2019
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */, 1 /* Actions.PUSH */]
|
|
2020
|
+
};
|
|
2021
|
+
pathStateMachine[4 /* States.IN_SUB_PATH */] = {
|
|
2022
|
+
["'" /* PathCharTypes.SINGLE_QUOTE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */],
|
|
2023
|
+
["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */],
|
|
2024
|
+
["[" /* PathCharTypes.LEFT_BRACKET */]: [
|
|
2025
|
+
4 /* States.IN_SUB_PATH */,
|
|
2026
|
+
2 /* Actions.INC_SUB_PATH_DEPTH */
|
|
2027
|
+
],
|
|
2028
|
+
["]" /* PathCharTypes.RIGHT_BRACKET */]: [1 /* States.IN_PATH */, 3 /* Actions.PUSH_SUB_PATH */],
|
|
2029
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
2030
|
+
["l" /* PathCharTypes.ELSE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */]
|
|
2031
|
+
};
|
|
2032
|
+
pathStateMachine[5 /* States.IN_SINGLE_QUOTE */] = {
|
|
2033
|
+
["'" /* PathCharTypes.SINGLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
|
|
2034
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
2035
|
+
["l" /* PathCharTypes.ELSE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */]
|
|
2036
|
+
};
|
|
2037
|
+
pathStateMachine[6 /* States.IN_DOUBLE_QUOTE */] = {
|
|
2038
|
+
["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
|
|
2039
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
2040
|
+
["l" /* PathCharTypes.ELSE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */]
|
|
2041
|
+
};
|
|
2042
|
+
/**
|
|
2043
|
+
* Check if an expression is a literal value.
|
|
2279
2044
|
*/
|
|
2280
|
-
const
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2045
|
+
const literalValueRE = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/;
|
|
2046
|
+
function isLiteral(exp) {
|
|
2047
|
+
return literalValueRE.test(exp);
|
|
2048
|
+
}
|
|
2049
|
+
/**
|
|
2050
|
+
* Strip quotes from a string
|
|
2051
|
+
*/
|
|
2052
|
+
function stripQuotes(str) {
|
|
2053
|
+
const a = str.charCodeAt(0);
|
|
2054
|
+
const b = str.charCodeAt(str.length - 1);
|
|
2055
|
+
return a === b && (a === 0x22 || a === 0x27) ? str.slice(1, -1) : str;
|
|
2056
|
+
}
|
|
2057
|
+
/**
|
|
2058
|
+
* Determine the type of a character in a keypath.
|
|
2059
|
+
*/
|
|
2060
|
+
function getPathCharType(ch) {
|
|
2061
|
+
if (ch === undefined || ch === null) {
|
|
2062
|
+
return "o" /* PathCharTypes.END_OF_FAIL */;
|
|
2063
|
+
}
|
|
2064
|
+
const code = ch.charCodeAt(0);
|
|
2065
|
+
switch (code) {
|
|
2066
|
+
case 0x5b: // [
|
|
2067
|
+
case 0x5d: // ]
|
|
2068
|
+
case 0x2e: // .
|
|
2069
|
+
case 0x22: // "
|
|
2070
|
+
case 0x27: // '
|
|
2071
|
+
return ch;
|
|
2072
|
+
case 0x5f: // _
|
|
2073
|
+
case 0x24: // $
|
|
2074
|
+
case 0x2d: // -
|
|
2075
|
+
return "i" /* PathCharTypes.IDENT */;
|
|
2076
|
+
case 0x09: // Tab (HT)
|
|
2077
|
+
case 0x0a: // Newline (LF)
|
|
2078
|
+
case 0x0d: // Return (CR)
|
|
2079
|
+
case 0xa0: // No-break space (NBSP)
|
|
2080
|
+
case 0xfeff: // Byte Order Mark (BOM)
|
|
2081
|
+
case 0x2028: // Line Separator (LS)
|
|
2082
|
+
case 0x2029: // Paragraph Separator (PS)
|
|
2083
|
+
return "w" /* PathCharTypes.WORKSPACE */;
|
|
2084
|
+
}
|
|
2085
|
+
return "i" /* PathCharTypes.IDENT */;
|
|
2086
|
+
}
|
|
2087
|
+
/**
|
|
2088
|
+
* Format a subPath, return its plain form if it is
|
|
2089
|
+
* a literal string or number. Otherwise prepend the
|
|
2090
|
+
* dynamic indicator (*).
|
|
2091
|
+
*/
|
|
2092
|
+
function formatSubPath(path) {
|
|
2093
|
+
const trimmed = path.trim();
|
|
2094
|
+
// invalid leading 0
|
|
2095
|
+
if (path.charAt(0) === '0' && isNaN(parseInt(path))) {
|
|
2096
|
+
return false;
|
|
2097
|
+
}
|
|
2098
|
+
return isLiteral(trimmed)
|
|
2099
|
+
? stripQuotes(trimmed)
|
|
2100
|
+
: "*" /* PathCharTypes.ASTARISK */ + trimmed;
|
|
2101
|
+
}
|
|
2102
|
+
/**
|
|
2103
|
+
* Parse a string path into an array of segments
|
|
2104
|
+
*/
|
|
2105
|
+
function parse(path) {
|
|
2106
|
+
const keys = [];
|
|
2107
|
+
let index = -1;
|
|
2108
|
+
let mode = 0 /* States.BEFORE_PATH */;
|
|
2109
|
+
let subPathDepth = 0;
|
|
2110
|
+
let c;
|
|
2111
|
+
let key; // eslint-disable-line
|
|
2112
|
+
let newChar;
|
|
2113
|
+
let type;
|
|
2114
|
+
let transition;
|
|
2115
|
+
let action;
|
|
2116
|
+
let typeMap;
|
|
2117
|
+
const actions = [];
|
|
2118
|
+
actions[0 /* Actions.APPEND */] = () => {
|
|
2119
|
+
if (key === undefined) {
|
|
2120
|
+
key = newChar;
|
|
2121
|
+
}
|
|
2122
|
+
else {
|
|
2123
|
+
key += newChar;
|
|
2310
2124
|
}
|
|
2311
2125
|
};
|
|
2126
|
+
actions[1 /* Actions.PUSH */] = () => {
|
|
2127
|
+
if (key !== undefined) {
|
|
2128
|
+
keys.push(key);
|
|
2129
|
+
key = undefined;
|
|
2130
|
+
}
|
|
2131
|
+
};
|
|
2132
|
+
actions[2 /* Actions.INC_SUB_PATH_DEPTH */] = () => {
|
|
2133
|
+
actions[0 /* Actions.APPEND */]();
|
|
2134
|
+
subPathDepth++;
|
|
2135
|
+
};
|
|
2136
|
+
actions[3 /* Actions.PUSH_SUB_PATH */] = () => {
|
|
2137
|
+
if (subPathDepth > 0) {
|
|
2138
|
+
subPathDepth--;
|
|
2139
|
+
mode = 4 /* States.IN_SUB_PATH */;
|
|
2140
|
+
actions[0 /* Actions.APPEND */]();
|
|
2141
|
+
}
|
|
2142
|
+
else {
|
|
2143
|
+
subPathDepth = 0;
|
|
2144
|
+
if (key === undefined) {
|
|
2145
|
+
return false;
|
|
2146
|
+
}
|
|
2147
|
+
key = formatSubPath(key);
|
|
2148
|
+
if (key === false) {
|
|
2149
|
+
return false;
|
|
2150
|
+
}
|
|
2151
|
+
else {
|
|
2152
|
+
actions[1 /* Actions.PUSH */]();
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
};
|
|
2156
|
+
function maybeUnescapeQuote() {
|
|
2157
|
+
const nextChar = path[index + 1];
|
|
2158
|
+
if ((mode === 5 /* States.IN_SINGLE_QUOTE */ &&
|
|
2159
|
+
nextChar === "'" /* PathCharTypes.SINGLE_QUOTE */) ||
|
|
2160
|
+
(mode === 6 /* States.IN_DOUBLE_QUOTE */ &&
|
|
2161
|
+
nextChar === "\"" /* PathCharTypes.DOUBLE_QUOTE */)) {
|
|
2162
|
+
index++;
|
|
2163
|
+
newChar = '\\' + nextChar;
|
|
2164
|
+
actions[0 /* Actions.APPEND */]();
|
|
2165
|
+
return true;
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
while (mode !== null) {
|
|
2169
|
+
index++;
|
|
2170
|
+
c = path[index];
|
|
2171
|
+
if (c === '\\' && maybeUnescapeQuote()) {
|
|
2172
|
+
continue;
|
|
2173
|
+
}
|
|
2174
|
+
type = getPathCharType(c);
|
|
2175
|
+
typeMap = pathStateMachine[mode];
|
|
2176
|
+
transition = typeMap[type] || typeMap["l" /* PathCharTypes.ELSE */] || 8 /* States.ERROR */;
|
|
2177
|
+
// check parse error
|
|
2178
|
+
if (transition === 8 /* States.ERROR */) {
|
|
2179
|
+
return;
|
|
2180
|
+
}
|
|
2181
|
+
mode = transition[0];
|
|
2182
|
+
if (transition[1] !== undefined) {
|
|
2183
|
+
action = actions[transition[1]];
|
|
2184
|
+
if (action) {
|
|
2185
|
+
newChar = c;
|
|
2186
|
+
if (action() === false) {
|
|
2187
|
+
return;
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
// check parse finish
|
|
2192
|
+
if (mode === 7 /* States.AFTER_PATH */) {
|
|
2193
|
+
return keys;
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2312
2196
|
}
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
_compiler = compiler;
|
|
2316
|
-
}
|
|
2317
|
-
let _resolver;
|
|
2197
|
+
// path token cache
|
|
2198
|
+
const cache = new Map();
|
|
2318
2199
|
/**
|
|
2319
|
-
*
|
|
2200
|
+
* key-value message resolver
|
|
2320
2201
|
*
|
|
2321
|
-
* @
|
|
2202
|
+
* @remarks
|
|
2203
|
+
* Resolves messages with the key-value structure. Note that messages with a hierarchical structure such as objects cannot be resolved
|
|
2204
|
+
*
|
|
2205
|
+
* @param obj - A target object to be resolved with path
|
|
2206
|
+
* @param path - A {@link Path | path} to resolve the value of message
|
|
2207
|
+
*
|
|
2208
|
+
* @returns A resolved {@link PathValue | path value}
|
|
2322
2209
|
*
|
|
2323
2210
|
* @VueI18nGeneral
|
|
2324
2211
|
*/
|
|
2325
|
-
function
|
|
2326
|
-
|
|
2212
|
+
function resolveWithKeyValue(obj, path) {
|
|
2213
|
+
return isObject(obj) ? obj[path] : null;
|
|
2327
2214
|
}
|
|
2328
|
-
let _fallbacker;
|
|
2329
2215
|
/**
|
|
2330
|
-
*
|
|
2216
|
+
* message resolver
|
|
2331
2217
|
*
|
|
2332
|
-
* @
|
|
2218
|
+
* @remarks
|
|
2219
|
+
* Resolves messages. messages with a hierarchical structure such as objects can be resolved. This resolver is used in VueI18n as default.
|
|
2220
|
+
*
|
|
2221
|
+
* @param obj - A target object to be resolved with path
|
|
2222
|
+
* @param path - A {@link Path | path} to resolve the value of message
|
|
2223
|
+
*
|
|
2224
|
+
* @returns A resolved {@link PathValue | path value}
|
|
2225
|
+
*
|
|
2226
|
+
* @VueI18nGeneral
|
|
2227
|
+
*/
|
|
2228
|
+
function resolveValue(obj, path) {
|
|
2229
|
+
// check object
|
|
2230
|
+
if (!isObject(obj)) {
|
|
2231
|
+
return null;
|
|
2232
|
+
}
|
|
2233
|
+
// parse path
|
|
2234
|
+
let hit = cache.get(path);
|
|
2235
|
+
if (!hit) {
|
|
2236
|
+
hit = parse(path);
|
|
2237
|
+
if (hit) {
|
|
2238
|
+
cache.set(path, hit);
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
// check hit
|
|
2242
|
+
if (!hit) {
|
|
2243
|
+
return null;
|
|
2244
|
+
}
|
|
2245
|
+
// resolve path value
|
|
2246
|
+
const len = hit.length;
|
|
2247
|
+
let last = obj;
|
|
2248
|
+
let i = 0;
|
|
2249
|
+
while (i < len) {
|
|
2250
|
+
const val = last[hit[i]];
|
|
2251
|
+
if (val === undefined) {
|
|
2252
|
+
return null;
|
|
2253
|
+
}
|
|
2254
|
+
if (isFunction(last)) {
|
|
2255
|
+
return null;
|
|
2256
|
+
}
|
|
2257
|
+
last = val;
|
|
2258
|
+
i++;
|
|
2259
|
+
}
|
|
2260
|
+
return last;
|
|
2261
|
+
}
|
|
2262
|
+
|
|
2263
|
+
const CoreWarnCodes = {
|
|
2264
|
+
NOT_FOUND_KEY: 1,
|
|
2265
|
+
FALLBACK_TO_TRANSLATE: 2,
|
|
2266
|
+
CANNOT_FORMAT_NUMBER: 3,
|
|
2267
|
+
FALLBACK_TO_NUMBER_FORMAT: 4,
|
|
2268
|
+
CANNOT_FORMAT_DATE: 5,
|
|
2269
|
+
FALLBACK_TO_DATE_FORMAT: 6,
|
|
2270
|
+
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7
|
|
2271
|
+
};
|
|
2272
|
+
const CORE_WARN_CODES_EXTEND_POINT = 8;
|
|
2273
|
+
/** @internal */
|
|
2274
|
+
const warnMessages = {
|
|
2275
|
+
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale messages.`,
|
|
2276
|
+
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
2277
|
+
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
2278
|
+
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
2279
|
+
[CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
|
|
2280
|
+
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
2281
|
+
[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.`
|
|
2282
|
+
};
|
|
2283
|
+
function getWarnMessage(code, ...args) {
|
|
2284
|
+
return format$1(warnMessages[code], ...args);
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2288
|
+
/**
|
|
2289
|
+
* Intlify core-base version
|
|
2290
|
+
* @internal
|
|
2291
|
+
*/
|
|
2292
|
+
const VERSION = '10.0.0';
|
|
2293
|
+
const NOT_REOSLVED = -1;
|
|
2294
|
+
const DEFAULT_LOCALE = 'en-US';
|
|
2295
|
+
const MISSING_RESOLVE_VALUE = '';
|
|
2296
|
+
const capitalize = (str) => `${str.charAt(0).toLocaleUpperCase()}${str.substr(1)}`;
|
|
2297
|
+
function getDefaultLinkedModifiers() {
|
|
2298
|
+
return {
|
|
2299
|
+
upper: (val, type) => {
|
|
2300
|
+
// prettier-ignore
|
|
2301
|
+
return type === 'text' && isString(val)
|
|
2302
|
+
? val.toUpperCase()
|
|
2303
|
+
: type === 'vnode' && isObject(val) && '__v_isVNode' in val
|
|
2304
|
+
? val.children.toUpperCase()
|
|
2305
|
+
: val;
|
|
2306
|
+
},
|
|
2307
|
+
lower: (val, type) => {
|
|
2308
|
+
// prettier-ignore
|
|
2309
|
+
return type === 'text' && isString(val)
|
|
2310
|
+
? val.toLowerCase()
|
|
2311
|
+
: type === 'vnode' && isObject(val) && '__v_isVNode' in val
|
|
2312
|
+
? val.children.toLowerCase()
|
|
2313
|
+
: val;
|
|
2314
|
+
},
|
|
2315
|
+
capitalize: (val, type) => {
|
|
2316
|
+
// prettier-ignore
|
|
2317
|
+
return (type === 'text' && isString(val)
|
|
2318
|
+
? capitalize(val)
|
|
2319
|
+
: type === 'vnode' && isObject(val) && '__v_isVNode' in val
|
|
2320
|
+
? capitalize(val.children)
|
|
2321
|
+
: val);
|
|
2322
|
+
}
|
|
2323
|
+
};
|
|
2324
|
+
}
|
|
2325
|
+
let _compiler;
|
|
2326
|
+
function registerMessageCompiler(compiler) {
|
|
2327
|
+
_compiler = compiler;
|
|
2328
|
+
}
|
|
2329
|
+
let _resolver;
|
|
2330
|
+
/**
|
|
2331
|
+
* Register the message resolver
|
|
2332
|
+
*
|
|
2333
|
+
* @param resolver - A {@link MessageResolver} function
|
|
2334
|
+
*
|
|
2335
|
+
* @VueI18nGeneral
|
|
2336
|
+
*/
|
|
2337
|
+
function registerMessageResolver(resolver) {
|
|
2338
|
+
_resolver = resolver;
|
|
2339
|
+
}
|
|
2340
|
+
let _fallbacker;
|
|
2341
|
+
/**
|
|
2342
|
+
* Register the locale fallbacker
|
|
2343
|
+
*
|
|
2344
|
+
* @param fallbacker - A {@link LocaleFallbacker} function
|
|
2333
2345
|
*
|
|
2334
2346
|
* @VueI18nGeneral
|
|
2335
2347
|
*/
|
|
@@ -2452,652 +2464,269 @@ function createCoreContext(options = {}) {
|
|
|
2452
2464
|
context.__datetimeFormatters = __datetimeFormatters;
|
|
2453
2465
|
context.__numberFormatters = __numberFormatters;
|
|
2454
2466
|
}
|
|
2455
|
-
// for vue-devtools timeline event
|
|
2456
|
-
{
|
|
2457
|
-
context.__v_emitter =
|
|
2458
|
-
internalOptions.__v_emitter != null
|
|
2459
|
-
? internalOptions.__v_emitter
|
|
2460
|
-
: undefined;
|
|
2461
|
-
}
|
|
2462
|
-
// NOTE: experimental !!
|
|
2463
|
-
{
|
|
2464
|
-
initI18nDevTools(context, version, __meta);
|
|
2465
|
-
}
|
|
2466
|
-
return context;
|
|
2467
|
-
}
|
|
2468
|
-
/** @internal */
|
|
2469
|
-
function isTranslateFallbackWarn(fallback, key) {
|
|
2470
|
-
return fallback instanceof RegExp ? fallback.test(key) : fallback;
|
|
2471
|
-
}
|
|
2472
|
-
/** @internal */
|
|
2473
|
-
function isTranslateMissingWarn(missing, key) {
|
|
2474
|
-
return missing instanceof RegExp ? missing.test(key) : missing;
|
|
2475
|
-
}
|
|
2476
|
-
/** @internal */
|
|
2477
|
-
function handleMissing(context, key, locale, missingWarn, type) {
|
|
2478
|
-
const { missing, onWarn } = context;
|
|
2479
|
-
// for vue-devtools timeline event
|
|
2480
|
-
{
|
|
2481
|
-
const emitter = context.__v_emitter;
|
|
2482
|
-
if (emitter) {
|
|
2483
|
-
emitter.emit('missing', {
|
|
2484
|
-
locale,
|
|
2485
|
-
key,
|
|
2486
|
-
type,
|
|
2487
|
-
groupId: `${type}:${key}`
|
|
2488
|
-
});
|
|
2489
|
-
}
|
|
2490
|
-
}
|
|
2491
|
-
if (missing !== null) {
|
|
2492
|
-
const ret = missing(context, locale, key, type);
|
|
2493
|
-
return isString(ret) ? ret : key;
|
|
2494
|
-
}
|
|
2495
|
-
else {
|
|
2496
|
-
if (isTranslateMissingWarn(missingWarn, key)) {
|
|
2497
|
-
onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, { key, locale }));
|
|
2498
|
-
}
|
|
2499
|
-
return key;
|
|
2500
|
-
}
|
|
2501
|
-
}
|
|
2502
|
-
/** @internal */
|
|
2503
|
-
function updateFallbackLocale(ctx, locale, fallback) {
|
|
2504
|
-
const context = ctx;
|
|
2505
|
-
context.__localeChainCache = new Map();
|
|
2506
|
-
ctx.localeFallbacker(ctx, fallback, locale);
|
|
2507
|
-
}
|
|
2508
|
-
/** @internal */
|
|
2509
|
-
function isAlmostSameLocale(locale, compareLocale) {
|
|
2510
|
-
if (locale === compareLocale)
|
|
2511
|
-
return false;
|
|
2512
|
-
return locale.split('-')[0] === compareLocale.split('-')[0];
|
|
2513
|
-
}
|
|
2514
|
-
/** @internal */
|
|
2515
|
-
function isImplicitFallback(targetLocale, locales) {
|
|
2516
|
-
const index = locales.indexOf(targetLocale);
|
|
2517
|
-
if (index === -1) {
|
|
2518
|
-
return false;
|
|
2519
|
-
}
|
|
2520
|
-
for (let i = index + 1; i < locales.length; i++) {
|
|
2521
|
-
if (isAlmostSameLocale(targetLocale, locales[i])) {
|
|
2522
|
-
return true;
|
|
2523
|
-
}
|
|
2524
|
-
}
|
|
2525
|
-
return false;
|
|
2526
|
-
}
|
|
2527
|
-
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
2528
|
-
|
|
2529
|
-
function format(ast) {
|
|
2530
|
-
const msg = (ctx) => formatParts(ctx, ast);
|
|
2531
|
-
return msg;
|
|
2532
|
-
}
|
|
2533
|
-
function formatParts(ctx, ast) {
|
|
2534
|
-
const body = ast.b || ast.body;
|
|
2535
|
-
if ((body.t || body.type) === 1 /* NodeTypes.Plural */) {
|
|
2536
|
-
const plural = body;
|
|
2537
|
-
const cases = plural.c || plural.cases;
|
|
2538
|
-
return ctx.plural(cases.reduce((messages, c) => [
|
|
2539
|
-
...messages,
|
|
2540
|
-
formatMessageParts(ctx, c)
|
|
2541
|
-
], []));
|
|
2542
|
-
}
|
|
2543
|
-
else {
|
|
2544
|
-
return formatMessageParts(ctx, body);
|
|
2545
|
-
}
|
|
2546
|
-
}
|
|
2547
|
-
function formatMessageParts(ctx, node) {
|
|
2548
|
-
const _static = node.s || node.static;
|
|
2549
|
-
if (_static) {
|
|
2550
|
-
return ctx.type === 'text'
|
|
2551
|
-
? _static
|
|
2552
|
-
: ctx.normalize([_static]);
|
|
2553
|
-
}
|
|
2554
|
-
else {
|
|
2555
|
-
const messages = (node.i || node.items).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
|
|
2556
|
-
return ctx.normalize(messages);
|
|
2557
|
-
}
|
|
2558
|
-
}
|
|
2559
|
-
function formatMessagePart(ctx, node) {
|
|
2560
|
-
const type = node.t || node.type;
|
|
2561
|
-
switch (type) {
|
|
2562
|
-
case 3 /* NodeTypes.Text */: {
|
|
2563
|
-
const text = node;
|
|
2564
|
-
return (text.v || text.value);
|
|
2565
|
-
}
|
|
2566
|
-
case 9 /* NodeTypes.Literal */: {
|
|
2567
|
-
const literal = node;
|
|
2568
|
-
return (literal.v || literal.value);
|
|
2569
|
-
}
|
|
2570
|
-
case 4 /* NodeTypes.Named */: {
|
|
2571
|
-
const named = node;
|
|
2572
|
-
return ctx.interpolate(ctx.named(named.k || named.key));
|
|
2573
|
-
}
|
|
2574
|
-
case 5 /* NodeTypes.List */: {
|
|
2575
|
-
const list = node;
|
|
2576
|
-
return ctx.interpolate(ctx.list(list.i != null ? list.i : list.index));
|
|
2577
|
-
}
|
|
2578
|
-
case 6 /* NodeTypes.Linked */: {
|
|
2579
|
-
const linked = node;
|
|
2580
|
-
const modifier = linked.m || linked.modifier;
|
|
2581
|
-
return ctx.linked(formatMessagePart(ctx, linked.k || linked.key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
|
|
2582
|
-
}
|
|
2583
|
-
case 7 /* NodeTypes.LinkedKey */: {
|
|
2584
|
-
const linkedKey = node;
|
|
2585
|
-
return (linkedKey.v || linkedKey.value);
|
|
2586
|
-
}
|
|
2587
|
-
case 8 /* NodeTypes.LinkedModifier */: {
|
|
2588
|
-
const linkedModifier = node;
|
|
2589
|
-
return (linkedModifier.v || linkedModifier.value);
|
|
2590
|
-
}
|
|
2591
|
-
default:
|
|
2592
|
-
throw new Error(`unhandled node type on format message part: ${type}`);
|
|
2593
|
-
}
|
|
2594
|
-
}
|
|
2595
|
-
|
|
2596
|
-
const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
|
|
2597
|
-
function checkHtmlMessage(source, warnHtmlMessage) {
|
|
2598
|
-
if (warnHtmlMessage && detectHtmlTag(source)) {
|
|
2599
|
-
warn(format$1(WARN_MESSAGE, { source }));
|
|
2600
|
-
}
|
|
2601
|
-
}
|
|
2602
|
-
const defaultOnCacheKey = (message) => message;
|
|
2603
|
-
let compileCache = Object.create(null);
|
|
2604
|
-
function clearCompileCache() {
|
|
2605
|
-
compileCache = Object.create(null);
|
|
2606
|
-
}
|
|
2607
|
-
const isMessageAST = (val) => isObject(val) &&
|
|
2608
|
-
(val.t === 0 || val.type === 0) &&
|
|
2609
|
-
('b' in val || 'body' in val);
|
|
2610
|
-
function baseCompile(message, options = {}) {
|
|
2611
|
-
// error detecting on compile
|
|
2612
|
-
let detectError = false;
|
|
2613
|
-
const onError = options.onError || defaultOnError;
|
|
2614
|
-
options.onError = (err) => {
|
|
2615
|
-
detectError = true;
|
|
2616
|
-
onError(err);
|
|
2617
|
-
};
|
|
2618
|
-
// compile with mesasge-compiler
|
|
2619
|
-
return { ...baseCompile$1(message, options), detectError };
|
|
2620
|
-
}
|
|
2621
|
-
/* #__NO_SIDE_EFFECTS__ */
|
|
2622
|
-
function compile(message, context) {
|
|
2623
|
-
if (isString(message)) {
|
|
2624
|
-
// check HTML message
|
|
2625
|
-
const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
|
|
2626
|
-
? context.warnHtmlMessage
|
|
2627
|
-
: true;
|
|
2628
|
-
checkHtmlMessage(message, warnHtmlMessage);
|
|
2629
|
-
// check caches
|
|
2630
|
-
const onCacheKey = context.onCacheKey || defaultOnCacheKey;
|
|
2631
|
-
const cacheKey = onCacheKey(message);
|
|
2632
|
-
const cached = compileCache[cacheKey];
|
|
2633
|
-
if (cached) {
|
|
2634
|
-
return cached;
|
|
2635
|
-
}
|
|
2636
|
-
// compile with JIT mode
|
|
2637
|
-
const { ast, detectError } = baseCompile(message, {
|
|
2638
|
-
...context,
|
|
2639
|
-
location: true,
|
|
2640
|
-
jit: true
|
|
2641
|
-
});
|
|
2642
|
-
// compose message function from AST
|
|
2643
|
-
const msg = format(ast);
|
|
2644
|
-
// if occurred compile error, don't cache
|
|
2645
|
-
return !detectError
|
|
2646
|
-
? (compileCache[cacheKey] = msg)
|
|
2647
|
-
: msg;
|
|
2648
|
-
}
|
|
2649
|
-
else {
|
|
2650
|
-
if (!isMessageAST(message)) {
|
|
2651
|
-
warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
|
|
2652
|
-
return (() => message);
|
|
2653
|
-
}
|
|
2654
|
-
// AST case (passed from bundler)
|
|
2655
|
-
const cacheKey = message.cacheKey;
|
|
2656
|
-
if (cacheKey) {
|
|
2657
|
-
const cached = compileCache[cacheKey];
|
|
2658
|
-
if (cached) {
|
|
2659
|
-
return cached;
|
|
2660
|
-
}
|
|
2661
|
-
// compose message function from message (AST)
|
|
2662
|
-
return (compileCache[cacheKey] =
|
|
2663
|
-
format(message));
|
|
2664
|
-
}
|
|
2665
|
-
else {
|
|
2666
|
-
return format(message);
|
|
2667
|
-
}
|
|
2668
|
-
}
|
|
2669
|
-
}
|
|
2670
|
-
|
|
2671
|
-
const NOOP_MESSAGE_FUNCTION = () => '';
|
|
2672
|
-
const isMessageFunction = (val) => isFunction(val);
|
|
2673
|
-
// implementation of `translate` function
|
|
2674
|
-
function translate(context, ...args) {
|
|
2675
|
-
const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
|
|
2676
|
-
const [key, options] = parseTranslateArgs(...args);
|
|
2677
|
-
const missingWarn = isBoolean(options.missingWarn)
|
|
2678
|
-
? options.missingWarn
|
|
2679
|
-
: context.missingWarn;
|
|
2680
|
-
const fallbackWarn = isBoolean(options.fallbackWarn)
|
|
2681
|
-
? options.fallbackWarn
|
|
2682
|
-
: context.fallbackWarn;
|
|
2683
|
-
const escapeParameter = isBoolean(options.escapeParameter)
|
|
2684
|
-
? options.escapeParameter
|
|
2685
|
-
: context.escapeParameter;
|
|
2686
|
-
const resolvedMessage = !!options.resolvedMessage;
|
|
2687
|
-
// prettier-ignore
|
|
2688
|
-
const defaultMsgOrKey = isString(options.default) || isBoolean(options.default) // default by function option
|
|
2689
|
-
? !isBoolean(options.default)
|
|
2690
|
-
? options.default
|
|
2691
|
-
: (!messageCompiler ? () => key : key)
|
|
2692
|
-
: fallbackFormat // default by `fallbackFormat` option
|
|
2693
|
-
? (!messageCompiler ? () => key : key)
|
|
2694
|
-
: null;
|
|
2695
|
-
const enableDefaultMsg = fallbackFormat ||
|
|
2696
|
-
(defaultMsgOrKey != null &&
|
|
2697
|
-
(isString(defaultMsgOrKey) || isFunction(defaultMsgOrKey)));
|
|
2698
|
-
const locale = getLocale(context, options);
|
|
2699
|
-
// escape params
|
|
2700
|
-
escapeParameter && escapeParams(options);
|
|
2701
|
-
// resolve message format
|
|
2702
|
-
// eslint-disable-next-line prefer-const
|
|
2703
|
-
let [formatScope, targetLocale, message] = !resolvedMessage
|
|
2704
|
-
? resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn)
|
|
2705
|
-
: [
|
|
2706
|
-
key,
|
|
2707
|
-
locale,
|
|
2708
|
-
messages[locale] || {}
|
|
2709
|
-
];
|
|
2710
|
-
// NOTE:
|
|
2711
|
-
// Fix to work around `ssrTransfrom` bug in Vite.
|
|
2712
|
-
// https://github.com/vitejs/vite/issues/4306
|
|
2713
|
-
// To get around this, use temporary variables.
|
|
2714
|
-
// https://github.com/nuxt/framework/issues/1461#issuecomment-954606243
|
|
2715
|
-
let format = formatScope;
|
|
2716
|
-
// if you use default message, set it as message format!
|
|
2717
|
-
let cacheBaseKey = key;
|
|
2718
|
-
if (!resolvedMessage &&
|
|
2719
|
-
!(isString(format) ||
|
|
2720
|
-
isMessageAST(format) ||
|
|
2721
|
-
isMessageFunction(format))) {
|
|
2722
|
-
if (enableDefaultMsg) {
|
|
2723
|
-
format = defaultMsgOrKey;
|
|
2724
|
-
cacheBaseKey = format;
|
|
2725
|
-
}
|
|
2726
|
-
}
|
|
2727
|
-
// checking message format and target locale
|
|
2728
|
-
if (!resolvedMessage &&
|
|
2729
|
-
(!(isString(format) ||
|
|
2730
|
-
isMessageAST(format) ||
|
|
2731
|
-
isMessageFunction(format)) ||
|
|
2732
|
-
!isString(targetLocale))) {
|
|
2733
|
-
return unresolving ? NOT_REOSLVED : key;
|
|
2734
|
-
}
|
|
2735
|
-
// TODO: refactor
|
|
2736
|
-
if (isString(format) && context.messageCompiler == null) {
|
|
2737
|
-
warn(`The message format compilation is not supported in this build. ` +
|
|
2738
|
-
`Because message compiler isn't included. ` +
|
|
2739
|
-
`You need to pre-compilation all message format. ` +
|
|
2740
|
-
`So translate function return '${key}'.`);
|
|
2741
|
-
return key;
|
|
2742
|
-
}
|
|
2743
|
-
// setup compile error detecting
|
|
2744
|
-
let occurred = false;
|
|
2745
|
-
const onError = () => {
|
|
2746
|
-
occurred = true;
|
|
2747
|
-
};
|
|
2748
|
-
// compile message format
|
|
2749
|
-
const msg = !isMessageFunction(format)
|
|
2750
|
-
? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError)
|
|
2751
|
-
: format;
|
|
2752
|
-
// if occurred compile error, return the message format
|
|
2753
|
-
if (occurred) {
|
|
2754
|
-
return format;
|
|
2467
|
+
// for vue-devtools timeline event
|
|
2468
|
+
{
|
|
2469
|
+
context.__v_emitter =
|
|
2470
|
+
internalOptions.__v_emitter != null
|
|
2471
|
+
? internalOptions.__v_emitter
|
|
2472
|
+
: undefined;
|
|
2755
2473
|
}
|
|
2756
|
-
// evaluate message with context
|
|
2757
|
-
const ctxOptions = getMessageContextOptions(context, targetLocale, message, options);
|
|
2758
|
-
const msgContext = createMessageContext(ctxOptions);
|
|
2759
|
-
const messaged = evaluateMessage(context, msg, msgContext);
|
|
2760
|
-
// if use post translation option, proceed it with handler
|
|
2761
|
-
const ret = postTranslation
|
|
2762
|
-
? postTranslation(messaged, key)
|
|
2763
|
-
: messaged;
|
|
2764
2474
|
// NOTE: experimental !!
|
|
2765
2475
|
{
|
|
2766
|
-
|
|
2767
|
-
const payloads = {
|
|
2768
|
-
timestamp: Date.now(),
|
|
2769
|
-
key: isString(key)
|
|
2770
|
-
? key
|
|
2771
|
-
: isMessageFunction(format)
|
|
2772
|
-
? format.key
|
|
2773
|
-
: '',
|
|
2774
|
-
locale: targetLocale || (isMessageFunction(format)
|
|
2775
|
-
? format.locale
|
|
2776
|
-
: ''),
|
|
2777
|
-
format: isString(format)
|
|
2778
|
-
? format
|
|
2779
|
-
: isMessageFunction(format)
|
|
2780
|
-
? format.source
|
|
2781
|
-
: '',
|
|
2782
|
-
message: ret
|
|
2783
|
-
};
|
|
2784
|
-
payloads.meta = assign({}, context.__meta, getAdditionalMeta() || {});
|
|
2785
|
-
translateDevTools(payloads);
|
|
2476
|
+
initI18nDevTools(context, version, __meta);
|
|
2786
2477
|
}
|
|
2787
|
-
return
|
|
2478
|
+
return context;
|
|
2788
2479
|
}
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
2792
|
-
}
|
|
2793
|
-
else if (isObject(options.named)) {
|
|
2794
|
-
Object.keys(options.named).forEach(key => {
|
|
2795
|
-
if (isString(options.named[key])) {
|
|
2796
|
-
options.named[key] = escapeHtml(options.named[key]);
|
|
2797
|
-
}
|
|
2798
|
-
});
|
|
2799
|
-
}
|
|
2480
|
+
/** @internal */
|
|
2481
|
+
function isTranslateFallbackWarn(fallback, key) {
|
|
2482
|
+
return fallback instanceof RegExp ? fallback.test(key) : fallback;
|
|
2800
2483
|
}
|
|
2801
|
-
|
|
2802
|
-
|
|
2803
|
-
|
|
2804
|
-
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2813
|
-
|
|
2814
|
-
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
2815
|
-
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_TRANSLATE, {
|
|
2484
|
+
/** @internal */
|
|
2485
|
+
function isTranslateMissingWarn(missing, key) {
|
|
2486
|
+
return missing instanceof RegExp ? missing.test(key) : missing;
|
|
2487
|
+
}
|
|
2488
|
+
/** @internal */
|
|
2489
|
+
function handleMissing(context, key, locale, missingWarn, type) {
|
|
2490
|
+
const { missing, onWarn } = context;
|
|
2491
|
+
// for vue-devtools timeline event
|
|
2492
|
+
{
|
|
2493
|
+
const emitter = context.__v_emitter;
|
|
2494
|
+
if (emitter) {
|
|
2495
|
+
emitter.emit('missing', {
|
|
2496
|
+
locale,
|
|
2816
2497
|
key,
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
// for vue-devtools timeline event
|
|
2821
|
-
if (locale !== targetLocale) {
|
|
2822
|
-
const emitter = context.__v_emitter;
|
|
2823
|
-
if (emitter) {
|
|
2824
|
-
emitter.emit('fallback', {
|
|
2825
|
-
type,
|
|
2826
|
-
key,
|
|
2827
|
-
from,
|
|
2828
|
-
to,
|
|
2829
|
-
groupId: `${type}:${key}`
|
|
2830
|
-
});
|
|
2831
|
-
}
|
|
2832
|
-
}
|
|
2833
|
-
message =
|
|
2834
|
-
messages[targetLocale] || {};
|
|
2835
|
-
// for vue-devtools timeline event
|
|
2836
|
-
let start = null;
|
|
2837
|
-
let startTag;
|
|
2838
|
-
let endTag;
|
|
2839
|
-
if (inBrowser) {
|
|
2840
|
-
start = window.performance.now();
|
|
2841
|
-
startTag = 'intlify-message-resolve-start';
|
|
2842
|
-
endTag = 'intlify-message-resolve-end';
|
|
2843
|
-
mark && mark(startTag);
|
|
2844
|
-
}
|
|
2845
|
-
if ((format = resolveValue(message, key)) === null) {
|
|
2846
|
-
// if null, resolve with object key path
|
|
2847
|
-
format = message[key]; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
2848
|
-
}
|
|
2849
|
-
// for vue-devtools timeline event
|
|
2850
|
-
if (inBrowser) {
|
|
2851
|
-
const end = window.performance.now();
|
|
2852
|
-
const emitter = context.__v_emitter;
|
|
2853
|
-
if (emitter && start && format) {
|
|
2854
|
-
emitter.emit('message-resolve', {
|
|
2855
|
-
type: 'message-resolve',
|
|
2856
|
-
key,
|
|
2857
|
-
message: format,
|
|
2858
|
-
time: end - start,
|
|
2859
|
-
groupId: `${type}:${key}`
|
|
2860
|
-
});
|
|
2861
|
-
}
|
|
2862
|
-
if (startTag && endTag && mark && measure) {
|
|
2863
|
-
mark(endTag);
|
|
2864
|
-
measure('intlify message resolve', startTag, endTag);
|
|
2865
|
-
}
|
|
2866
|
-
}
|
|
2867
|
-
if (isString(format) || isMessageAST(format) || isMessageFunction(format)) {
|
|
2868
|
-
break;
|
|
2498
|
+
type,
|
|
2499
|
+
groupId: `${type}:${key}`
|
|
2500
|
+
});
|
|
2869
2501
|
}
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2502
|
+
}
|
|
2503
|
+
if (missing !== null) {
|
|
2504
|
+
const ret = missing(context, locale, key, type);
|
|
2505
|
+
return isString(ret) ? ret : key;
|
|
2506
|
+
}
|
|
2507
|
+
else {
|
|
2508
|
+
if (isTranslateMissingWarn(missingWarn, key)) {
|
|
2509
|
+
onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, { key, locale }));
|
|
2876
2510
|
}
|
|
2877
|
-
|
|
2511
|
+
return key;
|
|
2878
2512
|
}
|
|
2879
|
-
return [format, targetLocale, message];
|
|
2880
2513
|
}
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2514
|
+
/** @internal */
|
|
2515
|
+
function updateFallbackLocale(ctx, locale, fallback) {
|
|
2516
|
+
const context = ctx;
|
|
2517
|
+
context.__localeChainCache = new Map();
|
|
2518
|
+
ctx.localeFallbacker(ctx, fallback, locale);
|
|
2519
|
+
}
|
|
2520
|
+
/** @internal */
|
|
2521
|
+
function isAlmostSameLocale(locale, compareLocale) {
|
|
2522
|
+
if (locale === compareLocale)
|
|
2523
|
+
return false;
|
|
2524
|
+
return locale.split('-')[0] === compareLocale.split('-')[0];
|
|
2525
|
+
}
|
|
2526
|
+
/** @internal */
|
|
2527
|
+
function isImplicitFallback(targetLocale, locales) {
|
|
2528
|
+
const index = locales.indexOf(targetLocale);
|
|
2529
|
+
if (index === -1) {
|
|
2530
|
+
return false;
|
|
2888
2531
|
}
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
return msg;
|
|
2532
|
+
for (let i = index + 1; i < locales.length; i++) {
|
|
2533
|
+
if (isAlmostSameLocale(targetLocale, locales[i])) {
|
|
2534
|
+
return true;
|
|
2535
|
+
}
|
|
2894
2536
|
}
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2537
|
+
return false;
|
|
2538
|
+
}
|
|
2539
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
2540
|
+
|
|
2541
|
+
const intlDefined = typeof Intl !== 'undefined';
|
|
2542
|
+
const Availabilities = {
|
|
2543
|
+
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== 'undefined',
|
|
2544
|
+
numberFormat: intlDefined && typeof Intl.NumberFormat !== 'undefined'
|
|
2545
|
+
};
|
|
2546
|
+
|
|
2547
|
+
// implementation of `datetime` function
|
|
2548
|
+
function datetime(context, ...args) {
|
|
2549
|
+
const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
2550
|
+
const { __datetimeFormatters } = context;
|
|
2551
|
+
if (!Availabilities.dateTimeFormat) {
|
|
2552
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
2553
|
+
return MISSING_RESOLVE_VALUE;
|
|
2904
2554
|
}
|
|
2905
|
-
const
|
|
2906
|
-
|
|
2907
|
-
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2555
|
+
const [key, value, options, overrides] = parseDateTimeArgs(...args);
|
|
2556
|
+
const missingWarn = isBoolean(options.missingWarn)
|
|
2557
|
+
? options.missingWarn
|
|
2558
|
+
: context.missingWarn;
|
|
2559
|
+
const fallbackWarn = isBoolean(options.fallbackWarn)
|
|
2560
|
+
? options.fallbackWarn
|
|
2561
|
+
: context.fallbackWarn;
|
|
2562
|
+
const part = !!options.part;
|
|
2563
|
+
const locale = getLocale(context, options);
|
|
2564
|
+
const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
2565
|
+
fallbackLocale, locale);
|
|
2566
|
+
if (!isString(key) || key === '') {
|
|
2567
|
+
return new Intl.DateTimeFormat(locale, overrides).format(value);
|
|
2568
|
+
}
|
|
2569
|
+
// resolve format
|
|
2570
|
+
let datetimeFormat = {};
|
|
2571
|
+
let targetLocale;
|
|
2572
|
+
let format = null;
|
|
2573
|
+
let from = locale;
|
|
2574
|
+
let to = null;
|
|
2575
|
+
const type = 'datetime format';
|
|
2576
|
+
for (let i = 0; i < locales.length; i++) {
|
|
2577
|
+
targetLocale = to = locales[i];
|
|
2578
|
+
if (locale !== targetLocale &&
|
|
2579
|
+
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
2580
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DATE_FORMAT, {
|
|
2581
|
+
key,
|
|
2582
|
+
target: targetLocale
|
|
2583
|
+
}));
|
|
2917
2584
|
}
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2585
|
+
// for vue-devtools timeline event
|
|
2586
|
+
if (locale !== targetLocale) {
|
|
2587
|
+
const emitter = context.__v_emitter;
|
|
2588
|
+
if (emitter) {
|
|
2589
|
+
emitter.emit('fallback', {
|
|
2590
|
+
type,
|
|
2591
|
+
key,
|
|
2592
|
+
from,
|
|
2593
|
+
to,
|
|
2594
|
+
groupId: `${type}:${key}`
|
|
2595
|
+
});
|
|
2596
|
+
}
|
|
2921
2597
|
}
|
|
2598
|
+
datetimeFormat =
|
|
2599
|
+
datetimeFormats[targetLocale] || {};
|
|
2600
|
+
format = datetimeFormat[key];
|
|
2601
|
+
if (isPlainObject(format))
|
|
2602
|
+
break;
|
|
2603
|
+
handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
2604
|
+
from = to;
|
|
2922
2605
|
}
|
|
2923
|
-
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
return msg;
|
|
2927
|
-
}
|
|
2928
|
-
function evaluateMessage(context, msg, msgCtx) {
|
|
2929
|
-
// for vue-devtools timeline event
|
|
2930
|
-
let start = null;
|
|
2931
|
-
let startTag;
|
|
2932
|
-
let endTag;
|
|
2933
|
-
if (inBrowser) {
|
|
2934
|
-
start = window.performance.now();
|
|
2935
|
-
startTag = 'intlify-message-evaluation-start';
|
|
2936
|
-
endTag = 'intlify-message-evaluation-end';
|
|
2937
|
-
mark && mark(startTag);
|
|
2606
|
+
// checking format and target locale
|
|
2607
|
+
if (!isPlainObject(format) || !isString(targetLocale)) {
|
|
2608
|
+
return unresolving ? NOT_REOSLVED : key;
|
|
2938
2609
|
}
|
|
2939
|
-
|
|
2940
|
-
|
|
2941
|
-
|
|
2942
|
-
const end = window.performance.now();
|
|
2943
|
-
const emitter = context.__v_emitter;
|
|
2944
|
-
if (emitter && start) {
|
|
2945
|
-
emitter.emit('message-evaluation', {
|
|
2946
|
-
type: 'message-evaluation',
|
|
2947
|
-
value: messaged,
|
|
2948
|
-
time: end - start,
|
|
2949
|
-
groupId: `${'translate'}:${msg.key}`
|
|
2950
|
-
});
|
|
2951
|
-
}
|
|
2952
|
-
if (startTag && endTag && mark && measure) {
|
|
2953
|
-
mark(endTag);
|
|
2954
|
-
measure('intlify message evaluation', startTag, endTag);
|
|
2955
|
-
}
|
|
2610
|
+
let id = `${targetLocale}__${key}`;
|
|
2611
|
+
if (!isEmptyObject(overrides)) {
|
|
2612
|
+
id = `${id}__${JSON.stringify(overrides)}`;
|
|
2956
2613
|
}
|
|
2957
|
-
|
|
2614
|
+
let formatter = __datetimeFormatters.get(id);
|
|
2615
|
+
if (!formatter) {
|
|
2616
|
+
formatter = new Intl.DateTimeFormat(targetLocale, assign({}, format, overrides));
|
|
2617
|
+
__datetimeFormatters.set(id, formatter);
|
|
2618
|
+
}
|
|
2619
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2958
2620
|
}
|
|
2959
2621
|
/** @internal */
|
|
2960
|
-
|
|
2961
|
-
|
|
2622
|
+
const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
2623
|
+
'localeMatcher',
|
|
2624
|
+
'weekday',
|
|
2625
|
+
'era',
|
|
2626
|
+
'year',
|
|
2627
|
+
'month',
|
|
2628
|
+
'day',
|
|
2629
|
+
'hour',
|
|
2630
|
+
'minute',
|
|
2631
|
+
'second',
|
|
2632
|
+
'timeZoneName',
|
|
2633
|
+
'formatMatcher',
|
|
2634
|
+
'hour12',
|
|
2635
|
+
'timeZone',
|
|
2636
|
+
'dateStyle',
|
|
2637
|
+
'timeStyle',
|
|
2638
|
+
'calendar',
|
|
2639
|
+
'dayPeriod',
|
|
2640
|
+
'numberingSystem',
|
|
2641
|
+
'hourCycle',
|
|
2642
|
+
'fractionalSecondDigits'
|
|
2643
|
+
];
|
|
2644
|
+
/** @internal */
|
|
2645
|
+
function parseDateTimeArgs(...args) {
|
|
2646
|
+
const [arg1, arg2, arg3, arg4] = args;
|
|
2962
2647
|
const options = {};
|
|
2963
|
-
|
|
2964
|
-
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2648
|
+
let overrides = {};
|
|
2649
|
+
let value;
|
|
2650
|
+
if (isString(arg1)) {
|
|
2651
|
+
// Only allow ISO strings - other date formats are often supported,
|
|
2652
|
+
// but may cause different results in different browsers.
|
|
2653
|
+
const matches = arg1.match(/(\d{4}-\d{2}-\d{2})(T|\s)?(.*)/);
|
|
2654
|
+
if (!matches) {
|
|
2655
|
+
throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
2656
|
+
}
|
|
2657
|
+
// Some browsers can not parse the iso datetime separated by space,
|
|
2658
|
+
// this is a compromise solution by replace the 'T'/' ' with 'T'
|
|
2659
|
+
const dateTime = matches[3]
|
|
2660
|
+
? matches[3].trim().startsWith('T')
|
|
2661
|
+
? `${matches[1].trim()}${matches[3].trim()}`
|
|
2662
|
+
: `${matches[1].trim()}T${matches[3].trim()}`
|
|
2663
|
+
: matches[1].trim();
|
|
2664
|
+
value = new Date(dateTime);
|
|
2665
|
+
try {
|
|
2666
|
+
// This will fail if the date is not valid
|
|
2667
|
+
value.toISOString();
|
|
2668
|
+
}
|
|
2669
|
+
catch {
|
|
2670
|
+
throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
2671
|
+
}
|
|
2983
2672
|
}
|
|
2984
|
-
else if (
|
|
2985
|
-
|
|
2673
|
+
else if (isDate(arg1)) {
|
|
2674
|
+
if (isNaN(arg1.getTime())) {
|
|
2675
|
+
throw createCoreError(CoreErrorCodes.INVALID_DATE_ARGUMENT);
|
|
2676
|
+
}
|
|
2677
|
+
value = arg1;
|
|
2986
2678
|
}
|
|
2987
|
-
if (isNumber(
|
|
2988
|
-
|
|
2679
|
+
else if (isNumber(arg1)) {
|
|
2680
|
+
value = arg1;
|
|
2989
2681
|
}
|
|
2990
|
-
else
|
|
2991
|
-
|
|
2682
|
+
else {
|
|
2683
|
+
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
2992
2684
|
}
|
|
2993
|
-
|
|
2994
|
-
|
|
2685
|
+
if (isString(arg2)) {
|
|
2686
|
+
options.key = arg2;
|
|
2995
2687
|
}
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
locale,
|
|
3001
|
-
key,
|
|
3002
|
-
warnHtmlMessage,
|
|
3003
|
-
onError: (err) => {
|
|
3004
|
-
onError && onError(err);
|
|
3005
|
-
{
|
|
3006
|
-
const _source = getSourceForCodeFrame(source);
|
|
3007
|
-
const message = `Message compilation error: ${err.message}`;
|
|
3008
|
-
const codeFrame = err.location &&
|
|
3009
|
-
_source &&
|
|
3010
|
-
generateCodeFrame(_source, err.location.start.offset, err.location.end.offset);
|
|
3011
|
-
const emitter = context.__v_emitter;
|
|
3012
|
-
if (emitter && _source) {
|
|
3013
|
-
emitter.emit('compile-error', {
|
|
3014
|
-
message: _source,
|
|
3015
|
-
error: err.message,
|
|
3016
|
-
start: err.location && err.location.start.offset,
|
|
3017
|
-
end: err.location && err.location.end.offset,
|
|
3018
|
-
groupId: `${'translate'}:${key}`
|
|
3019
|
-
});
|
|
3020
|
-
}
|
|
3021
|
-
console.error(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
2688
|
+
else if (isPlainObject(arg2)) {
|
|
2689
|
+
Object.keys(arg2).forEach(key => {
|
|
2690
|
+
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
2691
|
+
overrides[key] = arg2[key];
|
|
3022
2692
|
}
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
}
|
|
3027
|
-
function getSourceForCodeFrame(source) {
|
|
3028
|
-
if (isString(source)) {
|
|
3029
|
-
return source;
|
|
3030
|
-
}
|
|
3031
|
-
else {
|
|
3032
|
-
if (source.loc && source.loc.source) {
|
|
3033
|
-
return source.loc.source;
|
|
3034
|
-
}
|
|
2693
|
+
else {
|
|
2694
|
+
options[key] = arg2[key];
|
|
2695
|
+
}
|
|
2696
|
+
});
|
|
3035
2697
|
}
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
|
|
3039
|
-
const resolveMessage = (key, useLinked) => {
|
|
3040
|
-
let val = resolveValue(message, key);
|
|
3041
|
-
// fallback
|
|
3042
|
-
if (val == null && (fallbackContext || useLinked)) {
|
|
3043
|
-
const [, , message] = resolveMessageFormat(fallbackContext || context, // NOTE: if has fallbackContext, fallback to root, else if use linked, fallback to local context
|
|
3044
|
-
key, locale, fallbackLocale, fallbackWarn, missingWarn);
|
|
3045
|
-
val = resolveValue(message, key);
|
|
3046
|
-
}
|
|
3047
|
-
if (isString(val) || isMessageAST(val)) {
|
|
3048
|
-
let occurred = false;
|
|
3049
|
-
const onError = () => {
|
|
3050
|
-
occurred = true;
|
|
3051
|
-
};
|
|
3052
|
-
const msg = compileMessageFormat(context, key, locale, val, key, onError);
|
|
3053
|
-
return !occurred
|
|
3054
|
-
? msg
|
|
3055
|
-
: NOOP_MESSAGE_FUNCTION;
|
|
3056
|
-
}
|
|
3057
|
-
else if (isMessageFunction(val)) {
|
|
3058
|
-
return val;
|
|
3059
|
-
}
|
|
3060
|
-
else {
|
|
3061
|
-
// TODO: should be implemented warning message
|
|
3062
|
-
return NOOP_MESSAGE_FUNCTION;
|
|
3063
|
-
}
|
|
3064
|
-
};
|
|
3065
|
-
const ctxOptions = {
|
|
3066
|
-
locale,
|
|
3067
|
-
modifiers,
|
|
3068
|
-
pluralRules,
|
|
3069
|
-
messages: resolveMessage
|
|
3070
|
-
};
|
|
3071
|
-
if (context.processor) {
|
|
3072
|
-
ctxOptions.processor = context.processor;
|
|
2698
|
+
if (isString(arg3)) {
|
|
2699
|
+
options.locale = arg3;
|
|
3073
2700
|
}
|
|
3074
|
-
if (
|
|
3075
|
-
|
|
2701
|
+
else if (isPlainObject(arg3)) {
|
|
2702
|
+
overrides = arg3;
|
|
3076
2703
|
}
|
|
3077
|
-
if (
|
|
3078
|
-
|
|
2704
|
+
if (isPlainObject(arg4)) {
|
|
2705
|
+
overrides = arg4;
|
|
3079
2706
|
}
|
|
3080
|
-
|
|
3081
|
-
|
|
2707
|
+
return [options.key || '', value, options, overrides];
|
|
2708
|
+
}
|
|
2709
|
+
/** @internal */
|
|
2710
|
+
function clearDateTimeFormat(ctx, locale, format) {
|
|
2711
|
+
const context = ctx;
|
|
2712
|
+
for (const key in format) {
|
|
2713
|
+
const id = `${locale}__${key}`;
|
|
2714
|
+
if (!context.__datetimeFormatters.has(id)) {
|
|
2715
|
+
continue;
|
|
2716
|
+
}
|
|
2717
|
+
context.__datetimeFormatters.delete(id);
|
|
3082
2718
|
}
|
|
3083
|
-
return ctxOptions;
|
|
3084
2719
|
}
|
|
3085
2720
|
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
// implementation of `datetime` function
|
|
3093
|
-
function datetime(context, ...args) {
|
|
3094
|
-
const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
3095
|
-
const { __datetimeFormatters } = context;
|
|
3096
|
-
if (!Availabilities.dateTimeFormat) {
|
|
3097
|
-
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
2721
|
+
// implementation of `number` function
|
|
2722
|
+
function number(context, ...args) {
|
|
2723
|
+
const { numberFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
2724
|
+
const { __numberFormatters } = context;
|
|
2725
|
+
if (!Availabilities.numberFormat) {
|
|
2726
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
|
|
3098
2727
|
return MISSING_RESOLVE_VALUE;
|
|
3099
2728
|
}
|
|
3100
|
-
const [key, value, options, overrides] =
|
|
2729
|
+
const [key, value, options, overrides] = parseNumberArgs(...args);
|
|
3101
2730
|
const missingWarn = isBoolean(options.missingWarn)
|
|
3102
2731
|
? options.missingWarn
|
|
3103
2732
|
: context.missingWarn;
|
|
@@ -3109,20 +2738,20 @@ function datetime(context, ...args) {
|
|
|
3109
2738
|
const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
3110
2739
|
fallbackLocale, locale);
|
|
3111
2740
|
if (!isString(key) || key === '') {
|
|
3112
|
-
return new Intl.
|
|
2741
|
+
return new Intl.NumberFormat(locale, overrides).format(value);
|
|
3113
2742
|
}
|
|
3114
2743
|
// resolve format
|
|
3115
|
-
let
|
|
2744
|
+
let numberFormat = {};
|
|
3116
2745
|
let targetLocale;
|
|
3117
2746
|
let format = null;
|
|
3118
2747
|
let from = locale;
|
|
3119
2748
|
let to = null;
|
|
3120
|
-
const type = '
|
|
2749
|
+
const type = 'number format';
|
|
3121
2750
|
for (let i = 0; i < locales.length; i++) {
|
|
3122
2751
|
targetLocale = to = locales[i];
|
|
3123
2752
|
if (locale !== targetLocale &&
|
|
3124
2753
|
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
3125
|
-
onWarn(getWarnMessage(CoreWarnCodes.
|
|
2754
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
3126
2755
|
key,
|
|
3127
2756
|
target: targetLocale
|
|
3128
2757
|
}));
|
|
@@ -3140,9 +2769,9 @@ function datetime(context, ...args) {
|
|
|
3140
2769
|
});
|
|
3141
2770
|
}
|
|
3142
2771
|
}
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
format =
|
|
2772
|
+
numberFormat =
|
|
2773
|
+
numberFormats[targetLocale] || {};
|
|
2774
|
+
format = numberFormat[key];
|
|
3146
2775
|
if (isPlainObject(format))
|
|
3147
2776
|
break;
|
|
3148
2777
|
handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
@@ -3156,147 +2785,356 @@ function datetime(context, ...args) {
|
|
|
3156
2785
|
if (!isEmptyObject(overrides)) {
|
|
3157
2786
|
id = `${id}__${JSON.stringify(overrides)}`;
|
|
3158
2787
|
}
|
|
3159
|
-
let formatter =
|
|
2788
|
+
let formatter = __numberFormatters.get(id);
|
|
3160
2789
|
if (!formatter) {
|
|
3161
|
-
formatter = new Intl.
|
|
3162
|
-
|
|
2790
|
+
formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
|
|
2791
|
+
__numberFormatters.set(id, formatter);
|
|
3163
2792
|
}
|
|
3164
2793
|
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
3165
2794
|
}
|
|
3166
2795
|
/** @internal */
|
|
3167
|
-
const
|
|
2796
|
+
const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
3168
2797
|
'localeMatcher',
|
|
3169
|
-
'
|
|
3170
|
-
'
|
|
3171
|
-
'
|
|
3172
|
-
'
|
|
3173
|
-
'
|
|
3174
|
-
'
|
|
3175
|
-
'
|
|
3176
|
-
'
|
|
3177
|
-
'
|
|
3178
|
-
'
|
|
3179
|
-
'
|
|
3180
|
-
'
|
|
3181
|
-
'
|
|
3182
|
-
'
|
|
3183
|
-
'
|
|
3184
|
-
'
|
|
3185
|
-
'
|
|
3186
|
-
'
|
|
3187
|
-
'
|
|
2798
|
+
'style',
|
|
2799
|
+
'currency',
|
|
2800
|
+
'currencyDisplay',
|
|
2801
|
+
'currencySign',
|
|
2802
|
+
'useGrouping',
|
|
2803
|
+
'minimumIntegerDigits',
|
|
2804
|
+
'minimumFractionDigits',
|
|
2805
|
+
'maximumFractionDigits',
|
|
2806
|
+
'minimumSignificantDigits',
|
|
2807
|
+
'maximumSignificantDigits',
|
|
2808
|
+
'compactDisplay',
|
|
2809
|
+
'notation',
|
|
2810
|
+
'signDisplay',
|
|
2811
|
+
'unit',
|
|
2812
|
+
'unitDisplay',
|
|
2813
|
+
'roundingMode',
|
|
2814
|
+
'roundingPriority',
|
|
2815
|
+
'roundingIncrement',
|
|
2816
|
+
'trailingZeroDisplay'
|
|
3188
2817
|
];
|
|
3189
2818
|
/** @internal */
|
|
3190
|
-
function
|
|
2819
|
+
function parseNumberArgs(...args) {
|
|
3191
2820
|
const [arg1, arg2, arg3, arg4] = args;
|
|
3192
2821
|
const options = {};
|
|
3193
2822
|
let overrides = {};
|
|
3194
|
-
|
|
3195
|
-
|
|
3196
|
-
|
|
3197
|
-
|
|
3198
|
-
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
3211
|
-
|
|
3212
|
-
|
|
3213
|
-
|
|
3214
|
-
|
|
3215
|
-
|
|
2823
|
+
if (!isNumber(arg1)) {
|
|
2824
|
+
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
2825
|
+
}
|
|
2826
|
+
const value = arg1;
|
|
2827
|
+
if (isString(arg2)) {
|
|
2828
|
+
options.key = arg2;
|
|
2829
|
+
}
|
|
2830
|
+
else if (isPlainObject(arg2)) {
|
|
2831
|
+
Object.keys(arg2).forEach(key => {
|
|
2832
|
+
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
2833
|
+
overrides[key] = arg2[key];
|
|
2834
|
+
}
|
|
2835
|
+
else {
|
|
2836
|
+
options[key] = arg2[key];
|
|
2837
|
+
}
|
|
2838
|
+
});
|
|
2839
|
+
}
|
|
2840
|
+
if (isString(arg3)) {
|
|
2841
|
+
options.locale = arg3;
|
|
2842
|
+
}
|
|
2843
|
+
else if (isPlainObject(arg3)) {
|
|
2844
|
+
overrides = arg3;
|
|
2845
|
+
}
|
|
2846
|
+
if (isPlainObject(arg4)) {
|
|
2847
|
+
overrides = arg4;
|
|
2848
|
+
}
|
|
2849
|
+
return [options.key || '', value, options, overrides];
|
|
2850
|
+
}
|
|
2851
|
+
/** @internal */
|
|
2852
|
+
function clearNumberFormat(ctx, locale, format) {
|
|
2853
|
+
const context = ctx;
|
|
2854
|
+
for (const key in format) {
|
|
2855
|
+
const id = `${locale}__${key}`;
|
|
2856
|
+
if (!context.__numberFormatters.has(id)) {
|
|
2857
|
+
continue;
|
|
3216
2858
|
}
|
|
2859
|
+
context.__numberFormatters.delete(id);
|
|
3217
2860
|
}
|
|
3218
|
-
|
|
3219
|
-
|
|
3220
|
-
|
|
2861
|
+
}
|
|
2862
|
+
|
|
2863
|
+
const DEFAULT_MODIFIER = (str) => str;
|
|
2864
|
+
const DEFAULT_MESSAGE = (ctx) => ''; // eslint-disable-line
|
|
2865
|
+
const DEFAULT_MESSAGE_DATA_TYPE = 'text';
|
|
2866
|
+
const DEFAULT_NORMALIZE = (values) => values.length === 0 ? '' : join(values);
|
|
2867
|
+
const DEFAULT_INTERPOLATE = toDisplayString;
|
|
2868
|
+
function pluralDefault(choice, choicesLength) {
|
|
2869
|
+
choice = Math.abs(choice);
|
|
2870
|
+
if (choicesLength === 2) {
|
|
2871
|
+
// prettier-ignore
|
|
2872
|
+
return choice
|
|
2873
|
+
? choice > 1
|
|
2874
|
+
? 1
|
|
2875
|
+
: 0
|
|
2876
|
+
: 1;
|
|
2877
|
+
}
|
|
2878
|
+
return choice ? Math.min(choice, 2) : 0;
|
|
2879
|
+
}
|
|
2880
|
+
function getPluralIndex(options) {
|
|
2881
|
+
// prettier-ignore
|
|
2882
|
+
const index = isNumber(options.pluralIndex)
|
|
2883
|
+
? options.pluralIndex
|
|
2884
|
+
: -1;
|
|
2885
|
+
// prettier-ignore
|
|
2886
|
+
return options.named && (isNumber(options.named.count) || isNumber(options.named.n))
|
|
2887
|
+
? isNumber(options.named.count)
|
|
2888
|
+
? options.named.count
|
|
2889
|
+
: isNumber(options.named.n)
|
|
2890
|
+
? options.named.n
|
|
2891
|
+
: index
|
|
2892
|
+
: index;
|
|
2893
|
+
}
|
|
2894
|
+
function normalizeNamed(pluralIndex, props) {
|
|
2895
|
+
if (!props.count) {
|
|
2896
|
+
props.count = pluralIndex;
|
|
2897
|
+
}
|
|
2898
|
+
if (!props.n) {
|
|
2899
|
+
props.n = pluralIndex;
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2902
|
+
function createMessageContext(options = {}) {
|
|
2903
|
+
const locale = options.locale;
|
|
2904
|
+
const pluralIndex = getPluralIndex(options);
|
|
2905
|
+
const pluralRule = isObject(options.pluralRules) &&
|
|
2906
|
+
isString(locale) &&
|
|
2907
|
+
isFunction(options.pluralRules[locale])
|
|
2908
|
+
? options.pluralRules[locale]
|
|
2909
|
+
: pluralDefault;
|
|
2910
|
+
const orgPluralRule = isObject(options.pluralRules) &&
|
|
2911
|
+
isString(locale) &&
|
|
2912
|
+
isFunction(options.pluralRules[locale])
|
|
2913
|
+
? pluralDefault
|
|
2914
|
+
: undefined;
|
|
2915
|
+
const plural = (messages) => {
|
|
2916
|
+
return messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
|
|
2917
|
+
};
|
|
2918
|
+
const _list = options.list || [];
|
|
2919
|
+
const list = (index) => _list[index];
|
|
2920
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2921
|
+
const _named = options.named || {};
|
|
2922
|
+
isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
|
|
2923
|
+
const named = (key) => _named[key];
|
|
2924
|
+
function message(key, useLinked) {
|
|
2925
|
+
// prettier-ignore
|
|
2926
|
+
const msg = isFunction(options.messages)
|
|
2927
|
+
? options.messages(key, !!useLinked)
|
|
2928
|
+
: isObject(options.messages)
|
|
2929
|
+
? options.messages[key]
|
|
2930
|
+
: false;
|
|
2931
|
+
return !msg
|
|
2932
|
+
? options.parent
|
|
2933
|
+
? options.parent.message(key) // resolve from parent messages
|
|
2934
|
+
: DEFAULT_MESSAGE
|
|
2935
|
+
: msg;
|
|
2936
|
+
}
|
|
2937
|
+
const _modifier = (name) => options.modifiers
|
|
2938
|
+
? options.modifiers[name]
|
|
2939
|
+
: DEFAULT_MODIFIER;
|
|
2940
|
+
const normalize = isPlainObject(options.processor) && isFunction(options.processor.normalize)
|
|
2941
|
+
? options.processor.normalize
|
|
2942
|
+
: DEFAULT_NORMALIZE;
|
|
2943
|
+
const interpolate = isPlainObject(options.processor) &&
|
|
2944
|
+
isFunction(options.processor.interpolate)
|
|
2945
|
+
? options.processor.interpolate
|
|
2946
|
+
: DEFAULT_INTERPOLATE;
|
|
2947
|
+
const type = isPlainObject(options.processor) && isString(options.processor.type)
|
|
2948
|
+
? options.processor.type
|
|
2949
|
+
: DEFAULT_MESSAGE_DATA_TYPE;
|
|
2950
|
+
const linked = (key, ...args) => {
|
|
2951
|
+
const [arg1, arg2] = args;
|
|
2952
|
+
let type = 'text';
|
|
2953
|
+
let modifier = '';
|
|
2954
|
+
if (args.length === 1) {
|
|
2955
|
+
if (isObject(arg1)) {
|
|
2956
|
+
modifier = arg1.modifier || modifier;
|
|
2957
|
+
type = arg1.type || type;
|
|
2958
|
+
}
|
|
2959
|
+
else if (isString(arg1)) {
|
|
2960
|
+
modifier = arg1 || modifier;
|
|
2961
|
+
}
|
|
3221
2962
|
}
|
|
3222
|
-
|
|
3223
|
-
|
|
3224
|
-
|
|
3225
|
-
value = arg1;
|
|
3226
|
-
}
|
|
3227
|
-
else {
|
|
3228
|
-
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
3229
|
-
}
|
|
3230
|
-
if (isString(arg2)) {
|
|
3231
|
-
options.key = arg2;
|
|
3232
|
-
}
|
|
3233
|
-
else if (isPlainObject(arg2)) {
|
|
3234
|
-
Object.keys(arg2).forEach(key => {
|
|
3235
|
-
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
3236
|
-
overrides[key] = arg2[key];
|
|
2963
|
+
else if (args.length === 2) {
|
|
2964
|
+
if (isString(arg1)) {
|
|
2965
|
+
modifier = arg1 || modifier;
|
|
3237
2966
|
}
|
|
3238
|
-
|
|
3239
|
-
|
|
2967
|
+
if (isString(arg2)) {
|
|
2968
|
+
type = arg2 || type;
|
|
3240
2969
|
}
|
|
3241
|
-
});
|
|
3242
|
-
}
|
|
3243
|
-
if (isString(arg3)) {
|
|
3244
|
-
options.locale = arg3;
|
|
3245
|
-
}
|
|
3246
|
-
else if (isPlainObject(arg3)) {
|
|
3247
|
-
overrides = arg3;
|
|
3248
|
-
}
|
|
3249
|
-
if (isPlainObject(arg4)) {
|
|
3250
|
-
overrides = arg4;
|
|
3251
|
-
}
|
|
3252
|
-
return [options.key || '', value, options, overrides];
|
|
3253
|
-
}
|
|
3254
|
-
/** @internal */
|
|
3255
|
-
function clearDateTimeFormat(ctx, locale, format) {
|
|
3256
|
-
const context = ctx;
|
|
3257
|
-
for (const key in format) {
|
|
3258
|
-
const id = `${locale}__${key}`;
|
|
3259
|
-
if (!context.__datetimeFormatters.has(id)) {
|
|
3260
|
-
continue;
|
|
3261
2970
|
}
|
|
3262
|
-
|
|
3263
|
-
|
|
2971
|
+
const ret = message(key, true)(ctx);
|
|
2972
|
+
const msg =
|
|
2973
|
+
// The message in vnode resolved with linked are returned as an array by processor.nomalize
|
|
2974
|
+
type === 'vnode' && isArray(ret) && modifier
|
|
2975
|
+
? ret[0]
|
|
2976
|
+
: ret;
|
|
2977
|
+
return modifier ? _modifier(modifier)(msg, type) : msg;
|
|
2978
|
+
};
|
|
2979
|
+
const ctx = {
|
|
2980
|
+
["list" /* HelperNameMap.LIST */]: list,
|
|
2981
|
+
["named" /* HelperNameMap.NAMED */]: named,
|
|
2982
|
+
["plural" /* HelperNameMap.PLURAL */]: plural,
|
|
2983
|
+
["linked" /* HelperNameMap.LINKED */]: linked,
|
|
2984
|
+
["message" /* HelperNameMap.MESSAGE */]: message,
|
|
2985
|
+
["type" /* HelperNameMap.TYPE */]: type,
|
|
2986
|
+
["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
|
|
2987
|
+
["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
|
|
2988
|
+
["values" /* HelperNameMap.VALUES */]: assign({}, _list, _named)
|
|
2989
|
+
};
|
|
2990
|
+
return ctx;
|
|
3264
2991
|
}
|
|
3265
2992
|
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
return MISSING_RESOLVE_VALUE;
|
|
3273
|
-
}
|
|
3274
|
-
const [key, value, options, overrides] = parseNumberArgs(...args);
|
|
2993
|
+
const NOOP_MESSAGE_FUNCTION = () => '';
|
|
2994
|
+
const isMessageFunction = (val) => isFunction(val);
|
|
2995
|
+
// implementation of `translate` function
|
|
2996
|
+
function translate(context, ...args) {
|
|
2997
|
+
const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
|
|
2998
|
+
const [key, options] = parseTranslateArgs(...args);
|
|
3275
2999
|
const missingWarn = isBoolean(options.missingWarn)
|
|
3276
3000
|
? options.missingWarn
|
|
3277
3001
|
: context.missingWarn;
|
|
3278
3002
|
const fallbackWarn = isBoolean(options.fallbackWarn)
|
|
3279
3003
|
? options.fallbackWarn
|
|
3280
3004
|
: context.fallbackWarn;
|
|
3281
|
-
const
|
|
3005
|
+
const escapeParameter = isBoolean(options.escapeParameter)
|
|
3006
|
+
? options.escapeParameter
|
|
3007
|
+
: context.escapeParameter;
|
|
3008
|
+
const resolvedMessage = !!options.resolvedMessage;
|
|
3009
|
+
// prettier-ignore
|
|
3010
|
+
const defaultMsgOrKey = isString(options.default) || isBoolean(options.default) // default by function option
|
|
3011
|
+
? !isBoolean(options.default)
|
|
3012
|
+
? options.default
|
|
3013
|
+
: (!messageCompiler ? () => key : key)
|
|
3014
|
+
: fallbackFormat // default by `fallbackFormat` option
|
|
3015
|
+
? (!messageCompiler ? () => key : key)
|
|
3016
|
+
: null;
|
|
3017
|
+
const enableDefaultMsg = fallbackFormat ||
|
|
3018
|
+
(defaultMsgOrKey != null &&
|
|
3019
|
+
(isString(defaultMsgOrKey) || isFunction(defaultMsgOrKey)));
|
|
3282
3020
|
const locale = getLocale(context, options);
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
|
|
3021
|
+
// escape params
|
|
3022
|
+
escapeParameter && escapeParams(options);
|
|
3023
|
+
// resolve message format
|
|
3024
|
+
// eslint-disable-next-line prefer-const
|
|
3025
|
+
let [formatScope, targetLocale, message] = !resolvedMessage
|
|
3026
|
+
? resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn)
|
|
3027
|
+
: [
|
|
3028
|
+
key,
|
|
3029
|
+
locale,
|
|
3030
|
+
messages[locale] || {}
|
|
3031
|
+
];
|
|
3032
|
+
// NOTE:
|
|
3033
|
+
// Fix to work around `ssrTransfrom` bug in Vite.
|
|
3034
|
+
// https://github.com/vitejs/vite/issues/4306
|
|
3035
|
+
// To get around this, use temporary variables.
|
|
3036
|
+
// https://github.com/nuxt/framework/issues/1461#issuecomment-954606243
|
|
3037
|
+
let format = formatScope;
|
|
3038
|
+
// if you use default message, set it as message format!
|
|
3039
|
+
let cacheBaseKey = key;
|
|
3040
|
+
if (!resolvedMessage &&
|
|
3041
|
+
!(isString(format) ||
|
|
3042
|
+
isMessageAST(format) ||
|
|
3043
|
+
isMessageFunction(format))) {
|
|
3044
|
+
if (enableDefaultMsg) {
|
|
3045
|
+
format = defaultMsgOrKey;
|
|
3046
|
+
cacheBaseKey = format;
|
|
3047
|
+
}
|
|
3287
3048
|
}
|
|
3288
|
-
//
|
|
3289
|
-
|
|
3049
|
+
// checking message format and target locale
|
|
3050
|
+
if (!resolvedMessage &&
|
|
3051
|
+
(!(isString(format) ||
|
|
3052
|
+
isMessageAST(format) ||
|
|
3053
|
+
isMessageFunction(format)) ||
|
|
3054
|
+
!isString(targetLocale))) {
|
|
3055
|
+
return unresolving ? NOT_REOSLVED : key;
|
|
3056
|
+
}
|
|
3057
|
+
// TODO: refactor
|
|
3058
|
+
if (isString(format) && context.messageCompiler == null) {
|
|
3059
|
+
warn(`The message format compilation is not supported in this build. ` +
|
|
3060
|
+
`Because message compiler isn't included. ` +
|
|
3061
|
+
`You need to pre-compilation all message format. ` +
|
|
3062
|
+
`So translate function return '${key}'.`);
|
|
3063
|
+
return key;
|
|
3064
|
+
}
|
|
3065
|
+
// setup compile error detecting
|
|
3066
|
+
let occurred = false;
|
|
3067
|
+
const onError = () => {
|
|
3068
|
+
occurred = true;
|
|
3069
|
+
};
|
|
3070
|
+
// compile message format
|
|
3071
|
+
const msg = !isMessageFunction(format)
|
|
3072
|
+
? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError)
|
|
3073
|
+
: format;
|
|
3074
|
+
// if occurred compile error, return the message format
|
|
3075
|
+
if (occurred) {
|
|
3076
|
+
return format;
|
|
3077
|
+
}
|
|
3078
|
+
// evaluate message with context
|
|
3079
|
+
const ctxOptions = getMessageContextOptions(context, targetLocale, message, options);
|
|
3080
|
+
const msgContext = createMessageContext(ctxOptions);
|
|
3081
|
+
const messaged = evaluateMessage(context, msg, msgContext);
|
|
3082
|
+
// if use post translation option, proceed it with handler
|
|
3083
|
+
const ret = postTranslation
|
|
3084
|
+
? postTranslation(messaged, key)
|
|
3085
|
+
: messaged;
|
|
3086
|
+
// NOTE: experimental !!
|
|
3087
|
+
{
|
|
3088
|
+
// prettier-ignore
|
|
3089
|
+
const payloads = {
|
|
3090
|
+
timestamp: Date.now(),
|
|
3091
|
+
key: isString(key)
|
|
3092
|
+
? key
|
|
3093
|
+
: isMessageFunction(format)
|
|
3094
|
+
? format.key
|
|
3095
|
+
: '',
|
|
3096
|
+
locale: targetLocale || (isMessageFunction(format)
|
|
3097
|
+
? format.locale
|
|
3098
|
+
: ''),
|
|
3099
|
+
format: isString(format)
|
|
3100
|
+
? format
|
|
3101
|
+
: isMessageFunction(format)
|
|
3102
|
+
? format.source
|
|
3103
|
+
: '',
|
|
3104
|
+
message: ret
|
|
3105
|
+
};
|
|
3106
|
+
payloads.meta = assign({}, context.__meta, getAdditionalMeta() || {});
|
|
3107
|
+
translateDevTools(payloads);
|
|
3108
|
+
}
|
|
3109
|
+
return ret;
|
|
3110
|
+
}
|
|
3111
|
+
function escapeParams(options) {
|
|
3112
|
+
if (isArray(options.list)) {
|
|
3113
|
+
options.list = options.list.map(item => isString(item) ? escapeHtml(item) : item);
|
|
3114
|
+
}
|
|
3115
|
+
else if (isObject(options.named)) {
|
|
3116
|
+
Object.keys(options.named).forEach(key => {
|
|
3117
|
+
if (isString(options.named[key])) {
|
|
3118
|
+
options.named[key] = escapeHtml(options.named[key]);
|
|
3119
|
+
}
|
|
3120
|
+
});
|
|
3121
|
+
}
|
|
3122
|
+
}
|
|
3123
|
+
function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
|
|
3124
|
+
const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
|
|
3125
|
+
const locales = localeFallbacker(context, fallbackLocale, locale); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
3126
|
+
let message = {};
|
|
3290
3127
|
let targetLocale;
|
|
3291
3128
|
let format = null;
|
|
3292
3129
|
let from = locale;
|
|
3293
3130
|
let to = null;
|
|
3294
|
-
const type = '
|
|
3131
|
+
const type = 'translate';
|
|
3295
3132
|
for (let i = 0; i < locales.length; i++) {
|
|
3296
3133
|
targetLocale = to = locales[i];
|
|
3297
3134
|
if (locale !== targetLocale &&
|
|
3135
|
+
!isAlmostSameLocale(locale, targetLocale) &&
|
|
3298
3136
|
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
3299
|
-
onWarn(getWarnMessage(CoreWarnCodes.
|
|
3137
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_TRANSLATE, {
|
|
3300
3138
|
key,
|
|
3301
3139
|
target: targetLocale
|
|
3302
3140
|
}));
|
|
@@ -3314,95 +3152,257 @@ function number(context, ...args) {
|
|
|
3314
3152
|
});
|
|
3315
3153
|
}
|
|
3316
3154
|
}
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3155
|
+
message =
|
|
3156
|
+
messages[targetLocale] || {};
|
|
3157
|
+
// for vue-devtools timeline event
|
|
3158
|
+
let start = null;
|
|
3159
|
+
let startTag;
|
|
3160
|
+
let endTag;
|
|
3161
|
+
if (inBrowser) {
|
|
3162
|
+
start = window.performance.now();
|
|
3163
|
+
startTag = 'intlify-message-resolve-start';
|
|
3164
|
+
endTag = 'intlify-message-resolve-end';
|
|
3165
|
+
mark && mark(startTag);
|
|
3166
|
+
}
|
|
3167
|
+
if ((format = resolveValue(message, key)) === null) {
|
|
3168
|
+
// if null, resolve with object key path
|
|
3169
|
+
format = message[key]; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
3170
|
+
}
|
|
3171
|
+
// for vue-devtools timeline event
|
|
3172
|
+
if (inBrowser) {
|
|
3173
|
+
const end = window.performance.now();
|
|
3174
|
+
const emitter = context.__v_emitter;
|
|
3175
|
+
if (emitter && start && format) {
|
|
3176
|
+
emitter.emit('message-resolve', {
|
|
3177
|
+
type: 'message-resolve',
|
|
3178
|
+
key,
|
|
3179
|
+
message: format,
|
|
3180
|
+
time: end - start,
|
|
3181
|
+
groupId: `${type}:${key}`
|
|
3182
|
+
});
|
|
3183
|
+
}
|
|
3184
|
+
if (startTag && endTag && mark && measure) {
|
|
3185
|
+
mark(endTag);
|
|
3186
|
+
measure('intlify message resolve', startTag, endTag);
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
if (isString(format) || isMessageAST(format) || isMessageFunction(format)) {
|
|
3321
3190
|
break;
|
|
3322
|
-
|
|
3191
|
+
}
|
|
3192
|
+
if (!isImplicitFallback(targetLocale, locales)) {
|
|
3193
|
+
const missingRet = handleMissing(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
3194
|
+
key, targetLocale, missingWarn, type);
|
|
3195
|
+
if (missingRet !== key) {
|
|
3196
|
+
format = missingRet;
|
|
3197
|
+
}
|
|
3198
|
+
}
|
|
3323
3199
|
from = to;
|
|
3324
3200
|
}
|
|
3325
|
-
|
|
3326
|
-
|
|
3327
|
-
|
|
3201
|
+
return [format, targetLocale, message];
|
|
3202
|
+
}
|
|
3203
|
+
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
|
|
3204
|
+
const { messageCompiler, warnHtmlMessage } = context;
|
|
3205
|
+
if (isMessageFunction(format)) {
|
|
3206
|
+
const msg = format;
|
|
3207
|
+
msg.locale = msg.locale || targetLocale;
|
|
3208
|
+
msg.key = msg.key || key;
|
|
3209
|
+
return msg;
|
|
3328
3210
|
}
|
|
3329
|
-
|
|
3330
|
-
|
|
3331
|
-
|
|
3211
|
+
if (messageCompiler == null) {
|
|
3212
|
+
const msg = (() => format);
|
|
3213
|
+
msg.locale = targetLocale;
|
|
3214
|
+
msg.key = key;
|
|
3215
|
+
return msg;
|
|
3332
3216
|
}
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3217
|
+
// for vue-devtools timeline event
|
|
3218
|
+
let start = null;
|
|
3219
|
+
let startTag;
|
|
3220
|
+
let endTag;
|
|
3221
|
+
if (inBrowser) {
|
|
3222
|
+
start = window.performance.now();
|
|
3223
|
+
startTag = 'intlify-message-compilation-start';
|
|
3224
|
+
endTag = 'intlify-message-compilation-end';
|
|
3225
|
+
mark && mark(startTag);
|
|
3337
3226
|
}
|
|
3338
|
-
|
|
3227
|
+
const msg = messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, onError));
|
|
3228
|
+
// for vue-devtools timeline event
|
|
3229
|
+
if (inBrowser) {
|
|
3230
|
+
const end = window.performance.now();
|
|
3231
|
+
const emitter = context.__v_emitter;
|
|
3232
|
+
if (emitter && start) {
|
|
3233
|
+
emitter.emit('message-compilation', {
|
|
3234
|
+
type: 'message-compilation',
|
|
3235
|
+
message: format,
|
|
3236
|
+
time: end - start,
|
|
3237
|
+
groupId: `${'translate'}:${key}`
|
|
3238
|
+
});
|
|
3239
|
+
}
|
|
3240
|
+
if (startTag && endTag && mark && measure) {
|
|
3241
|
+
mark(endTag);
|
|
3242
|
+
measure('intlify message compilation', startTag, endTag);
|
|
3243
|
+
}
|
|
3244
|
+
}
|
|
3245
|
+
msg.locale = targetLocale;
|
|
3246
|
+
msg.key = key;
|
|
3247
|
+
msg.source = format;
|
|
3248
|
+
return msg;
|
|
3249
|
+
}
|
|
3250
|
+
function evaluateMessage(context, msg, msgCtx) {
|
|
3251
|
+
// for vue-devtools timeline event
|
|
3252
|
+
let start = null;
|
|
3253
|
+
let startTag;
|
|
3254
|
+
let endTag;
|
|
3255
|
+
if (inBrowser) {
|
|
3256
|
+
start = window.performance.now();
|
|
3257
|
+
startTag = 'intlify-message-evaluation-start';
|
|
3258
|
+
endTag = 'intlify-message-evaluation-end';
|
|
3259
|
+
mark && mark(startTag);
|
|
3260
|
+
}
|
|
3261
|
+
const messaged = msg(msgCtx);
|
|
3262
|
+
// for vue-devtools timeline event
|
|
3263
|
+
if (inBrowser) {
|
|
3264
|
+
const end = window.performance.now();
|
|
3265
|
+
const emitter = context.__v_emitter;
|
|
3266
|
+
if (emitter && start) {
|
|
3267
|
+
emitter.emit('message-evaluation', {
|
|
3268
|
+
type: 'message-evaluation',
|
|
3269
|
+
value: messaged,
|
|
3270
|
+
time: end - start,
|
|
3271
|
+
groupId: `${'translate'}:${msg.key}`
|
|
3272
|
+
});
|
|
3273
|
+
}
|
|
3274
|
+
if (startTag && endTag && mark && measure) {
|
|
3275
|
+
mark(endTag);
|
|
3276
|
+
measure('intlify message evaluation', startTag, endTag);
|
|
3277
|
+
}
|
|
3278
|
+
}
|
|
3279
|
+
return messaged;
|
|
3339
3280
|
}
|
|
3340
3281
|
/** @internal */
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
'style',
|
|
3344
|
-
'currency',
|
|
3345
|
-
'currencyDisplay',
|
|
3346
|
-
'currencySign',
|
|
3347
|
-
'useGrouping',
|
|
3348
|
-
'minimumIntegerDigits',
|
|
3349
|
-
'minimumFractionDigits',
|
|
3350
|
-
'maximumFractionDigits',
|
|
3351
|
-
'minimumSignificantDigits',
|
|
3352
|
-
'maximumSignificantDigits',
|
|
3353
|
-
'compactDisplay',
|
|
3354
|
-
'notation',
|
|
3355
|
-
'signDisplay',
|
|
3356
|
-
'unit',
|
|
3357
|
-
'unitDisplay',
|
|
3358
|
-
'roundingMode',
|
|
3359
|
-
'roundingPriority',
|
|
3360
|
-
'roundingIncrement',
|
|
3361
|
-
'trailingZeroDisplay'
|
|
3362
|
-
];
|
|
3363
|
-
/** @internal */
|
|
3364
|
-
function parseNumberArgs(...args) {
|
|
3365
|
-
const [arg1, arg2, arg3, arg4] = args;
|
|
3282
|
+
function parseTranslateArgs(...args) {
|
|
3283
|
+
const [arg1, arg2, arg3] = args;
|
|
3366
3284
|
const options = {};
|
|
3367
|
-
|
|
3368
|
-
|
|
3285
|
+
if (!isString(arg1) &&
|
|
3286
|
+
!isNumber(arg1) &&
|
|
3287
|
+
!isMessageFunction(arg1) &&
|
|
3288
|
+
!isMessageAST(arg1)) {
|
|
3369
3289
|
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
3370
3290
|
}
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3291
|
+
// prettier-ignore
|
|
3292
|
+
const key = isNumber(arg1)
|
|
3293
|
+
? String(arg1)
|
|
3294
|
+
: isMessageFunction(arg1)
|
|
3295
|
+
? arg1
|
|
3296
|
+
: arg1;
|
|
3297
|
+
if (isNumber(arg2)) {
|
|
3298
|
+
options.plural = arg2;
|
|
3374
3299
|
}
|
|
3375
|
-
else if (
|
|
3376
|
-
|
|
3377
|
-
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
3378
|
-
overrides[key] = arg2[key];
|
|
3379
|
-
}
|
|
3380
|
-
else {
|
|
3381
|
-
options[key] = arg2[key];
|
|
3382
|
-
}
|
|
3383
|
-
});
|
|
3300
|
+
else if (isString(arg2)) {
|
|
3301
|
+
options.default = arg2;
|
|
3384
3302
|
}
|
|
3385
|
-
if (
|
|
3386
|
-
options.
|
|
3303
|
+
else if (isPlainObject(arg2) && !isEmptyObject(arg2)) {
|
|
3304
|
+
options.named = arg2;
|
|
3305
|
+
}
|
|
3306
|
+
else if (isArray(arg2)) {
|
|
3307
|
+
options.list = arg2;
|
|
3308
|
+
}
|
|
3309
|
+
if (isNumber(arg3)) {
|
|
3310
|
+
options.plural = arg3;
|
|
3311
|
+
}
|
|
3312
|
+
else if (isString(arg3)) {
|
|
3313
|
+
options.default = arg3;
|
|
3387
3314
|
}
|
|
3388
3315
|
else if (isPlainObject(arg3)) {
|
|
3389
|
-
|
|
3316
|
+
assign(options, arg3);
|
|
3390
3317
|
}
|
|
3391
|
-
|
|
3392
|
-
|
|
3318
|
+
return [key, options];
|
|
3319
|
+
}
|
|
3320
|
+
function getCompileContext(context, locale, key, source, warnHtmlMessage, onError) {
|
|
3321
|
+
return {
|
|
3322
|
+
locale,
|
|
3323
|
+
key,
|
|
3324
|
+
warnHtmlMessage,
|
|
3325
|
+
onError: (err) => {
|
|
3326
|
+
onError && onError(err);
|
|
3327
|
+
{
|
|
3328
|
+
const _source = getSourceForCodeFrame(source);
|
|
3329
|
+
const message = `Message compilation error: ${err.message}`;
|
|
3330
|
+
const codeFrame = err.location &&
|
|
3331
|
+
_source &&
|
|
3332
|
+
generateCodeFrame(_source, err.location.start.offset, err.location.end.offset);
|
|
3333
|
+
const emitter = context.__v_emitter;
|
|
3334
|
+
if (emitter && _source) {
|
|
3335
|
+
emitter.emit('compile-error', {
|
|
3336
|
+
message: _source,
|
|
3337
|
+
error: err.message,
|
|
3338
|
+
start: err.location && err.location.start.offset,
|
|
3339
|
+
end: err.location && err.location.end.offset,
|
|
3340
|
+
groupId: `${'translate'}:${key}`
|
|
3341
|
+
});
|
|
3342
|
+
}
|
|
3343
|
+
console.error(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
3344
|
+
}
|
|
3345
|
+
},
|
|
3346
|
+
onCacheKey: (source) => generateFormatCacheKey(locale, key, source)
|
|
3347
|
+
};
|
|
3348
|
+
}
|
|
3349
|
+
function getSourceForCodeFrame(source) {
|
|
3350
|
+
if (isString(source)) {
|
|
3351
|
+
return source;
|
|
3352
|
+
}
|
|
3353
|
+
else {
|
|
3354
|
+
if (source.loc && source.loc.source) {
|
|
3355
|
+
return source.loc.source;
|
|
3356
|
+
}
|
|
3393
3357
|
}
|
|
3394
|
-
return [options.key || '', value, options, overrides];
|
|
3395
3358
|
}
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
const
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
if (
|
|
3402
|
-
|
|
3359
|
+
function getMessageContextOptions(context, locale, message, options) {
|
|
3360
|
+
const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
|
|
3361
|
+
const resolveMessage = (key, useLinked) => {
|
|
3362
|
+
let val = resolveValue(message, key);
|
|
3363
|
+
// fallback
|
|
3364
|
+
if (val == null && (fallbackContext || useLinked)) {
|
|
3365
|
+
const [, , message] = resolveMessageFormat(fallbackContext || context, // NOTE: if has fallbackContext, fallback to root, else if use linked, fallback to local context
|
|
3366
|
+
key, locale, fallbackLocale, fallbackWarn, missingWarn);
|
|
3367
|
+
val = resolveValue(message, key);
|
|
3403
3368
|
}
|
|
3404
|
-
|
|
3369
|
+
if (isString(val) || isMessageAST(val)) {
|
|
3370
|
+
let occurred = false;
|
|
3371
|
+
const onError = () => {
|
|
3372
|
+
occurred = true;
|
|
3373
|
+
};
|
|
3374
|
+
const msg = compileMessageFormat(context, key, locale, val, key, onError);
|
|
3375
|
+
return !occurred
|
|
3376
|
+
? msg
|
|
3377
|
+
: NOOP_MESSAGE_FUNCTION;
|
|
3378
|
+
}
|
|
3379
|
+
else if (isMessageFunction(val)) {
|
|
3380
|
+
return val;
|
|
3381
|
+
}
|
|
3382
|
+
else {
|
|
3383
|
+
// TODO: should be implemented warning message
|
|
3384
|
+
return NOOP_MESSAGE_FUNCTION;
|
|
3385
|
+
}
|
|
3386
|
+
};
|
|
3387
|
+
const ctxOptions = {
|
|
3388
|
+
locale,
|
|
3389
|
+
modifiers,
|
|
3390
|
+
pluralRules,
|
|
3391
|
+
messages: resolveMessage
|
|
3392
|
+
};
|
|
3393
|
+
if (context.processor) {
|
|
3394
|
+
ctxOptions.processor = context.processor;
|
|
3395
|
+
}
|
|
3396
|
+
if (options.list) {
|
|
3397
|
+
ctxOptions.list = options.list;
|
|
3398
|
+
}
|
|
3399
|
+
if (options.named) {
|
|
3400
|
+
ctxOptions.named = options.named;
|
|
3405
3401
|
}
|
|
3402
|
+
if (isNumber(options.plural)) {
|
|
3403
|
+
ctxOptions.pluralIndex = options.plural;
|
|
3404
|
+
}
|
|
3405
|
+
return ctxOptions;
|
|
3406
3406
|
}
|
|
3407
3407
|
|
|
3408
3408
|
export { CORE_ERROR_CODES_EXTEND_POINT, CORE_WARN_CODES_EXTEND_POINT, CompileErrorCodes, CoreErrorCodes, CoreWarnCodes, DATETIME_FORMAT_OPTIONS_KEYS, DEFAULT_LOCALE, DEFAULT_MESSAGE_DATA_TYPE, MISSING_RESOLVE_VALUE, NOT_REOSLVED, NUMBER_FORMAT_OPTIONS_KEYS, VERSION, clearCompileCache, clearDateTimeFormat, clearNumberFormat, compile, createCompileError, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple, getAdditionalMeta, getDevToolsHook, getFallbackContext, getLocale, getWarnMessage, handleMissing, initI18nDevTools, isAlmostSameLocale, isImplicitFallback, isMessageAST, isMessageFunction, isTranslateFallbackWarn, isTranslateMissingWarn, number, parse, parseDateTimeArgs, parseNumberArgs, parseTranslateArgs, registerLocaleFallbacker, registerMessageCompiler, registerMessageResolver, resolveLocale, resolveValue, resolveWithKeyValue, setAdditionalMeta, setDevToolsHook, setFallbackContext, translate, translateDevTools, updateFallbackLocale };
|