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