@intlify/core-base 9.2.0-beta.7 → 9.2.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.js +166 -30
- package/dist/core-base.cjs.prod.js +166 -30
- package/dist/core-base.d.ts +27 -9
- package/dist/core-base.esm-browser.js +201 -48
- package/dist/core-base.esm-browser.prod.js +3 -3
- package/dist/core-base.esm-bundler.js +165 -32
- package/dist/core-base.global.js +205 -48
- package/dist/core-base.global.prod.js +3 -3
- package/index.mjs +1 -0
- package/package.json +19 -6
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* core-base v9.2.0
|
|
3
|
-
* (c)
|
|
2
|
+
* core-base v9.2.0
|
|
3
|
+
* (c) 2022 kazuya kawaguchi
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
|
-
import { isObject,
|
|
6
|
+
import { isObject, isString, isFunction, isNumber, isPlainObject, toDisplayString, isArray, format, isBoolean, assign, isRegExp, warn, escapeHtml, inBrowser, mark, measure, isEmptyObject, generateCodeFrame, generateFormatCacheKey, isDate, getGlobalThis } from '@intlify/shared';
|
|
7
7
|
import { defaultOnError, baseCompile, CompileErrorCodes, createCompileError } from '@intlify/message-compiler';
|
|
8
8
|
export { CompileErrorCodes, createCompileError } from '@intlify/message-compiler';
|
|
9
9
|
import { IntlifyDevToolsHooks } from '@intlify/devtools-if';
|
|
@@ -325,14 +325,15 @@ function createMessageContext(options = {}) {
|
|
|
325
325
|
isFunction(options.pluralRules[locale])
|
|
326
326
|
? pluralDefault
|
|
327
327
|
: undefined;
|
|
328
|
-
const plural = (messages) =>
|
|
328
|
+
const plural = (messages) => {
|
|
329
|
+
return messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
|
|
330
|
+
};
|
|
329
331
|
const _list = options.list || [];
|
|
330
332
|
const list = (index) => _list[index];
|
|
331
333
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
332
334
|
const _named = options.named || {};
|
|
333
335
|
isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
|
|
334
336
|
const named = (key) => _named[key];
|
|
335
|
-
// TODO: need to design resolve message function?
|
|
336
337
|
function message(key) {
|
|
337
338
|
// prettier-ignore
|
|
338
339
|
const msg = isFunction(options.messages)
|
|
@@ -359,15 +360,39 @@ function createMessageContext(options = {}) {
|
|
|
359
360
|
const type = isPlainObject(options.processor) && isString(options.processor.type)
|
|
360
361
|
? options.processor.type
|
|
361
362
|
: DEFAULT_MESSAGE_DATA_TYPE;
|
|
363
|
+
const linked = (key, ...args) => {
|
|
364
|
+
const [arg1, arg2] = args;
|
|
365
|
+
let type = 'text';
|
|
366
|
+
let modifier = '';
|
|
367
|
+
if (args.length === 1) {
|
|
368
|
+
if (isObject(arg1)) {
|
|
369
|
+
modifier = arg1.modifier || modifier;
|
|
370
|
+
type = arg1.type || type;
|
|
371
|
+
}
|
|
372
|
+
else if (isString(arg1)) {
|
|
373
|
+
modifier = arg1 || modifier;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else if (args.length === 2) {
|
|
377
|
+
if (isString(arg1)) {
|
|
378
|
+
modifier = arg1 || modifier;
|
|
379
|
+
}
|
|
380
|
+
if (isString(arg2)) {
|
|
381
|
+
type = arg2 || type;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
let msg = message(key)(ctx);
|
|
385
|
+
// The message in vnode resolved with linked are returned as an array by processor.nomalize
|
|
386
|
+
if (type === 'vnode' && isArray(msg) && modifier) {
|
|
387
|
+
msg = msg[0];
|
|
388
|
+
}
|
|
389
|
+
return modifier ? _modifier(modifier)(msg, type) : msg;
|
|
390
|
+
};
|
|
362
391
|
const ctx = {
|
|
363
392
|
["list" /* LIST */]: list,
|
|
364
393
|
["named" /* NAMED */]: named,
|
|
365
394
|
["plural" /* PLURAL */]: plural,
|
|
366
|
-
["linked" /* LINKED */]:
|
|
367
|
-
// TODO: should check `key`
|
|
368
|
-
const msg = message(key)(ctx);
|
|
369
|
-
return isString(modifier) ? _modifier(modifier)(msg) : msg;
|
|
370
|
-
},
|
|
395
|
+
["linked" /* LINKED */]: linked,
|
|
371
396
|
["message" /* MESSAGE */]: message,
|
|
372
397
|
["type" /* TYPE */]: type,
|
|
373
398
|
["interpolate" /* INTERPOLATE */]: interpolate,
|
|
@@ -541,18 +566,37 @@ function appendItemToChain(chain, target, blocks) {
|
|
|
541
566
|
* Intlify core-base version
|
|
542
567
|
* @internal
|
|
543
568
|
*/
|
|
544
|
-
const VERSION = '9.2.0
|
|
569
|
+
const VERSION = '9.2.0';
|
|
545
570
|
const NOT_REOSLVED = -1;
|
|
546
571
|
const DEFAULT_LOCALE = 'en-US';
|
|
547
572
|
const MISSING_RESOLVE_VALUE = '';
|
|
573
|
+
const capitalize = (str) => `${str.charAt(0).toLocaleUpperCase()}${str.substr(1)}`;
|
|
548
574
|
function getDefaultLinkedModifiers() {
|
|
549
575
|
return {
|
|
550
|
-
upper: (val) =>
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
576
|
+
upper: (val, type) => {
|
|
577
|
+
// prettier-ignore
|
|
578
|
+
return type === 'text' && isString(val)
|
|
579
|
+
? val.toUpperCase()
|
|
580
|
+
: type === 'vnode' && isObject(val) && '__v_isVNode' in val
|
|
581
|
+
? val.children.toUpperCase()
|
|
582
|
+
: val;
|
|
583
|
+
},
|
|
584
|
+
lower: (val, type) => {
|
|
585
|
+
// prettier-ignore
|
|
586
|
+
return type === 'text' && isString(val)
|
|
587
|
+
? val.toLowerCase()
|
|
588
|
+
: type === 'vnode' && isObject(val) && '__v_isVNode' in val
|
|
589
|
+
? val.children.toLowerCase()
|
|
590
|
+
: val;
|
|
591
|
+
},
|
|
592
|
+
capitalize: (val, type) => {
|
|
593
|
+
// prettier-ignore
|
|
594
|
+
return (type === 'text' && isString(val)
|
|
595
|
+
? capitalize(val)
|
|
596
|
+
: type === 'vnode' && isObject(val) && '__v_isVNode' in val
|
|
597
|
+
? capitalize(val.children)
|
|
598
|
+
: val);
|
|
599
|
+
}
|
|
556
600
|
};
|
|
557
601
|
}
|
|
558
602
|
let _compiler;
|
|
@@ -587,6 +631,11 @@ const setAdditionalMeta = (meta) => {
|
|
|
587
631
|
_additionalMeta = meta;
|
|
588
632
|
};
|
|
589
633
|
const getAdditionalMeta = () => _additionalMeta;
|
|
634
|
+
let _fallbackContext = null;
|
|
635
|
+
const setFallbackContext = (context) => {
|
|
636
|
+
_fallbackContext = context;
|
|
637
|
+
};
|
|
638
|
+
const getFallbackContext = () => _fallbackContext;
|
|
590
639
|
// ID for CoreContext
|
|
591
640
|
let _cid = 0;
|
|
592
641
|
function createCoreContext(options = {}) {
|
|
@@ -638,6 +687,9 @@ function createCoreContext(options = {}) {
|
|
|
638
687
|
const localeFallbacker = isFunction(options.localeFallbacker)
|
|
639
688
|
? options.localeFallbacker
|
|
640
689
|
: _fallbacker || fallbackWithSimple;
|
|
690
|
+
const fallbackContext = isObject(options.fallbackContext)
|
|
691
|
+
? options.fallbackContext
|
|
692
|
+
: undefined;
|
|
641
693
|
const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
|
|
642
694
|
// setup internal options
|
|
643
695
|
const internalOptions = options;
|
|
@@ -671,6 +723,7 @@ function createCoreContext(options = {}) {
|
|
|
671
723
|
messageCompiler,
|
|
672
724
|
messageResolver,
|
|
673
725
|
localeFallbacker,
|
|
726
|
+
fallbackContext,
|
|
674
727
|
onWarn,
|
|
675
728
|
__meta
|
|
676
729
|
};
|
|
@@ -778,7 +831,7 @@ function compileToFunction(source, options = {}) {
|
|
|
778
831
|
}
|
|
779
832
|
|
|
780
833
|
let code = CompileErrorCodes.__EXTEND_POINT__;
|
|
781
|
-
const inc = () => code
|
|
834
|
+
const inc = () => ++code;
|
|
782
835
|
const CoreErrorCodes = {
|
|
783
836
|
INVALID_ARGUMENT: code,
|
|
784
837
|
INVALID_DATE_ARGUMENT: inc(),
|
|
@@ -800,7 +853,7 @@ const NOOP_MESSAGE_FUNCTION = () => '';
|
|
|
800
853
|
const isMessageFunction = (val) => isFunction(val);
|
|
801
854
|
// implementation of `translate` function
|
|
802
855
|
function translate(context, ...args) {
|
|
803
|
-
const { fallbackFormat, postTranslation, unresolving, fallbackLocale, messages } = context;
|
|
856
|
+
const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
|
|
804
857
|
const [key, options] = parseTranslateArgs(...args);
|
|
805
858
|
const missingWarn = isBoolean(options.missingWarn)
|
|
806
859
|
? options.missingWarn
|
|
@@ -816,9 +869,9 @@ function translate(context, ...args) {
|
|
|
816
869
|
const defaultMsgOrKey = isString(options.default) || isBoolean(options.default) // default by function option
|
|
817
870
|
? !isBoolean(options.default)
|
|
818
871
|
? options.default
|
|
819
|
-
: key
|
|
872
|
+
: (!messageCompiler ? () => key : key)
|
|
820
873
|
: fallbackFormat // default by `fallbackFormat` option
|
|
821
|
-
? key
|
|
874
|
+
? (!messageCompiler ? () => key : key)
|
|
822
875
|
: '';
|
|
823
876
|
const enableDefaultMsg = fallbackFormat || defaultMsgOrKey !== '';
|
|
824
877
|
const locale = isString(options.locale) ? options.locale : context.locale;
|
|
@@ -826,13 +879,19 @@ function translate(context, ...args) {
|
|
|
826
879
|
escapeParameter && escapeParams(options);
|
|
827
880
|
// resolve message format
|
|
828
881
|
// eslint-disable-next-line prefer-const
|
|
829
|
-
let [
|
|
882
|
+
let [formatScope, targetLocale, message] = !resolvedMessage
|
|
830
883
|
? resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn)
|
|
831
884
|
: [
|
|
832
885
|
key,
|
|
833
886
|
locale,
|
|
834
887
|
messages[locale] || {}
|
|
835
888
|
];
|
|
889
|
+
// NOTE:
|
|
890
|
+
// Fix to work around `ssrTransfrom` bug in Vite.
|
|
891
|
+
// https://github.com/vitejs/vite/issues/4306
|
|
892
|
+
// To get around this, use temporary variables.
|
|
893
|
+
// https://github.com/nuxt/framework/issues/1461#issuecomment-954606243
|
|
894
|
+
let format = formatScope;
|
|
836
895
|
// if you use default message, set it as message format!
|
|
837
896
|
let cacheBaseKey = key;
|
|
838
897
|
if (!resolvedMessage &&
|
|
@@ -873,7 +932,9 @@ function translate(context, ...args) {
|
|
|
873
932
|
const msgContext = createMessageContext(ctxOptions);
|
|
874
933
|
const messaged = evaluateMessage(context, msg, msgContext);
|
|
875
934
|
// if use post translation option, proceed it with handler
|
|
876
|
-
const ret = postTranslation
|
|
935
|
+
const ret = postTranslation
|
|
936
|
+
? postTranslation(messaged, key)
|
|
937
|
+
: messaged;
|
|
877
938
|
// NOTE: experimental !!
|
|
878
939
|
if ((process.env.NODE_ENV !== 'production') || __INTLIFY_PROD_DEVTOOLS__) {
|
|
879
940
|
// prettier-ignore
|
|
@@ -996,6 +1057,12 @@ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
|
|
|
996
1057
|
msg.key = msg.key || key;
|
|
997
1058
|
return msg;
|
|
998
1059
|
}
|
|
1060
|
+
if (messageCompiler == null) {
|
|
1061
|
+
const msg = (() => format);
|
|
1062
|
+
msg.locale = targetLocale;
|
|
1063
|
+
msg.key = key;
|
|
1064
|
+
return msg;
|
|
1065
|
+
}
|
|
999
1066
|
// for vue-devtools timeline event
|
|
1000
1067
|
let start = null;
|
|
1001
1068
|
let startTag;
|
|
@@ -1125,9 +1192,14 @@ function getCompileOptions(context, locale, key, source, warnHtmlMessage, errorD
|
|
|
1125
1192
|
};
|
|
1126
1193
|
}
|
|
1127
1194
|
function getMessageContextOptions(context, locale, message, options) {
|
|
1128
|
-
const { modifiers, pluralRules, messageResolver: resolveValue } = context;
|
|
1195
|
+
const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
|
|
1129
1196
|
const resolveMessage = (key) => {
|
|
1130
|
-
|
|
1197
|
+
let val = resolveValue(message, key);
|
|
1198
|
+
// fallback to root context
|
|
1199
|
+
if (val == null && fallbackContext) {
|
|
1200
|
+
const [, , message] = resolveMessageFormat(fallbackContext, key, locale, fallbackLocale, fallbackWarn, missingWarn);
|
|
1201
|
+
val = resolveValue(message, key);
|
|
1202
|
+
}
|
|
1131
1203
|
if (isString(val)) {
|
|
1132
1204
|
let occurred = false;
|
|
1133
1205
|
const errorDetector = () => {
|
|
@@ -1193,7 +1265,7 @@ function datetime(context, ...args) {
|
|
|
1193
1265
|
const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1194
1266
|
fallbackLocale, locale);
|
|
1195
1267
|
if (!isString(key) || key === '') {
|
|
1196
|
-
return new Intl.DateTimeFormat(locale).format(value);
|
|
1268
|
+
return new Intl.DateTimeFormat(locale, overrides).format(value);
|
|
1197
1269
|
}
|
|
1198
1270
|
// resolve format
|
|
1199
1271
|
let datetimeFormat = {};
|
|
@@ -1249,9 +1321,32 @@ function datetime(context, ...args) {
|
|
|
1249
1321
|
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1250
1322
|
}
|
|
1251
1323
|
/** @internal */
|
|
1324
|
+
const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
1325
|
+
'localeMatcher',
|
|
1326
|
+
'weekday',
|
|
1327
|
+
'era',
|
|
1328
|
+
'year',
|
|
1329
|
+
'month',
|
|
1330
|
+
'day',
|
|
1331
|
+
'hour',
|
|
1332
|
+
'minute',
|
|
1333
|
+
'second',
|
|
1334
|
+
'timeZoneName',
|
|
1335
|
+
'formatMatcher',
|
|
1336
|
+
'hour12',
|
|
1337
|
+
'timeZone',
|
|
1338
|
+
'dateStyle',
|
|
1339
|
+
'timeStyle',
|
|
1340
|
+
'calendar',
|
|
1341
|
+
'dayPeriod',
|
|
1342
|
+
'numberingSystem',
|
|
1343
|
+
'hourCycle',
|
|
1344
|
+
'fractionalSecondDigits'
|
|
1345
|
+
];
|
|
1346
|
+
/** @internal */
|
|
1252
1347
|
function parseDateTimeArgs(...args) {
|
|
1253
1348
|
const [arg1, arg2, arg3, arg4] = args;
|
|
1254
|
-
|
|
1349
|
+
const options = {};
|
|
1255
1350
|
let overrides = {};
|
|
1256
1351
|
let value;
|
|
1257
1352
|
if (isString(arg1)) {
|
|
@@ -1293,7 +1388,14 @@ function parseDateTimeArgs(...args) {
|
|
|
1293
1388
|
options.key = arg2;
|
|
1294
1389
|
}
|
|
1295
1390
|
else if (isPlainObject(arg2)) {
|
|
1296
|
-
|
|
1391
|
+
Object.keys(arg2).forEach(key => {
|
|
1392
|
+
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
1393
|
+
overrides[key] = arg2[key];
|
|
1394
|
+
}
|
|
1395
|
+
else {
|
|
1396
|
+
options[key] = arg2[key];
|
|
1397
|
+
}
|
|
1398
|
+
});
|
|
1297
1399
|
}
|
|
1298
1400
|
if (isString(arg3)) {
|
|
1299
1401
|
options.locale = arg3;
|
|
@@ -1338,7 +1440,7 @@ function number(context, ...args) {
|
|
|
1338
1440
|
const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1339
1441
|
fallbackLocale, locale);
|
|
1340
1442
|
if (!isString(key) || key === '') {
|
|
1341
|
-
return new Intl.NumberFormat(locale).format(value);
|
|
1443
|
+
return new Intl.NumberFormat(locale, overrides).format(value);
|
|
1342
1444
|
}
|
|
1343
1445
|
// resolve format
|
|
1344
1446
|
let numberFormat = {};
|
|
@@ -1394,9 +1496,32 @@ function number(context, ...args) {
|
|
|
1394
1496
|
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1395
1497
|
}
|
|
1396
1498
|
/** @internal */
|
|
1499
|
+
const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
1500
|
+
'localeMatcher',
|
|
1501
|
+
'style',
|
|
1502
|
+
'currency',
|
|
1503
|
+
'currencyDisplay',
|
|
1504
|
+
'currencySign',
|
|
1505
|
+
'useGrouping',
|
|
1506
|
+
'minimumIntegerDigits',
|
|
1507
|
+
'minimumFractionDigits',
|
|
1508
|
+
'maximumFractionDigits',
|
|
1509
|
+
'minimumSignificantDigits',
|
|
1510
|
+
'maximumSignificantDigits',
|
|
1511
|
+
'compactDisplay',
|
|
1512
|
+
'notation',
|
|
1513
|
+
'signDisplay',
|
|
1514
|
+
'unit',
|
|
1515
|
+
'unitDisplay',
|
|
1516
|
+
'roundingMode',
|
|
1517
|
+
'roundingPriority',
|
|
1518
|
+
'roundingIncrement',
|
|
1519
|
+
'trailingZeroDisplay'
|
|
1520
|
+
];
|
|
1521
|
+
/** @internal */
|
|
1397
1522
|
function parseNumberArgs(...args) {
|
|
1398
1523
|
const [arg1, arg2, arg3, arg4] = args;
|
|
1399
|
-
|
|
1524
|
+
const options = {};
|
|
1400
1525
|
let overrides = {};
|
|
1401
1526
|
if (!isNumber(arg1)) {
|
|
1402
1527
|
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
@@ -1406,7 +1531,14 @@ function parseNumberArgs(...args) {
|
|
|
1406
1531
|
options.key = arg2;
|
|
1407
1532
|
}
|
|
1408
1533
|
else if (isPlainObject(arg2)) {
|
|
1409
|
-
|
|
1534
|
+
Object.keys(arg2).forEach(key => {
|
|
1535
|
+
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
1536
|
+
overrides[key] = arg2[key];
|
|
1537
|
+
}
|
|
1538
|
+
else {
|
|
1539
|
+
options[key] = arg2[key];
|
|
1540
|
+
}
|
|
1541
|
+
});
|
|
1410
1542
|
}
|
|
1411
1543
|
if (isString(arg3)) {
|
|
1412
1544
|
options.locale = arg3;
|
|
@@ -1431,10 +1563,11 @@ function clearNumberFormat(ctx, locale, format) {
|
|
|
1431
1563
|
}
|
|
1432
1564
|
}
|
|
1433
1565
|
|
|
1566
|
+
// TODO: we could not exports for Node native ES Moudles yet...
|
|
1434
1567
|
{
|
|
1435
1568
|
if (typeof __INTLIFY_PROD_DEVTOOLS__ !== 'boolean') {
|
|
1436
1569
|
getGlobalThis().__INTLIFY_PROD_DEVTOOLS__ = false;
|
|
1437
1570
|
}
|
|
1438
1571
|
}
|
|
1439
1572
|
|
|
1440
|
-
export { CoreErrorCodes, CoreWarnCodes, DEFAULT_LOCALE, DEFAULT_MESSAGE_DATA_TYPE, MISSING_RESOLVE_VALUE, NOT_REOSLVED, VERSION, clearCompileCache, clearDateTimeFormat, clearNumberFormat, compileToFunction, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple, getAdditionalMeta, getDevToolsHook, getWarnMessage, handleMissing, initI18nDevTools, isMessageFunction, isTranslateFallbackWarn, isTranslateMissingWarn, number, parse, parseDateTimeArgs, parseNumberArgs, parseTranslateArgs, registerLocaleFallbacker, registerMessageCompiler, registerMessageResolver, resolveValue, resolveWithKeyValue, setAdditionalMeta, setDevToolsHook, translate, translateDevTools, updateFallbackLocale };
|
|
1573
|
+
export { 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, compileToFunction, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple, getAdditionalMeta, getDevToolsHook, getFallbackContext, getWarnMessage, handleMissing, initI18nDevTools, isMessageFunction, isTranslateFallbackWarn, isTranslateMissingWarn, number, parse, parseDateTimeArgs, parseNumberArgs, parseTranslateArgs, registerLocaleFallbacker, registerMessageCompiler, registerMessageResolver, resolveValue, resolveWithKeyValue, setAdditionalMeta, setDevToolsHook, setFallbackContext, translate, translateDevTools, updateFallbackLocale };
|