@intlify/core-base 9.2.0-beta.9 → 9.2.2

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.
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * core-base v9.2.0-beta.9
3
- * (c) 2021 kazuya kawaguchi
2
+ * core-base v9.2.2
3
+ * (c) 2022 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
6
6
  'use strict';
@@ -328,14 +328,15 @@ function createMessageContext(options = {}) {
328
328
  shared.isFunction(options.pluralRules[locale])
329
329
  ? pluralDefault
330
330
  : undefined;
331
- const plural = (messages) => messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
331
+ const plural = (messages) => {
332
+ return messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
333
+ };
332
334
  const _list = options.list || [];
333
335
  const list = (index) => _list[index];
334
336
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
335
337
  const _named = options.named || {};
336
338
  shared.isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
337
339
  const named = (key) => _named[key];
338
- // TODO: need to design resolve message function?
339
340
  function message(key) {
340
341
  // prettier-ignore
341
342
  const msg = shared.isFunction(options.messages)
@@ -362,15 +363,39 @@ function createMessageContext(options = {}) {
362
363
  const type = shared.isPlainObject(options.processor) && shared.isString(options.processor.type)
363
364
  ? options.processor.type
364
365
  : DEFAULT_MESSAGE_DATA_TYPE;
366
+ const linked = (key, ...args) => {
367
+ const [arg1, arg2] = args;
368
+ let type = 'text';
369
+ let modifier = '';
370
+ if (args.length === 1) {
371
+ if (shared.isObject(arg1)) {
372
+ modifier = arg1.modifier || modifier;
373
+ type = arg1.type || type;
374
+ }
375
+ else if (shared.isString(arg1)) {
376
+ modifier = arg1 || modifier;
377
+ }
378
+ }
379
+ else if (args.length === 2) {
380
+ if (shared.isString(arg1)) {
381
+ modifier = arg1 || modifier;
382
+ }
383
+ if (shared.isString(arg2)) {
384
+ type = arg2 || type;
385
+ }
386
+ }
387
+ let msg = message(key)(ctx);
388
+ // The message in vnode resolved with linked are returned as an array by processor.nomalize
389
+ if (type === 'vnode' && shared.isArray(msg) && modifier) {
390
+ msg = msg[0];
391
+ }
392
+ return modifier ? _modifier(modifier)(msg, type) : msg;
393
+ };
365
394
  const ctx = {
366
395
  ["list" /* LIST */]: list,
367
396
  ["named" /* NAMED */]: named,
368
397
  ["plural" /* PLURAL */]: plural,
369
- ["linked" /* LINKED */]: (key, modifier) => {
370
- // TODO: should check `key`
371
- const msg = message(key)(ctx);
372
- return shared.isString(modifier) ? _modifier(modifier)(msg) : msg;
373
- },
398
+ ["linked" /* LINKED */]: linked,
374
399
  ["message" /* MESSAGE */]: message,
375
400
  ["type" /* TYPE */]: type,
376
401
  ["interpolate" /* INTERPOLATE */]: interpolate,
@@ -544,18 +569,37 @@ function appendItemToChain(chain, target, blocks) {
544
569
  * Intlify core-base version
545
570
  * @internal
546
571
  */
547
- const VERSION = '9.2.0-beta.9';
572
+ const VERSION = '9.2.2';
548
573
  const NOT_REOSLVED = -1;
549
574
  const DEFAULT_LOCALE = 'en-US';
550
575
  const MISSING_RESOLVE_VALUE = '';
576
+ const capitalize = (str) => `${str.charAt(0).toLocaleUpperCase()}${str.substr(1)}`;
551
577
  function getDefaultLinkedModifiers() {
552
578
  return {
553
- upper: (val) => (shared.isString(val) ? val.toUpperCase() : val),
554
- lower: (val) => (shared.isString(val) ? val.toLowerCase() : val),
555
- // prettier-ignore
556
- capitalize: (val) => (shared.isString(val)
557
- ? `${val.charAt(0).toLocaleUpperCase()}${val.substr(1)}`
558
- : val)
579
+ upper: (val, type) => {
580
+ // prettier-ignore
581
+ return type === 'text' && shared.isString(val)
582
+ ? val.toUpperCase()
583
+ : type === 'vnode' && shared.isObject(val) && '__v_isVNode' in val
584
+ ? val.children.toUpperCase()
585
+ : val;
586
+ },
587
+ lower: (val, type) => {
588
+ // prettier-ignore
589
+ return type === 'text' && shared.isString(val)
590
+ ? val.toLowerCase()
591
+ : type === 'vnode' && shared.isObject(val) && '__v_isVNode' in val
592
+ ? val.children.toLowerCase()
593
+ : val;
594
+ },
595
+ capitalize: (val, type) => {
596
+ // prettier-ignore
597
+ return (type === 'text' && shared.isString(val)
598
+ ? capitalize(val)
599
+ : type === 'vnode' && shared.isObject(val) && '__v_isVNode' in val
600
+ ? capitalize(val.children)
601
+ : val);
602
+ }
559
603
  };
560
604
  }
561
605
  let _compiler;
@@ -590,6 +634,11 @@ const setAdditionalMeta = (meta) => {
590
634
  _additionalMeta = meta;
591
635
  };
592
636
  const getAdditionalMeta = () => _additionalMeta;
637
+ let _fallbackContext = null;
638
+ const setFallbackContext = (context) => {
639
+ _fallbackContext = context;
640
+ };
641
+ const getFallbackContext = () => _fallbackContext;
593
642
  // ID for CoreContext
594
643
  let _cid = 0;
595
644
  function createCoreContext(options = {}) {
@@ -641,6 +690,9 @@ function createCoreContext(options = {}) {
641
690
  const localeFallbacker = shared.isFunction(options.localeFallbacker)
642
691
  ? options.localeFallbacker
643
692
  : _fallbacker || fallbackWithSimple;
693
+ const fallbackContext = shared.isObject(options.fallbackContext)
694
+ ? options.fallbackContext
695
+ : undefined;
644
696
  const onWarn = shared.isFunction(options.onWarn) ? options.onWarn : shared.warn;
645
697
  // setup internal options
646
698
  const internalOptions = options;
@@ -674,6 +726,7 @@ function createCoreContext(options = {}) {
674
726
  messageCompiler,
675
727
  messageResolver,
676
728
  localeFallbacker,
729
+ fallbackContext,
677
730
  onWarn,
678
731
  __meta
679
732
  };
@@ -781,7 +834,7 @@ function compileToFunction(source, options = {}) {
781
834
  }
782
835
 
783
836
  let code = messageCompiler.CompileErrorCodes.__EXTEND_POINT__;
784
- const inc = () => code++;
837
+ const inc = () => ++code;
785
838
  const CoreErrorCodes = {
786
839
  INVALID_ARGUMENT: code,
787
840
  INVALID_DATE_ARGUMENT: inc(),
@@ -803,7 +856,7 @@ const NOOP_MESSAGE_FUNCTION = () => '';
803
856
  const isMessageFunction = (val) => shared.isFunction(val);
804
857
  // implementation of `translate` function
805
858
  function translate(context, ...args) {
806
- const { fallbackFormat, postTranslation, unresolving, fallbackLocale, messages } = context;
859
+ const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
807
860
  const [key, options] = parseTranslateArgs(...args);
808
861
  const missingWarn = shared.isBoolean(options.missingWarn)
809
862
  ? options.missingWarn
@@ -819,9 +872,9 @@ function translate(context, ...args) {
819
872
  const defaultMsgOrKey = shared.isString(options.default) || shared.isBoolean(options.default) // default by function option
820
873
  ? !shared.isBoolean(options.default)
821
874
  ? options.default
822
- : key
875
+ : (!messageCompiler ? () => key : key)
823
876
  : fallbackFormat // default by `fallbackFormat` option
824
- ? key
877
+ ? (!messageCompiler ? () => key : key)
825
878
  : '';
826
879
  const enableDefaultMsg = fallbackFormat || defaultMsgOrKey !== '';
827
880
  const locale = shared.isString(options.locale) ? options.locale : context.locale;
@@ -829,13 +882,19 @@ function translate(context, ...args) {
829
882
  escapeParameter && escapeParams(options);
830
883
  // resolve message format
831
884
  // eslint-disable-next-line prefer-const
832
- let [format, targetLocale, message] = !resolvedMessage
885
+ let [formatScope, targetLocale, message] = !resolvedMessage
833
886
  ? resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn)
834
887
  : [
835
888
  key,
836
889
  locale,
837
890
  messages[locale] || {}
838
891
  ];
892
+ // NOTE:
893
+ // Fix to work around `ssrTransfrom` bug in Vite.
894
+ // https://github.com/vitejs/vite/issues/4306
895
+ // To get around this, use temporary variables.
896
+ // https://github.com/nuxt/framework/issues/1461#issuecomment-954606243
897
+ let format = formatScope;
839
898
  // if you use default message, set it as message format!
840
899
  let cacheBaseKey = key;
841
900
  if (!resolvedMessage &&
@@ -876,7 +935,9 @@ function translate(context, ...args) {
876
935
  const msgContext = createMessageContext(ctxOptions);
877
936
  const messaged = evaluateMessage(context, msg, msgContext);
878
937
  // if use post translation option, proceed it with handler
879
- const ret = postTranslation ? postTranslation(messaged) : messaged;
938
+ const ret = postTranslation
939
+ ? postTranslation(messaged, key)
940
+ : messaged;
880
941
  // NOTE: experimental !!
881
942
  {
882
943
  // prettier-ignore
@@ -998,6 +1059,12 @@ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
998
1059
  msg.key = msg.key || key;
999
1060
  return msg;
1000
1061
  }
1062
+ if (messageCompiler == null) {
1063
+ const msg = (() => format);
1064
+ msg.locale = targetLocale;
1065
+ msg.key = key;
1066
+ return msg;
1067
+ }
1001
1068
  // for vue-devtools timeline event
1002
1069
  let start = null;
1003
1070
  let startTag;
@@ -1124,9 +1191,14 @@ function getCompileOptions(context, locale, key, source, warnHtmlMessage, errorD
1124
1191
  };
1125
1192
  }
1126
1193
  function getMessageContextOptions(context, locale, message, options) {
1127
- const { modifiers, pluralRules, messageResolver: resolveValue } = context;
1194
+ const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
1128
1195
  const resolveMessage = (key) => {
1129
- const val = resolveValue(message, key);
1196
+ let val = resolveValue(message, key);
1197
+ // fallback to root context
1198
+ if (val == null && fallbackContext) {
1199
+ const [, , message] = resolveMessageFormat(fallbackContext, key, locale, fallbackLocale, fallbackWarn, missingWarn);
1200
+ val = resolveValue(message, key);
1201
+ }
1130
1202
  if (shared.isString(val)) {
1131
1203
  let occurred = false;
1132
1204
  const errorDetector = () => {
@@ -1192,7 +1264,7 @@ function datetime(context, ...args) {
1192
1264
  const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
1193
1265
  fallbackLocale, locale);
1194
1266
  if (!shared.isString(key) || key === '') {
1195
- return new Intl.DateTimeFormat(locale).format(value);
1267
+ return new Intl.DateTimeFormat(locale, overrides).format(value);
1196
1268
  }
1197
1269
  // resolve format
1198
1270
  let datetimeFormat = {};
@@ -1247,9 +1319,32 @@ function datetime(context, ...args) {
1247
1319
  return !part ? formatter.format(value) : formatter.formatToParts(value);
1248
1320
  }
1249
1321
  /** @internal */
1322
+ const DATETIME_FORMAT_OPTIONS_KEYS = [
1323
+ 'localeMatcher',
1324
+ 'weekday',
1325
+ 'era',
1326
+ 'year',
1327
+ 'month',
1328
+ 'day',
1329
+ 'hour',
1330
+ 'minute',
1331
+ 'second',
1332
+ 'timeZoneName',
1333
+ 'formatMatcher',
1334
+ 'hour12',
1335
+ 'timeZone',
1336
+ 'dateStyle',
1337
+ 'timeStyle',
1338
+ 'calendar',
1339
+ 'dayPeriod',
1340
+ 'numberingSystem',
1341
+ 'hourCycle',
1342
+ 'fractionalSecondDigits'
1343
+ ];
1344
+ /** @internal */
1250
1345
  function parseDateTimeArgs(...args) {
1251
1346
  const [arg1, arg2, arg3, arg4] = args;
1252
- let options = {};
1347
+ const options = {};
1253
1348
  let overrides = {};
1254
1349
  let value;
1255
1350
  if (shared.isString(arg1)) {
@@ -1291,7 +1386,14 @@ function parseDateTimeArgs(...args) {
1291
1386
  options.key = arg2;
1292
1387
  }
1293
1388
  else if (shared.isPlainObject(arg2)) {
1294
- options = arg2;
1389
+ Object.keys(arg2).forEach(key => {
1390
+ if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
1391
+ overrides[key] = arg2[key];
1392
+ }
1393
+ else {
1394
+ options[key] = arg2[key];
1395
+ }
1396
+ });
1295
1397
  }
1296
1398
  if (shared.isString(arg3)) {
1297
1399
  options.locale = arg3;
@@ -1336,7 +1438,7 @@ function number(context, ...args) {
1336
1438
  const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
1337
1439
  fallbackLocale, locale);
1338
1440
  if (!shared.isString(key) || key === '') {
1339
- return new Intl.NumberFormat(locale).format(value);
1441
+ return new Intl.NumberFormat(locale, overrides).format(value);
1340
1442
  }
1341
1443
  // resolve format
1342
1444
  let numberFormat = {};
@@ -1391,9 +1493,32 @@ function number(context, ...args) {
1391
1493
  return !part ? formatter.format(value) : formatter.formatToParts(value);
1392
1494
  }
1393
1495
  /** @internal */
1496
+ const NUMBER_FORMAT_OPTIONS_KEYS = [
1497
+ 'localeMatcher',
1498
+ 'style',
1499
+ 'currency',
1500
+ 'currencyDisplay',
1501
+ 'currencySign',
1502
+ 'useGrouping',
1503
+ 'minimumIntegerDigits',
1504
+ 'minimumFractionDigits',
1505
+ 'maximumFractionDigits',
1506
+ 'minimumSignificantDigits',
1507
+ 'maximumSignificantDigits',
1508
+ 'compactDisplay',
1509
+ 'notation',
1510
+ 'signDisplay',
1511
+ 'unit',
1512
+ 'unitDisplay',
1513
+ 'roundingMode',
1514
+ 'roundingPriority',
1515
+ 'roundingIncrement',
1516
+ 'trailingZeroDisplay'
1517
+ ];
1518
+ /** @internal */
1394
1519
  function parseNumberArgs(...args) {
1395
1520
  const [arg1, arg2, arg3, arg4] = args;
1396
- let options = {};
1521
+ const options = {};
1397
1522
  let overrides = {};
1398
1523
  if (!shared.isNumber(arg1)) {
1399
1524
  throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
@@ -1403,7 +1528,14 @@ function parseNumberArgs(...args) {
1403
1528
  options.key = arg2;
1404
1529
  }
1405
1530
  else if (shared.isPlainObject(arg2)) {
1406
- options = arg2;
1531
+ Object.keys(arg2).forEach(key => {
1532
+ if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
1533
+ overrides[key] = arg2[key];
1534
+ }
1535
+ else {
1536
+ options[key] = arg2[key];
1537
+ }
1538
+ });
1407
1539
  }
1408
1540
  if (shared.isString(arg3)) {
1409
1541
  options.locale = arg3;
@@ -1432,10 +1564,12 @@ exports.CompileErrorCodes = messageCompiler.CompileErrorCodes;
1432
1564
  exports.createCompileError = messageCompiler.createCompileError;
1433
1565
  exports.CoreErrorCodes = CoreErrorCodes;
1434
1566
  exports.CoreWarnCodes = CoreWarnCodes;
1567
+ exports.DATETIME_FORMAT_OPTIONS_KEYS = DATETIME_FORMAT_OPTIONS_KEYS;
1435
1568
  exports.DEFAULT_LOCALE = DEFAULT_LOCALE;
1436
1569
  exports.DEFAULT_MESSAGE_DATA_TYPE = DEFAULT_MESSAGE_DATA_TYPE;
1437
1570
  exports.MISSING_RESOLVE_VALUE = MISSING_RESOLVE_VALUE;
1438
1571
  exports.NOT_REOSLVED = NOT_REOSLVED;
1572
+ exports.NUMBER_FORMAT_OPTIONS_KEYS = NUMBER_FORMAT_OPTIONS_KEYS;
1439
1573
  exports.VERSION = VERSION;
1440
1574
  exports.clearCompileCache = clearCompileCache;
1441
1575
  exports.clearDateTimeFormat = clearDateTimeFormat;
@@ -1449,6 +1583,7 @@ exports.fallbackWithLocaleChain = fallbackWithLocaleChain;
1449
1583
  exports.fallbackWithSimple = fallbackWithSimple;
1450
1584
  exports.getAdditionalMeta = getAdditionalMeta;
1451
1585
  exports.getDevToolsHook = getDevToolsHook;
1586
+ exports.getFallbackContext = getFallbackContext;
1452
1587
  exports.getWarnMessage = getWarnMessage;
1453
1588
  exports.handleMissing = handleMissing;
1454
1589
  exports.initI18nDevTools = initI18nDevTools;
@@ -1467,6 +1602,7 @@ exports.resolveValue = resolveValue;
1467
1602
  exports.resolveWithKeyValue = resolveWithKeyValue;
1468
1603
  exports.setAdditionalMeta = setAdditionalMeta;
1469
1604
  exports.setDevToolsHook = setDevToolsHook;
1605
+ exports.setFallbackContext = setFallbackContext;
1470
1606
  exports.translate = translate;
1471
1607
  exports.translateDevTools = translateDevTools;
1472
1608
  exports.updateFallbackLocale = updateFallbackLocale;