@mmstack/translate 21.1.12 → 21.1.14
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/README.md +66 -16
- package/fesm2022/mmstack-translate.mjs +404 -79
- package/fesm2022/mmstack-translate.mjs.map +1 -1
- package/package.json +1 -1
- package/types/mmstack-translate.d.ts +300 -70
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { computed, inject, InjectionToken, LOCALE_ID, signal, isDevMode, effect, resource,
|
|
2
|
+
import { computed, inject, InjectionToken, LOCALE_ID, signal, untracked, isDevMode, effect, resource, Injectable, isSignal, input, Renderer2, ElementRef, afterRenderEffect, Directive, ChangeDetectorRef } from '@angular/core';
|
|
3
3
|
import { createIntlCache, createIntl } from '@formatjs/intl';
|
|
4
4
|
import { toSignal } from '@angular/core/rxjs-interop';
|
|
5
5
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
@@ -132,9 +132,27 @@ function injectSupportedLocales() {
|
|
|
132
132
|
* the actual locale signal used to store the current locale string
|
|
133
133
|
*/
|
|
134
134
|
const STORE_LOCALE = signal('en-US', ...(ngDevMode ? [{ debugName: "STORE_LOCALE" }] : /* istanbul ignore next */ []));
|
|
135
|
+
/**
|
|
136
|
+
* @internal
|
|
137
|
+
* @deprecated will be removed when ng23 drops
|
|
138
|
+
*/
|
|
139
|
+
function readLocaleUnsafe() {
|
|
140
|
+
return STORE_LOCALE();
|
|
141
|
+
}
|
|
135
142
|
function injectLocaleInternal() {
|
|
136
143
|
return STORE_LOCALE;
|
|
137
144
|
}
|
|
145
|
+
function proxyToGlobalSignleton(src) {
|
|
146
|
+
const originalSet = src.set;
|
|
147
|
+
src.set = (next) => {
|
|
148
|
+
originalSet(next);
|
|
149
|
+
STORE_LOCALE.set(next);
|
|
150
|
+
};
|
|
151
|
+
src.update = (updater) => {
|
|
152
|
+
src.set(updater(untracked(src)));
|
|
153
|
+
};
|
|
154
|
+
return src;
|
|
155
|
+
}
|
|
138
156
|
function isDynamicConfig(cfg) {
|
|
139
157
|
return !!cfg && 'localeStorage' in cfg && !!cfg.localeStorage;
|
|
140
158
|
}
|
|
@@ -230,7 +248,7 @@ class TranslationStore {
|
|
|
230
248
|
messages: this.messages(),
|
|
231
249
|
}, this.cache), ...(ngDevMode ? [{ debugName: "intl" }] : /* istanbul ignore next */ []));
|
|
232
250
|
constructor() {
|
|
233
|
-
this.locale = initLocale(
|
|
251
|
+
this.locale = proxyToGlobalSignleton(initLocale(signal('en-US')));
|
|
234
252
|
const paramName = this.config?.localeParamName;
|
|
235
253
|
if (paramName) {
|
|
236
254
|
const param = pathParam(paramName);
|
|
@@ -424,6 +442,53 @@ function injectAddTranslations() {
|
|
|
424
442
|
};
|
|
425
443
|
}
|
|
426
444
|
|
|
445
|
+
function equalLocale(a, b) {
|
|
446
|
+
return a.locale === b.locale;
|
|
447
|
+
}
|
|
448
|
+
function createFormatterProvider(formatterName, libraryDefaults, nonLocaleEqual) {
|
|
449
|
+
const token = new InjectionToken(`@mmstack/translate:format-${formatterName}-config`, {
|
|
450
|
+
factory: () => {
|
|
451
|
+
const loc = injectDynamicLocale();
|
|
452
|
+
return computed(() => ({
|
|
453
|
+
...libraryDefaults,
|
|
454
|
+
locale: loc(),
|
|
455
|
+
}), { equal: equalLocale });
|
|
456
|
+
},
|
|
457
|
+
});
|
|
458
|
+
const provider = (valueOrFn) => {
|
|
459
|
+
const fnProvider = typeof valueOrFn === 'function'
|
|
460
|
+
? valueOrFn
|
|
461
|
+
: () => valueOrFn;
|
|
462
|
+
return {
|
|
463
|
+
provide: token,
|
|
464
|
+
useFactory: () => {
|
|
465
|
+
const loc = injectDynamicLocale();
|
|
466
|
+
const providedDefaultsOrSignal = fnProvider();
|
|
467
|
+
if (isSignal(providedDefaultsOrSignal))
|
|
468
|
+
return computed(() => ({
|
|
469
|
+
...libraryDefaults,
|
|
470
|
+
...providedDefaultsOrSignal(),
|
|
471
|
+
locale: loc(),
|
|
472
|
+
}), {
|
|
473
|
+
equal: (a, b) => equalLocale(a, b) && nonLocaleEqual(a, b),
|
|
474
|
+
});
|
|
475
|
+
const defaults = {
|
|
476
|
+
...libraryDefaults,
|
|
477
|
+
...providedDefaultsOrSignal,
|
|
478
|
+
};
|
|
479
|
+
return computed(() => ({
|
|
480
|
+
...defaults,
|
|
481
|
+
locale: loc(),
|
|
482
|
+
}), {
|
|
483
|
+
equal: equalLocale,
|
|
484
|
+
});
|
|
485
|
+
},
|
|
486
|
+
};
|
|
487
|
+
};
|
|
488
|
+
const injectFn = () => inject(token);
|
|
489
|
+
return [provider, injectFn];
|
|
490
|
+
}
|
|
491
|
+
|
|
427
492
|
function unwrap(value) {
|
|
428
493
|
return isSignal(value) ? value() : value;
|
|
429
494
|
}
|
|
@@ -450,32 +515,68 @@ function validDateOrNull(date) {
|
|
|
450
515
|
}
|
|
451
516
|
const cache$4 = new Map();
|
|
452
517
|
function getFormatter$4(locale, format, timeZone) {
|
|
453
|
-
const cacheKey = `${locale}|${format}|${timeZone ?? ''}`;
|
|
518
|
+
const cacheKey = `${locale}|${typeof format === 'string' ? format : JSON.stringify(format)}|${timeZone ?? ''}`;
|
|
454
519
|
let formatter = cache$4.get(cacheKey);
|
|
455
520
|
if (!formatter) {
|
|
456
521
|
formatter = new Intl.DateTimeFormat(locale, {
|
|
457
|
-
...FORMAT_PRESETS[format],
|
|
522
|
+
...(typeof format === 'string' ? FORMAT_PRESETS[format] : format),
|
|
458
523
|
timeZone,
|
|
459
524
|
});
|
|
460
525
|
cache$4.set(cacheKey, formatter);
|
|
461
526
|
}
|
|
462
527
|
return formatter;
|
|
463
528
|
}
|
|
464
|
-
|
|
465
|
-
* Format a date using the current or provided locale & timezone
|
|
466
|
-
* By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
|
|
467
|
-
*
|
|
468
|
-
* @param date - Date to format
|
|
469
|
-
* @param opt - Options for formatting
|
|
470
|
-
* @returns Formatted date string
|
|
471
|
-
*/
|
|
472
|
-
function formatDate(date, opt) {
|
|
529
|
+
function formatDate(date, optOrLocale) {
|
|
473
530
|
const validDate = validDateOrNull(unwrap(date));
|
|
474
531
|
if (validDate === null)
|
|
475
532
|
return '';
|
|
476
|
-
const
|
|
477
|
-
|
|
478
|
-
|
|
533
|
+
const unwrappedArgs = unwrap(optOrLocale);
|
|
534
|
+
let locale;
|
|
535
|
+
let format = 'medium';
|
|
536
|
+
let tz;
|
|
537
|
+
if (typeof unwrappedArgs === 'string') {
|
|
538
|
+
locale = unwrappedArgs;
|
|
539
|
+
}
|
|
540
|
+
else if (unwrappedArgs && typeof unwrappedArgs === 'object') {
|
|
541
|
+
locale = unwrappedArgs.locale ?? readLocaleUnsafe();
|
|
542
|
+
format = unwrappedArgs.format ?? 'medium';
|
|
543
|
+
tz = unwrappedArgs.tz;
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
locale = readLocaleUnsafe();
|
|
547
|
+
}
|
|
548
|
+
return getFormatter$4(locale, format, tz).format(validDate);
|
|
549
|
+
}
|
|
550
|
+
const [provideFormatDateDefaults, injectFormatDateOptions] = createFormatterProvider('date', {
|
|
551
|
+
format: 'medium',
|
|
552
|
+
}, (a, b) => {
|
|
553
|
+
if (a.tz !== b.tz)
|
|
554
|
+
return false;
|
|
555
|
+
if (a.format === b.format)
|
|
556
|
+
return true;
|
|
557
|
+
return JSON.stringify(a.format) === JSON.stringify(b.format);
|
|
558
|
+
});
|
|
559
|
+
/**
|
|
560
|
+
* Inject a context-safe date formatting function tied to the current injector.
|
|
561
|
+
* Uses the libraries locale signal & provided default configuration to react to locale/config changes
|
|
562
|
+
* @example
|
|
563
|
+
* const formatDate = injectFormatDate();
|
|
564
|
+
* readonly displayDate = computed(() => formatDate(this.date()));
|
|
565
|
+
*/
|
|
566
|
+
function injectFormatDate() {
|
|
567
|
+
const defaults = injectFormatDateOptions();
|
|
568
|
+
return (date, optOrLocale) => {
|
|
569
|
+
if (!optOrLocale)
|
|
570
|
+
return formatDate(date, defaults());
|
|
571
|
+
const unwrapped = unwrap(optOrLocale);
|
|
572
|
+
const opt = typeof unwrapped === 'object'
|
|
573
|
+
? { ...defaults(), ...unwrapped }
|
|
574
|
+
: {
|
|
575
|
+
...defaults(),
|
|
576
|
+
locale: unwrapped,
|
|
577
|
+
};
|
|
578
|
+
return formatDate(date, opt);
|
|
579
|
+
};
|
|
479
580
|
}
|
|
480
581
|
|
|
481
582
|
const cache$3 = new Map();
|
|
@@ -493,21 +594,48 @@ function getFormatter$3(locale, type, style) {
|
|
|
493
594
|
}
|
|
494
595
|
/**
|
|
495
596
|
* Format a display name using the current or provided locale
|
|
496
|
-
* By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
|
|
497
597
|
*
|
|
498
598
|
* @param value - The code to format
|
|
499
599
|
* @param type - The type of display name to format
|
|
500
600
|
* @param opt - Options for formatting
|
|
501
601
|
* @returns Formatted display name string
|
|
502
602
|
*/
|
|
503
|
-
function formatDisplayName(value, type,
|
|
504
|
-
const
|
|
505
|
-
if (!
|
|
603
|
+
function formatDisplayName(value, type, localeOrOpt) {
|
|
604
|
+
const unwrappedValue = unwrap(value);
|
|
605
|
+
if (!unwrappedValue?.trim())
|
|
506
606
|
return '';
|
|
507
607
|
const unwrappedType = unwrap(type);
|
|
508
|
-
const
|
|
509
|
-
const locale =
|
|
510
|
-
|
|
608
|
+
const unwrapped = unwrap(localeOrOpt);
|
|
609
|
+
const locale = typeof unwrapped === 'string'
|
|
610
|
+
? unwrapped
|
|
611
|
+
: (unwrapped?.locale ?? readLocaleUnsafe());
|
|
612
|
+
const opt = typeof unwrapped === 'object' ? unwrapped : undefined;
|
|
613
|
+
return (getFormatter$3(locale, unwrappedType, opt?.style ?? 'long').of(unwrappedValue) ?? '');
|
|
614
|
+
}
|
|
615
|
+
const [provideFormatDisplayNameDefaults, injectFormatDisplayNameDefaults] = createFormatterProvider('displayName', {
|
|
616
|
+
style: 'long',
|
|
617
|
+
}, (a, b) => a.style === b.style);
|
|
618
|
+
/**
|
|
619
|
+
* Inject a context-safe date formatting function tied to the current injector.
|
|
620
|
+
* Uses the libraries locale signal & provided default configuration to react to locale/config changes
|
|
621
|
+
* @example
|
|
622
|
+
* const formatDisplayName = injectFormatDisplayName();
|
|
623
|
+
* readonly region = computed(() => formatDisplayName('US', 'region'));
|
|
624
|
+
*/
|
|
625
|
+
function injectFormatDisplayName() {
|
|
626
|
+
const defaults = injectFormatDisplayNameDefaults();
|
|
627
|
+
return (value, type, localeOrOpt) => {
|
|
628
|
+
if (!localeOrOpt)
|
|
629
|
+
return formatDisplayName(value, type, defaults());
|
|
630
|
+
const unwrapped = unwrap(localeOrOpt);
|
|
631
|
+
const opt = typeof unwrapped === 'object'
|
|
632
|
+
? { ...defaults, ...unwrapped }
|
|
633
|
+
: {
|
|
634
|
+
...defaults,
|
|
635
|
+
locale: unwrapped,
|
|
636
|
+
};
|
|
637
|
+
return formatDisplayName(value, type, opt);
|
|
638
|
+
};
|
|
511
639
|
}
|
|
512
640
|
|
|
513
641
|
const cache$2 = new Map();
|
|
@@ -525,21 +653,52 @@ function getFormatter$2(locale, type, style) {
|
|
|
525
653
|
}
|
|
526
654
|
return formatter;
|
|
527
655
|
}
|
|
656
|
+
function formatList(value, optOrLocale) {
|
|
657
|
+
const unwrappedValue = unwrapList(value);
|
|
658
|
+
if (unwrappedValue.length === 0)
|
|
659
|
+
return '';
|
|
660
|
+
const unwrappedArgs = unwrap(optOrLocale);
|
|
661
|
+
let locale;
|
|
662
|
+
let type = 'conjunction';
|
|
663
|
+
let style = 'long';
|
|
664
|
+
if (typeof unwrappedArgs === 'string') {
|
|
665
|
+
locale = unwrappedArgs;
|
|
666
|
+
}
|
|
667
|
+
else if (unwrappedArgs && typeof unwrappedArgs === 'object') {
|
|
668
|
+
locale = unwrappedArgs.locale ?? readLocaleUnsafe();
|
|
669
|
+
type = unwrappedArgs.type ?? 'conjunction';
|
|
670
|
+
style = unwrappedArgs.style ?? 'long';
|
|
671
|
+
}
|
|
672
|
+
else {
|
|
673
|
+
locale = readLocaleUnsafe();
|
|
674
|
+
}
|
|
675
|
+
return getFormatter$2(locale, type, style).format(unwrappedValue);
|
|
676
|
+
}
|
|
677
|
+
const [provideFormatListDefaults, injectFormatListOptions] = createFormatterProvider('list', {
|
|
678
|
+
type: 'conjunction',
|
|
679
|
+
style: 'long',
|
|
680
|
+
}, (a, b) => a.type === b.type && a.style === b.style);
|
|
528
681
|
/**
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
*
|
|
534
|
-
* @returns Formatted list string
|
|
682
|
+
* Inject a context-safe list formatting function tied to the current injector.
|
|
683
|
+
* Uses the libraries locale signal & provided default configuration to react to locale/config changes
|
|
684
|
+
* @example
|
|
685
|
+
* const formatList = injectFormatList();
|
|
686
|
+
* readonly displayList = computed(() => formatList(this.items()));
|
|
535
687
|
*/
|
|
536
|
-
function
|
|
537
|
-
const
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
688
|
+
function injectFormatList() {
|
|
689
|
+
const defaults = injectFormatListOptions();
|
|
690
|
+
return (value, optOrLocale) => {
|
|
691
|
+
if (!optOrLocale)
|
|
692
|
+
return formatList(value, defaults());
|
|
693
|
+
const unwrapped = unwrap(optOrLocale);
|
|
694
|
+
const opt = typeof unwrapped === 'object'
|
|
695
|
+
? { ...defaults(), ...unwrapped }
|
|
696
|
+
: {
|
|
697
|
+
...defaults(),
|
|
698
|
+
locale: unwrapped,
|
|
699
|
+
};
|
|
700
|
+
return formatList(value, opt);
|
|
701
|
+
};
|
|
543
702
|
}
|
|
544
703
|
|
|
545
704
|
const cache$1 = new Map();
|
|
@@ -566,45 +725,151 @@ function getFormatter$1(locale, minFractionDigits, maxFractionDigits, useGroupin
|
|
|
566
725
|
}
|
|
567
726
|
return formatter;
|
|
568
727
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
* @param opt - Options for formatting
|
|
575
|
-
* @returns Formatted number string
|
|
576
|
-
*/
|
|
577
|
-
function formatNumber(value, opt) {
|
|
578
|
-
const unwrappedOpt = unwrap(opt);
|
|
579
|
-
const unwrappedNumber = unwrapValue(value, unwrappedOpt?.fallbackToZero);
|
|
728
|
+
function formatNumber(value, optOrLocale) {
|
|
729
|
+
const unwrappedArgs = unwrap(optOrLocale);
|
|
730
|
+
const isOpt = unwrappedArgs != null && typeof unwrappedArgs === 'object';
|
|
731
|
+
const fallbackToZero = isOpt ? unwrappedArgs.fallbackToZero : undefined;
|
|
732
|
+
const unwrappedNumber = unwrapValue(value, fallbackToZero);
|
|
580
733
|
if (unwrappedNumber === null)
|
|
581
734
|
return '';
|
|
582
|
-
|
|
583
|
-
|
|
735
|
+
let locale;
|
|
736
|
+
let notation;
|
|
737
|
+
let minFractionDigits;
|
|
738
|
+
let maxFractionDigits;
|
|
739
|
+
let useGrouping = true;
|
|
740
|
+
if (typeof unwrappedArgs === 'string') {
|
|
741
|
+
locale = unwrappedArgs;
|
|
742
|
+
}
|
|
743
|
+
else if (isOpt) {
|
|
744
|
+
locale = unwrappedArgs.locale ?? readLocaleUnsafe();
|
|
745
|
+
notation = unwrappedArgs.notation ?? 'standard';
|
|
746
|
+
minFractionDigits = unwrappedArgs.minFractionDigits;
|
|
747
|
+
maxFractionDigits = unwrappedArgs.maxFractionDigits;
|
|
748
|
+
useGrouping = unwrappedArgs.useGrouping ?? true;
|
|
749
|
+
}
|
|
750
|
+
else {
|
|
751
|
+
locale = readLocaleUnsafe();
|
|
752
|
+
notation = 'standard';
|
|
753
|
+
}
|
|
754
|
+
return getFormatter$1(locale, minFractionDigits, maxFractionDigits, useGrouping, notation).format(unwrappedNumber);
|
|
584
755
|
}
|
|
756
|
+
const [provideFormatNumberDefaults, injectFormatNumberOptions] = createFormatterProvider('number', {
|
|
757
|
+
notation: 'standard',
|
|
758
|
+
useGrouping: true,
|
|
759
|
+
}, (a, b) => a.notation === b.notation &&
|
|
760
|
+
a.minFractionDigits === b.minFractionDigits &&
|
|
761
|
+
a.maxFractionDigits === b.maxFractionDigits &&
|
|
762
|
+
a.useGrouping === b.useGrouping &&
|
|
763
|
+
a.fallbackToZero === b.fallbackToZero);
|
|
585
764
|
/**
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*
|
|
591
|
-
* @returns Formatted percentage string
|
|
765
|
+
* Inject a context-safe number formatting function tied to the current injector.
|
|
766
|
+
* Uses the libraries locale signal & provided default configuration to react to locale/config changes
|
|
767
|
+
* @example
|
|
768
|
+
* const formatNumber = injectFormatNumber();
|
|
769
|
+
* readonly display = computed(() => formatNumber(this.value()));
|
|
592
770
|
*/
|
|
593
|
-
function
|
|
594
|
-
const
|
|
595
|
-
|
|
771
|
+
function injectFormatNumber() {
|
|
772
|
+
const defaults = injectFormatNumberOptions();
|
|
773
|
+
return (value, optOrLocale) => {
|
|
774
|
+
if (!optOrLocale)
|
|
775
|
+
return formatNumber(value, defaults());
|
|
776
|
+
const unwrapped = unwrap(optOrLocale);
|
|
777
|
+
const opt = typeof unwrapped === 'object'
|
|
778
|
+
? { ...defaults(), ...unwrapped }
|
|
779
|
+
: {
|
|
780
|
+
...defaults(),
|
|
781
|
+
locale: unwrapped,
|
|
782
|
+
};
|
|
783
|
+
return formatNumber(value, opt);
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
function formatPercent(value, optOrLocale) {
|
|
787
|
+
const unwrappedArgs = unwrap(optOrLocale);
|
|
788
|
+
const isOpt = unwrappedArgs != null && typeof unwrappedArgs === 'object';
|
|
789
|
+
const fallbackToZero = isOpt ? unwrappedArgs.fallbackToZero : undefined;
|
|
790
|
+
const unwrappedNumber = unwrapValue(value, fallbackToZero);
|
|
596
791
|
if (unwrappedNumber === null)
|
|
597
792
|
return '';
|
|
598
|
-
|
|
599
|
-
|
|
793
|
+
let locale;
|
|
794
|
+
let minFractionDigits;
|
|
795
|
+
let maxFractionDigits;
|
|
796
|
+
if (typeof unwrappedArgs === 'string') {
|
|
797
|
+
locale = unwrappedArgs;
|
|
798
|
+
}
|
|
799
|
+
else if (isOpt) {
|
|
800
|
+
locale = unwrappedArgs.locale ?? readLocaleUnsafe();
|
|
801
|
+
minFractionDigits = unwrappedArgs.minFractionDigits;
|
|
802
|
+
maxFractionDigits = unwrappedArgs.maxFractionDigits;
|
|
803
|
+
}
|
|
804
|
+
else {
|
|
805
|
+
locale = readLocaleUnsafe();
|
|
806
|
+
}
|
|
807
|
+
return getFormatter$1(locale, minFractionDigits, maxFractionDigits, undefined, undefined, undefined, undefined, 'percent').format(unwrappedNumber);
|
|
808
|
+
}
|
|
809
|
+
const [provideFormatPercentDefaults, injectFormatPercentOptions] = createFormatterProvider('percent', {}, (a, b) => a.minFractionDigits === b.minFractionDigits &&
|
|
810
|
+
a.maxFractionDigits === b.maxFractionDigits &&
|
|
811
|
+
a.fallbackToZero === b.fallbackToZero);
|
|
812
|
+
/**
|
|
813
|
+
* Inject a context-safe percent formatting function tied to the current injector.
|
|
814
|
+
* Uses the libraries locale signal & provided default configuration to react to locale/config changes
|
|
815
|
+
*/
|
|
816
|
+
function injectFormatPercent() {
|
|
817
|
+
const defaults = injectFormatPercentOptions();
|
|
818
|
+
return (value, optOrLocale) => {
|
|
819
|
+
if (!optOrLocale)
|
|
820
|
+
return formatPercent(value, defaults());
|
|
821
|
+
const unwrapped = unwrap(optOrLocale);
|
|
822
|
+
const opt = typeof unwrapped === 'object'
|
|
823
|
+
? { ...defaults(), ...unwrapped }
|
|
824
|
+
: {
|
|
825
|
+
...defaults(),
|
|
826
|
+
locale: unwrapped,
|
|
827
|
+
};
|
|
828
|
+
return formatPercent(value, opt);
|
|
829
|
+
};
|
|
600
830
|
}
|
|
601
|
-
function formatCurrency(value, currency,
|
|
602
|
-
const
|
|
603
|
-
const
|
|
831
|
+
function formatCurrency(value, currency, optOrLocale) {
|
|
832
|
+
const unwrappedArgs = unwrap(optOrLocale);
|
|
833
|
+
const isOpt = unwrappedArgs != null && typeof unwrappedArgs === 'object';
|
|
834
|
+
const fallbackToZero = isOpt ? unwrappedArgs.fallbackToZero : undefined;
|
|
835
|
+
const unwrappedValue = unwrapValue(value, fallbackToZero);
|
|
604
836
|
if (unwrappedValue === null)
|
|
605
837
|
return '';
|
|
606
|
-
|
|
607
|
-
|
|
838
|
+
let locale;
|
|
839
|
+
let display = 'symbol';
|
|
840
|
+
if (typeof unwrappedArgs === 'string') {
|
|
841
|
+
locale = unwrappedArgs;
|
|
842
|
+
}
|
|
843
|
+
else if (isOpt) {
|
|
844
|
+
locale = unwrappedArgs.locale ?? readLocaleUnsafe();
|
|
845
|
+
display = unwrappedArgs.display ?? 'symbol';
|
|
846
|
+
}
|
|
847
|
+
else {
|
|
848
|
+
locale = readLocaleUnsafe();
|
|
849
|
+
}
|
|
850
|
+
return getFormatter$1(locale, undefined, undefined, undefined, undefined, unwrap(currency), display, 'currency').format(unwrappedValue);
|
|
851
|
+
}
|
|
852
|
+
const [provideFormatCurrencyDefaults, injectFormatCurrencyOptions] = createFormatterProvider('currency', {
|
|
853
|
+
display: 'symbol',
|
|
854
|
+
}, (a, b) => a.display === b.display && a.fallbackToZero === b.fallbackToZero);
|
|
855
|
+
/**
|
|
856
|
+
* Inject a context-safe currency formatting function tied to the current injector.
|
|
857
|
+
* Uses the libraries locale signal & provided default configuration to react to locale/config changes
|
|
858
|
+
*/
|
|
859
|
+
function injectFormatCurrency() {
|
|
860
|
+
const defaults = injectFormatCurrencyOptions();
|
|
861
|
+
return (value, currency, optOrLocale) => {
|
|
862
|
+
if (!optOrLocale)
|
|
863
|
+
return formatCurrency(value, currency, defaults());
|
|
864
|
+
const unwrapped = unwrap(optOrLocale);
|
|
865
|
+
const opt = typeof unwrapped === 'object'
|
|
866
|
+
? { ...defaults(), ...unwrapped }
|
|
867
|
+
: {
|
|
868
|
+
...defaults(),
|
|
869
|
+
locale: unwrapped,
|
|
870
|
+
};
|
|
871
|
+
return formatCurrency(value, currency, opt);
|
|
872
|
+
};
|
|
608
873
|
}
|
|
609
874
|
|
|
610
875
|
const cache = new Map();
|
|
@@ -617,16 +882,7 @@ function getFormatter(locale, style, numeric) {
|
|
|
617
882
|
}
|
|
618
883
|
return formatter;
|
|
619
884
|
}
|
|
620
|
-
|
|
621
|
-
* Format a relative time using the current or provided locale
|
|
622
|
-
* By default it is reactive to the global dynamic locale, works best when wrapped in a computed() if you need to react to locale changes
|
|
623
|
-
*
|
|
624
|
-
* @param value - The numeric value to use in the relative time internationalization message
|
|
625
|
-
* @param unit - The unit to use in the relative time internationalization message
|
|
626
|
-
* @param opt - Options for formatting
|
|
627
|
-
* @returns Formatted relative time string
|
|
628
|
-
*/
|
|
629
|
-
function formatRelativeTime(value, unit, opt) {
|
|
885
|
+
function formatRelativeTime(value, unit, optOrLocale) {
|
|
630
886
|
const unwrappedValue = unwrap(value);
|
|
631
887
|
if (unwrappedValue === null ||
|
|
632
888
|
unwrappedValue === undefined ||
|
|
@@ -635,9 +891,78 @@ function formatRelativeTime(value, unit, opt) {
|
|
|
635
891
|
const unwrappedUnit = unwrap(unit);
|
|
636
892
|
if (!unwrappedUnit)
|
|
637
893
|
return '';
|
|
638
|
-
const
|
|
639
|
-
|
|
640
|
-
|
|
894
|
+
const unwrappedArgs = unwrap(optOrLocale);
|
|
895
|
+
let locale;
|
|
896
|
+
let style = 'long';
|
|
897
|
+
let numeric = 'always';
|
|
898
|
+
if (typeof unwrappedArgs === 'string') {
|
|
899
|
+
locale = unwrappedArgs;
|
|
900
|
+
}
|
|
901
|
+
else if (unwrappedArgs && typeof unwrappedArgs === 'object') {
|
|
902
|
+
locale = unwrappedArgs.locale ?? readLocaleUnsafe();
|
|
903
|
+
style = unwrappedArgs.style ?? 'long';
|
|
904
|
+
numeric = unwrappedArgs.numeric ?? 'always';
|
|
905
|
+
}
|
|
906
|
+
else {
|
|
907
|
+
locale = readLocaleUnsafe();
|
|
908
|
+
}
|
|
909
|
+
return getFormatter(locale, style, numeric).format(unwrappedValue, unwrappedUnit);
|
|
910
|
+
}
|
|
911
|
+
const [provideFormatRelativeTimeDefaults, injectFormatRelativeTimeOptions] = createFormatterProvider('relativeTime', {
|
|
912
|
+
style: 'long',
|
|
913
|
+
numeric: 'always',
|
|
914
|
+
}, (a, b) => a.style === b.style && a.numeric === b.numeric);
|
|
915
|
+
/**
|
|
916
|
+
* Inject a context-safe relative time formatting function tied to the current injector.
|
|
917
|
+
* Uses the libraries locale signal & provided default configuration to react to locale/config changes
|
|
918
|
+
* @example
|
|
919
|
+
* const formatRelativeTime = injectFormatRelativeTime();
|
|
920
|
+
* readonly relativeAge = computed(() => formatRelativeTime(this.delta(), 'day'));
|
|
921
|
+
*/
|
|
922
|
+
function injectFormatRelativeTime() {
|
|
923
|
+
const defaults = injectFormatRelativeTimeOptions();
|
|
924
|
+
return (value, unit, optOrLocale) => {
|
|
925
|
+
if (!optOrLocale)
|
|
926
|
+
return formatRelativeTime(value, unit, defaults());
|
|
927
|
+
const unwrapped = unwrap(optOrLocale);
|
|
928
|
+
const opt = typeof unwrapped === 'object'
|
|
929
|
+
? { ...defaults(), ...unwrapped }
|
|
930
|
+
: {
|
|
931
|
+
...defaults(),
|
|
932
|
+
locale: unwrapped,
|
|
933
|
+
};
|
|
934
|
+
return formatRelativeTime(value, unit, opt);
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
function provideFormatDefaults(cfg) {
|
|
939
|
+
const providers = [];
|
|
940
|
+
if (cfg.date)
|
|
941
|
+
providers.push(provideFormatDateDefaults(cfg.date));
|
|
942
|
+
if (cfg.displayName)
|
|
943
|
+
providers.push(provideFormatDisplayNameDefaults(cfg.displayName));
|
|
944
|
+
if (cfg.list)
|
|
945
|
+
providers.push(provideFormatListDefaults(cfg.list));
|
|
946
|
+
if (cfg.relativeTime)
|
|
947
|
+
providers.push(provideFormatRelativeTimeDefaults(cfg.relativeTime));
|
|
948
|
+
if (cfg.number)
|
|
949
|
+
providers.push(provideFormatNumberDefaults(cfg.number));
|
|
950
|
+
if (cfg.percent)
|
|
951
|
+
providers.push(provideFormatPercentDefaults(cfg.percent));
|
|
952
|
+
if (cfg.currency)
|
|
953
|
+
providers.push(provideFormatCurrencyDefaults(cfg.currency));
|
|
954
|
+
return providers;
|
|
955
|
+
}
|
|
956
|
+
function injectFormatters() {
|
|
957
|
+
return {
|
|
958
|
+
date: injectFormatDate(),
|
|
959
|
+
displayName: injectFormatDisplayName(),
|
|
960
|
+
list: injectFormatList(),
|
|
961
|
+
relativeTime: injectFormatRelativeTime(),
|
|
962
|
+
number: injectFormatNumber(),
|
|
963
|
+
percent: injectFormatPercent(),
|
|
964
|
+
currency: injectFormatCurrency(),
|
|
965
|
+
};
|
|
641
966
|
}
|
|
642
967
|
|
|
643
968
|
function injectResolveParamLocale(snapshot) {
|
|
@@ -1097,5 +1422,5 @@ function withParams(message) {
|
|
|
1097
1422
|
* Generated bundle index. Do not edit.
|
|
1098
1423
|
*/
|
|
1099
1424
|
|
|
1100
|
-
export { Translate, Translator, canMatchLocale, compileTranslation, createNamespace, formatCurrency, formatDate, formatDisplayName, formatList, formatNumber, formatPercent, formatRelativeTime, injectAddTranslations, injectDynamicLocale, injectIntl, injectResolveParamLocale, injectSupportedLocales, injectUnsafeT, provideIntlConfig, provideMockTranslations, registerNamespace, registerRemoteNamespace, withParams };
|
|
1425
|
+
export { Translate, Translator, canMatchLocale, compileTranslation, createNamespace, formatCurrency, formatDate, formatDisplayName, formatList, formatNumber, formatPercent, formatRelativeTime, injectAddTranslations, injectDynamicLocale, injectFormatCurrency, injectFormatDate, injectFormatDisplayName, injectFormatList, injectFormatNumber, injectFormatPercent, injectFormatRelativeTime, injectFormatters, injectIntl, injectResolveParamLocale, injectSupportedLocales, injectUnsafeT, provideFormatCurrencyDefaults, provideFormatDateDefaults, provideFormatDefaults, provideFormatDisplayNameDefaults, provideFormatListDefaults, provideFormatNumberDefaults, provideFormatPercentDefaults, provideFormatRelativeTimeDefaults, provideIntlConfig, provideMockTranslations, registerNamespace, registerRemoteNamespace, withParams };
|
|
1101
1426
|
//# sourceMappingURL=mmstack-translate.mjs.map
|