@rivinia/i18n 0.5.0 → 0.7.1
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/CHANGELOG.md +137 -0
- package/README.md +849 -125
- package/cli/rivinia-i18n-keys.mjs +402 -2
- package/fesm2022/rivinia-i18n-ui.mjs +366 -0
- package/fesm2022/rivinia-i18n-ui.mjs.map +1 -0
- package/fesm2022/rivinia-i18n.mjs +472 -333
- package/fesm2022/rivinia-i18n.mjs.map +1 -1
- package/package.json +5 -1
- package/types/rivinia-i18n-ui.d.ts +112 -0
- package/types/rivinia-i18n.d.ts +82 -103
|
@@ -688,6 +688,370 @@ function resolveUrl(value, base) {
|
|
|
688
688
|
return null;
|
|
689
689
|
}
|
|
690
690
|
}
|
|
691
|
+
var DocumentDirectionApplier = class {
|
|
692
|
+
_deps;
|
|
693
|
+
_applyCount = 0;
|
|
694
|
+
constructor(_deps) {
|
|
695
|
+
this._deps = _deps;
|
|
696
|
+
}
|
|
697
|
+
install(language) {
|
|
698
|
+
effect(() => this.applyDirection(language()));
|
|
699
|
+
}
|
|
700
|
+
applyDirection(language) {
|
|
701
|
+
const root = this._deps.document.documentElement;
|
|
702
|
+
if (!root) return;
|
|
703
|
+
root.dir = language.dir;
|
|
704
|
+
root.lang = language.locale ?? language.id;
|
|
705
|
+
}
|
|
706
|
+
apply(language, callback) {
|
|
707
|
+
const config = this._deps.config;
|
|
708
|
+
const service = this._deps.viewTransition;
|
|
709
|
+
this._applyCount++;
|
|
710
|
+
if (!(config?.enabled && (!config.skipInitialTransition || this._applyCount > 1) && service != null)) {
|
|
711
|
+
callback();
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
const info = {
|
|
715
|
+
language: language.id,
|
|
716
|
+
direction: language.dir
|
|
717
|
+
};
|
|
718
|
+
config.onViewTransitionCreated?.(info);
|
|
719
|
+
service.run(callback);
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
var DictionaryStore = class {
|
|
723
|
+
_deps;
|
|
724
|
+
_visible = signal({}, ...ngDevMode ? [{ debugName: "_visible" }] : /* istanbul ignore next */ []);
|
|
725
|
+
_fallbackDict = signal(null, ...ngDevMode ? [{ debugName: "_fallbackDict" }] : /* istanbul ignore next */ []);
|
|
726
|
+
_loadingState = signal("idle", ...ngDevMode ? [{ debugName: "_loadingState" }] : /* istanbul ignore next */ []);
|
|
727
|
+
_loadingError = signal(null, ...ngDevMode ? [{ debugName: "_loadingError" }] : /* istanbul ignore next */ []);
|
|
728
|
+
_cache = /* @__PURE__ */ new Map();
|
|
729
|
+
_generations = /* @__PURE__ */ new Map();
|
|
730
|
+
_inflight = /* @__PURE__ */ new Map();
|
|
731
|
+
_loadingCount = 0;
|
|
732
|
+
_resolvedLoader = null;
|
|
733
|
+
visible = this._visible.asReadonly();
|
|
734
|
+
fallback = this._fallbackDict.asReadonly();
|
|
735
|
+
loadingState = this._loadingState.asReadonly();
|
|
736
|
+
loadingError = this._loadingError.asReadonly();
|
|
737
|
+
constructor(_deps) {
|
|
738
|
+
this._deps = _deps;
|
|
739
|
+
}
|
|
740
|
+
has(languageId) {
|
|
741
|
+
return this._cache.has(languageId) || this._deps.hasInline(languageId);
|
|
742
|
+
}
|
|
743
|
+
seedVisible(data) {
|
|
744
|
+
this._visible.set(data);
|
|
745
|
+
}
|
|
746
|
+
adoptTransferred(languageId, data) {
|
|
747
|
+
this._cache.set(languageId, data);
|
|
748
|
+
this._apply(languageId, data);
|
|
749
|
+
}
|
|
750
|
+
consumeTransfer(languageId) {
|
|
751
|
+
const transfer = this._deps.transfer;
|
|
752
|
+
if (!transfer) return null;
|
|
753
|
+
const key = this._transferKey(languageId);
|
|
754
|
+
if (!transfer.hasKey(key)) return null;
|
|
755
|
+
const dict = transfer.get(key, null);
|
|
756
|
+
transfer.remove(key);
|
|
757
|
+
return dict;
|
|
758
|
+
}
|
|
759
|
+
async load(languageId, opts) {
|
|
760
|
+
const background = opts?.background === true;
|
|
761
|
+
if (!opts?.force) {
|
|
762
|
+
const cached = this._cache.get(languageId);
|
|
763
|
+
if (cached) {
|
|
764
|
+
this._apply(languageId, cached);
|
|
765
|
+
return {
|
|
766
|
+
data: cached,
|
|
767
|
+
ok: true
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
let task = opts?.force ? void 0 : this._inflight.get(languageId);
|
|
772
|
+
if (!task) {
|
|
773
|
+
task = this._fetch(languageId, this._generationOf(languageId));
|
|
774
|
+
this._inflight.set(languageId, task);
|
|
775
|
+
}
|
|
776
|
+
if (!background) {
|
|
777
|
+
this._loadingCount++;
|
|
778
|
+
this._loadingState.set("loading");
|
|
779
|
+
}
|
|
780
|
+
try {
|
|
781
|
+
const result = await task;
|
|
782
|
+
if (result.ok) this._apply(languageId, result.data);
|
|
783
|
+
else if (!background && result.error !== void 0 && languageId === this._deps.currentLanguage()) {
|
|
784
|
+
this._loadingError.set(result.error);
|
|
785
|
+
this._loadingState.set("error");
|
|
786
|
+
}
|
|
787
|
+
return {
|
|
788
|
+
data: result.data,
|
|
789
|
+
ok: result.ok
|
|
790
|
+
};
|
|
791
|
+
} finally {
|
|
792
|
+
if (!background) {
|
|
793
|
+
this._loadingCount = Math.max(0, this._loadingCount - 1);
|
|
794
|
+
if (this._loadingCount === 0 && this._loadingState() === "loading") this._loadingState.set("idle");
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
reload(languageId) {
|
|
799
|
+
if (languageId !== void 0) {
|
|
800
|
+
if (!this._deps.languageIds().includes(languageId)) return Promise.resolve(false);
|
|
801
|
+
this._invalidate(languageId);
|
|
802
|
+
return this._reloadOne(languageId);
|
|
803
|
+
}
|
|
804
|
+
const currentId = this._deps.currentLanguage();
|
|
805
|
+
const fallbackId = this._deps.fallbackLanguage;
|
|
806
|
+
for (const cachedId of [...this._cache.keys()]) if (cachedId !== currentId && cachedId !== fallbackId) this._cache.delete(cachedId);
|
|
807
|
+
for (const language of this._deps.languageIds()) this._invalidate(language);
|
|
808
|
+
const currentLoad = this._reloadOne(currentId);
|
|
809
|
+
if (fallbackId !== null && fallbackId !== currentId) this._reloadOne(fallbackId);
|
|
810
|
+
return currentLoad;
|
|
811
|
+
}
|
|
812
|
+
ensureFallback() {
|
|
813
|
+
const fallbackId = this._deps.fallbackLanguage;
|
|
814
|
+
if (fallbackId === null || fallbackId === this._deps.currentLanguage()) return;
|
|
815
|
+
if (this.has(fallbackId)) return;
|
|
816
|
+
this.load(fallbackId, { background: true });
|
|
817
|
+
}
|
|
818
|
+
_reloadOne(languageId) {
|
|
819
|
+
return this.load(languageId, {
|
|
820
|
+
force: true,
|
|
821
|
+
background: languageId !== this._deps.currentLanguage()
|
|
822
|
+
}).then((result) => result.ok);
|
|
823
|
+
}
|
|
824
|
+
_apply(languageId, data) {
|
|
825
|
+
if (languageId === this._deps.currentLanguage()) {
|
|
826
|
+
this._visible.set(data);
|
|
827
|
+
this._loadingError.set(null);
|
|
828
|
+
if (this._loadingCount === 0 && this._loadingState() === "error") this._loadingState.set("idle");
|
|
829
|
+
}
|
|
830
|
+
if (languageId === this._deps.fallbackLanguage) this._fallbackDict.set(data);
|
|
831
|
+
}
|
|
832
|
+
_fetch(languageId, generation) {
|
|
833
|
+
let task;
|
|
834
|
+
task = (async () => {
|
|
835
|
+
const stale = () => this._generationOf(languageId) !== generation;
|
|
836
|
+
try {
|
|
837
|
+
if (!this._resolvedLoader) this._resolvedLoader = await this._deps.loader(this._deps.loaderContext);
|
|
838
|
+
const translations = await this._resolvedLoader.load(languageId, this._deps.loaderContext);
|
|
839
|
+
if (stale()) return {
|
|
840
|
+
data: {},
|
|
841
|
+
ok: false
|
|
842
|
+
};
|
|
843
|
+
this._cache.set(languageId, translations);
|
|
844
|
+
this._writeTransfer(languageId, translations);
|
|
845
|
+
return {
|
|
846
|
+
data: translations,
|
|
847
|
+
ok: true
|
|
848
|
+
};
|
|
849
|
+
} catch (error) {
|
|
850
|
+
if (stale()) return {
|
|
851
|
+
data: {},
|
|
852
|
+
ok: false
|
|
853
|
+
};
|
|
854
|
+
this._deps.onLoadError?.(languageId, error);
|
|
855
|
+
return {
|
|
856
|
+
data: {},
|
|
857
|
+
ok: false,
|
|
858
|
+
error
|
|
859
|
+
};
|
|
860
|
+
} finally {
|
|
861
|
+
if (this._inflight.get(languageId) === task) this._inflight.delete(languageId);
|
|
862
|
+
}
|
|
863
|
+
})();
|
|
864
|
+
return task;
|
|
865
|
+
}
|
|
866
|
+
_generationOf(languageId) {
|
|
867
|
+
return this._generations.get(languageId) ?? 0;
|
|
868
|
+
}
|
|
869
|
+
_invalidate(languageId) {
|
|
870
|
+
this._generations.set(languageId, this._generationOf(languageId) + 1);
|
|
871
|
+
}
|
|
872
|
+
_transferKey(languageId) {
|
|
873
|
+
return makeStateKey(`rivinia.i18n.dict.${languageId}`);
|
|
874
|
+
}
|
|
875
|
+
_writeTransfer(languageId, dict) {
|
|
876
|
+
if (!this._deps.request) return;
|
|
877
|
+
this._deps.transfer?.set(this._transferKey(languageId), dict);
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
var LanguageStorage = class {
|
|
881
|
+
_deps;
|
|
882
|
+
constructor(_deps) {
|
|
883
|
+
this._deps = _deps;
|
|
884
|
+
}
|
|
885
|
+
read() {
|
|
886
|
+
if (this._deps.strategy === "cookie") {
|
|
887
|
+
const header = this._deps.request?.headers.get("cookie") ?? null;
|
|
888
|
+
if (header !== null) return parseCookie(header, this._deps.key);
|
|
889
|
+
try {
|
|
890
|
+
return parseCookie(this._deps.document.cookie, this._deps.key);
|
|
891
|
+
} catch {
|
|
892
|
+
return null;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
return safeGetItem(this._deps.key, this._deps.strategy);
|
|
896
|
+
}
|
|
897
|
+
write(id) {
|
|
898
|
+
if (this._deps.strategy === "cookie") {
|
|
899
|
+
if (this._deps.request) return;
|
|
900
|
+
try {
|
|
901
|
+
const location = this._deps.document?.location;
|
|
902
|
+
this._deps.document.cookie = serializeCookie(this._deps.key, id, { secure: shouldSecureCookie(location) });
|
|
903
|
+
} catch {}
|
|
904
|
+
return;
|
|
905
|
+
}
|
|
906
|
+
safeSetItem(this._deps.key, id, this._deps.strategy);
|
|
907
|
+
}
|
|
908
|
+
persistAfterSwitch(id) {
|
|
909
|
+
if (!this._deps.persist) return;
|
|
910
|
+
if (this.read() === id) return;
|
|
911
|
+
this.write(id);
|
|
912
|
+
}
|
|
913
|
+
resolveInitialLanguage() {
|
|
914
|
+
const stored = this.read();
|
|
915
|
+
const languages = this._deps.languages();
|
|
916
|
+
if (stored && languages.some((l) => l.id === stored)) return stored;
|
|
917
|
+
if (this._deps.detectBrowser) {
|
|
918
|
+
const detected = detectBrowserLanguage(languages.map((l) => l.id), this._deps.browserLanguages());
|
|
919
|
+
if (detected) return detected;
|
|
920
|
+
}
|
|
921
|
+
return this._deps.defaultLanguage;
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
var MissingKeyReporter = class {
|
|
925
|
+
_deps;
|
|
926
|
+
_missingKeys = signal([], ...ngDevMode ? [{ debugName: "_missingKeys" }] : /* istanbul ignore next */ []);
|
|
927
|
+
_reportedKeys = /* @__PURE__ */ new Set();
|
|
928
|
+
_pendingKeys = [];
|
|
929
|
+
_pendingKeyEvents = [];
|
|
930
|
+
_keyFlushQueued = false;
|
|
931
|
+
_reportedParams = /* @__PURE__ */ new Set();
|
|
932
|
+
_pendingParamEvents = [];
|
|
933
|
+
_paramFlushQueued = false;
|
|
934
|
+
missingKeys = this._missingKeys.asReadonly();
|
|
935
|
+
constructor(_deps) {
|
|
936
|
+
this._deps = _deps;
|
|
937
|
+
}
|
|
938
|
+
reportKey(key) {
|
|
939
|
+
if (!this._deps.hasDictionary(this._deps.language())) return;
|
|
940
|
+
if (this._reportedKeys.has(key)) return;
|
|
941
|
+
this._reportedKeys.add(key);
|
|
942
|
+
const language = this._deps.language();
|
|
943
|
+
const fallbackLanguage = this._deps.fallbackLanguage;
|
|
944
|
+
this._pendingKeys.push(key);
|
|
945
|
+
this._pendingKeyEvents.push(fallbackLanguage === null ? {
|
|
946
|
+
key,
|
|
947
|
+
language
|
|
948
|
+
} : {
|
|
949
|
+
key,
|
|
950
|
+
language,
|
|
951
|
+
fallbackLanguage
|
|
952
|
+
});
|
|
953
|
+
this._queueKeyFlush();
|
|
954
|
+
if (this._deps.warnOnMissingKey) console.warn(`[rivinia/i18n] Missing key "${key}" for language "${language}"` + (fallbackLanguage === null ? "." : ` (fallback "${fallbackLanguage}" has no value either).`));
|
|
955
|
+
}
|
|
956
|
+
reportParam(key, param) {
|
|
957
|
+
const dedupe = `${key}\u0000${param}`;
|
|
958
|
+
if (this._reportedParams.has(dedupe)) return;
|
|
959
|
+
this._reportedParams.add(dedupe);
|
|
960
|
+
const language = this._deps.language();
|
|
961
|
+
this._pendingParamEvents.push({
|
|
962
|
+
key,
|
|
963
|
+
param,
|
|
964
|
+
language
|
|
965
|
+
});
|
|
966
|
+
this._queueParamFlush();
|
|
967
|
+
if (this._deps.warnOnMissingParam) console.warn(`[rivinia/i18n] Missing parameter "${param}" for key "${key}" and language "${language}"; the placeholder was kept.`);
|
|
968
|
+
}
|
|
969
|
+
clearKeys() {
|
|
970
|
+
this._reportedKeys.clear();
|
|
971
|
+
this._pendingKeys.length = 0;
|
|
972
|
+
this._pendingKeyEvents.length = 0;
|
|
973
|
+
this._missingKeys.set([]);
|
|
974
|
+
}
|
|
975
|
+
clearParams() {
|
|
976
|
+
this._reportedParams.clear();
|
|
977
|
+
this._pendingParamEvents.length = 0;
|
|
978
|
+
}
|
|
979
|
+
_queueKeyFlush() {
|
|
980
|
+
if (this._keyFlushQueued) return;
|
|
981
|
+
this._keyFlushQueued = true;
|
|
982
|
+
queueMicrotask(() => {
|
|
983
|
+
this._keyFlushQueued = false;
|
|
984
|
+
const pending = this._pendingKeys.splice(0);
|
|
985
|
+
if (pending.length === 0) return;
|
|
986
|
+
this._missingKeys.update((current) => [...current, ...pending]);
|
|
987
|
+
const events = this._pendingKeyEvents.splice(0);
|
|
988
|
+
for (const event of events) this._deps.onMissingKey?.(event);
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
_queueParamFlush() {
|
|
992
|
+
if (this._paramFlushQueued) return;
|
|
993
|
+
this._paramFlushQueued = true;
|
|
994
|
+
queueMicrotask(() => {
|
|
995
|
+
this._paramFlushQueued = false;
|
|
996
|
+
const events = this._pendingParamEvents.splice(0);
|
|
997
|
+
for (const event of events) this._deps.onMissingParam?.(event);
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
const FORMATTER_CACHE_LIMIT = 40;
|
|
1002
|
+
var MessageFormatter = class {
|
|
1003
|
+
_deps;
|
|
1004
|
+
_capacity;
|
|
1005
|
+
_formatters = /* @__PURE__ */ new Map();
|
|
1006
|
+
constructor(_deps, _capacity = FORMATTER_CACHE_LIMIT) {
|
|
1007
|
+
this._deps = _deps;
|
|
1008
|
+
this._capacity = _capacity;
|
|
1009
|
+
}
|
|
1010
|
+
get cachedFormatterCount() {
|
|
1011
|
+
return this._formatters.size;
|
|
1012
|
+
}
|
|
1013
|
+
render(template, params, locale, key) {
|
|
1014
|
+
const quoting = template.includes("'");
|
|
1015
|
+
if (params === void 0 && !quoting) return template;
|
|
1016
|
+
if (!quoting && !template.includes("{")) return template;
|
|
1017
|
+
try {
|
|
1018
|
+
return formatIcu(template, params ?? {}, locale, { onMissingParam: (name) => this._deps.onMissingParam?.(key, name) });
|
|
1019
|
+
} catch {
|
|
1020
|
+
return template;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
formatNumber(value, options) {
|
|
1024
|
+
return this._numberFormatter(options).format(value);
|
|
1025
|
+
}
|
|
1026
|
+
formatDate(value, options) {
|
|
1027
|
+
return this._dateTimeFormatter(options).format(value);
|
|
1028
|
+
}
|
|
1029
|
+
_numberFormatter(options) {
|
|
1030
|
+
const locale = this._deps.locale();
|
|
1031
|
+
const key = `n|${locale}|${JSON.stringify(options ?? null)}`;
|
|
1032
|
+
return this._memo(key, () => new Intl.NumberFormat(locale, options));
|
|
1033
|
+
}
|
|
1034
|
+
_dateTimeFormatter(options) {
|
|
1035
|
+
const locale = this._deps.locale();
|
|
1036
|
+
const key = `d|${locale}|${JSON.stringify(options ?? null)}`;
|
|
1037
|
+
return this._memo(key, () => new Intl.DateTimeFormat(locale, options));
|
|
1038
|
+
}
|
|
1039
|
+
_memo(key, create) {
|
|
1040
|
+
const cached = this._formatters.get(key);
|
|
1041
|
+
if (cached) {
|
|
1042
|
+
this._formatters.delete(key);
|
|
1043
|
+
this._formatters.set(key, cached);
|
|
1044
|
+
return cached;
|
|
1045
|
+
}
|
|
1046
|
+
const created = create();
|
|
1047
|
+
if (this._formatters.size >= this._capacity) {
|
|
1048
|
+
const oldest = this._formatters.keys().next().value;
|
|
1049
|
+
if (oldest !== void 0) this._formatters.delete(oldest);
|
|
1050
|
+
}
|
|
1051
|
+
this._formatters.set(key, created);
|
|
1052
|
+
return created;
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
691
1055
|
var ViewTransitionService = class ViewTransitionService {
|
|
692
1056
|
_doc = inject(DOCUMENT);
|
|
693
1057
|
run(callback) {
|
|
@@ -720,29 +1084,55 @@ i0.ɵɵngDeclareClassMetadata({
|
|
|
720
1084
|
var RiviniaI18n = class RiviniaI18n {
|
|
721
1085
|
_config = inject(RIVINIA_I18N_CONFIG);
|
|
722
1086
|
_viewTransition = inject(ViewTransitionService, { optional: true });
|
|
723
|
-
_storageKey = this._config.storageKey ?? "rivinia.i18n.language";
|
|
724
|
-
_storageStrategy = this._config.storageStrategy ?? "local";
|
|
725
1087
|
_autoApplyDirection = this._config.autoApplyDirection ?? true;
|
|
726
|
-
_persist = this._config.persist ?? true;
|
|
727
1088
|
_fallbackId = this._config.fallbackLanguage ?? null;
|
|
728
1089
|
_doc = inject(DOCUMENT);
|
|
729
1090
|
_request = inject(REQUEST, { optional: true });
|
|
730
1091
|
_transfer = inject(TransferState, { optional: true });
|
|
1092
|
+
_direction = new DocumentDirectionApplier({
|
|
1093
|
+
document: this._doc,
|
|
1094
|
+
viewTransition: this._viewTransition,
|
|
1095
|
+
config: this._config.viewTransition
|
|
1096
|
+
});
|
|
731
1097
|
loaderContext = createLoaderContext(this._request ?? null, this._doc);
|
|
732
|
-
_currentLanguageId = signal(this._resolveInitialLanguage(), ...ngDevMode ? [{ debugName: "_currentLanguageId" }] : /* istanbul ignore next */ []);
|
|
733
1098
|
_languages = signal(this._config.languages, ...ngDevMode ? [{ debugName: "_languages" }] : /* istanbul ignore next */ []);
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
1099
|
+
_defaultLanguageId = resolveDefaultLanguage(this._config);
|
|
1100
|
+
_languageStorage = new LanguageStorage({
|
|
1101
|
+
key: this._config.storageKey ?? "rivinia.i18n.language",
|
|
1102
|
+
strategy: this._config.storageStrategy ?? "local",
|
|
1103
|
+
languages: () => this._languages(),
|
|
1104
|
+
defaultLanguage: this._defaultLanguageId,
|
|
1105
|
+
detectBrowser: this._config.detectBrowser ?? false,
|
|
1106
|
+
document: this._doc,
|
|
1107
|
+
request: this._request ?? null,
|
|
1108
|
+
persist: this._config.persist ?? true,
|
|
1109
|
+
browserLanguages
|
|
1110
|
+
});
|
|
1111
|
+
_currentLanguageId = signal(this._languageStorage.resolveInitialLanguage(), ...ngDevMode ? [{ debugName: "_currentLanguageId" }] : /* istanbul ignore next */ []);
|
|
1112
|
+
_warnedLoadErrors = /* @__PURE__ */ new Set();
|
|
1113
|
+
_onDictionaryLoadError = (language, error) => {
|
|
1114
|
+
if (this._config.onLoadError) {
|
|
1115
|
+
this._config.onLoadError(language, error);
|
|
1116
|
+
return;
|
|
1117
|
+
}
|
|
1118
|
+
if (!(this._config.warnOnLoadError ?? isDevMode())) return;
|
|
1119
|
+
if (this._warnedLoadErrors.has(language)) return;
|
|
1120
|
+
this._warnedLoadErrors.add(language);
|
|
1121
|
+
console.error(`[rivinia/i18n] Failed to load the "${language}" dictionary — every key will render as its raw name. Pass \`onLoadError\` to handle load failures yourself.`, error);
|
|
1122
|
+
};
|
|
1123
|
+
_store = new DictionaryStore({
|
|
1124
|
+
loader: (context) => this._config.loader ? this._config.loader.resolve(context) : jsonLoader().resolve(context),
|
|
1125
|
+
loaderContext: this.loaderContext,
|
|
1126
|
+
request: this._request ?? null,
|
|
1127
|
+
transfer: this._transfer,
|
|
1128
|
+
onLoadError: this._onDictionaryLoadError,
|
|
1129
|
+
currentLanguage: () => this._currentLanguageId(),
|
|
1130
|
+
fallbackLanguage: this._fallbackId,
|
|
1131
|
+
languageIds: () => this._languages().map((l) => l.id),
|
|
1132
|
+
hasInline: (languageId) => this._config.translations?.[languageId] !== void 0
|
|
1133
|
+
});
|
|
743
1134
|
_initialized = false;
|
|
744
1135
|
_tasks = inject(PendingTasks, { optional: true });
|
|
745
|
-
languageChanged = computed(() => this._currentLanguageId(), ...ngDevMode ? [{ debugName: "languageChanged" }] : /* istanbul ignore next */ []);
|
|
746
1136
|
languages = this._languages.asReadonly();
|
|
747
1137
|
currentLanguage = this._currentLanguageId.asReadonly();
|
|
748
1138
|
currentLanguageObject = computed(() => {
|
|
@@ -751,45 +1141,30 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
751
1141
|
}, ...ngDevMode ? [{ debugName: "currentLanguageObject" }] : /* istanbul ignore next */ []);
|
|
752
1142
|
dir = computed(() => this.currentLanguageObject().dir, ...ngDevMode ? [{ debugName: "dir" }] : /* istanbul ignore next */ []);
|
|
753
1143
|
isRtl = computed(() => this.dir() === "rtl", ...ngDevMode ? [{ debugName: "isRtl" }] : /* istanbul ignore next */ []);
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
loadingError = this._loadingError.asReadonly();
|
|
758
|
-
isLoading = computed(() => this._loadingState() === "loading", ...ngDevMode ? [{ debugName: "isLoading" }] : /* istanbul ignore next */ []);
|
|
1144
|
+
loadingState = this._store.loadingState;
|
|
1145
|
+
loadingError = this._store.loadingError;
|
|
1146
|
+
isLoading = computed(() => this._store.loadingState() === "loading", ...ngDevMode ? [{ debugName: "isLoading" }] : /* istanbul ignore next */ []);
|
|
759
1147
|
_ready = signal(false, ...ngDevMode ? [{ debugName: "_ready" }] : /* istanbul ignore next */ []);
|
|
760
1148
|
ready = this._ready.asReadonly();
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
missingKeys = this._missingKeys.asReadonly();
|
|
1149
|
+
_missingKeyReporter = new MissingKeyReporter({
|
|
1150
|
+
language: () => this._currentLanguageId(),
|
|
1151
|
+
fallbackLanguage: this._fallbackId,
|
|
1152
|
+
hasDictionary: (language) => this._hasDictionary(language),
|
|
1153
|
+
onMissingKey: this._config.onMissingKey,
|
|
1154
|
+
onMissingParam: this._config.onMissingParam,
|
|
1155
|
+
warnOnMissingKey: this._config.warnOnMissingKey ?? (isDevMode() && this._config.onMissingKey === void 0),
|
|
1156
|
+
warnOnMissingParam: this._config.warnOnMissingParam ?? (isDevMode() && this._config.onMissingParam === void 0)
|
|
1157
|
+
});
|
|
1158
|
+
missingKeys = this._missingKeyReporter.missingKeys;
|
|
772
1159
|
clearMissingKeys() {
|
|
773
|
-
this.
|
|
774
|
-
this._pendingMissingKeys.length = 0;
|
|
775
|
-
this._pendingMissingEvents.length = 0;
|
|
776
|
-
this._missingKeys.set([]);
|
|
1160
|
+
this._missingKeyReporter.clearKeys();
|
|
777
1161
|
}
|
|
778
1162
|
clearMissingParams() {
|
|
779
|
-
this.
|
|
780
|
-
this._pendingMissingParams.length = 0;
|
|
1163
|
+
this._missingKeyReporter.clearParams();
|
|
781
1164
|
}
|
|
782
1165
|
constructor() {
|
|
783
|
-
if (this._config.languages.length === 0) throw new Error("[rivinia/i18n] At least one language must be provided in the languages array.");
|
|
784
|
-
if (!this._config.languages.some((l) => l.id === this._config.defaultLanguage)) throw new Error(`[rivinia/i18n] defaultLanguage "${this._config.defaultLanguage}" must be one of the configured language IDs.`);
|
|
785
1166
|
if (this._fallbackId !== null && !this._config.languages.some((l) => l.id === this._fallbackId)) throw new Error(`[rivinia/i18n] fallbackLanguage "${this._fallbackId}" must be one of the configured language IDs.`);
|
|
786
|
-
if (this._autoApplyDirection)
|
|
787
|
-
const lang = this.currentLanguageObject();
|
|
788
|
-
const root = this._doc.documentElement;
|
|
789
|
-
if (!root) return;
|
|
790
|
-
root.dir = lang.dir;
|
|
791
|
-
root.lang = lang.locale ?? lang.id;
|
|
792
|
-
});
|
|
1167
|
+
if (this._autoApplyDirection) this._direction.install(() => this.currentLanguageObject());
|
|
793
1168
|
}
|
|
794
1169
|
getLanguage(id) {
|
|
795
1170
|
return this._languages().find((l) => l.id === id);
|
|
@@ -803,28 +1178,20 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
803
1178
|
const fallback = this._fallbackId;
|
|
804
1179
|
const hasFallback = fallback !== null && fallback !== current;
|
|
805
1180
|
const preload = this._config.preload ?? true;
|
|
806
|
-
const transferred = this.
|
|
807
|
-
if (transferred)
|
|
808
|
-
|
|
809
|
-
this._loadedTranslations.set(transferred);
|
|
810
|
-
} else {
|
|
1181
|
+
const transferred = this._store.consumeTransfer(current);
|
|
1182
|
+
if (transferred) this._store.adoptTransferred(current, transferred);
|
|
1183
|
+
else {
|
|
811
1184
|
const inline = this._config.translations?.[current];
|
|
812
|
-
if (inline) this.
|
|
1185
|
+
if (inline) this._store.seedVisible(inline);
|
|
813
1186
|
}
|
|
814
1187
|
let fallbackTransferred = null;
|
|
815
1188
|
if (hasFallback) {
|
|
816
|
-
fallbackTransferred = this.
|
|
817
|
-
if (fallbackTransferred)
|
|
818
|
-
this._translationsCache.set(fallback, fallbackTransferred);
|
|
819
|
-
this._fallbackDict.set(fallbackTransferred);
|
|
820
|
-
}
|
|
1189
|
+
fallbackTransferred = this._store.consumeTransfer(fallback);
|
|
1190
|
+
if (fallbackTransferred) this._store.adoptTransferred(fallback, fallbackTransferred);
|
|
821
1191
|
}
|
|
822
1192
|
const loads = [];
|
|
823
|
-
if (!transferred && preload) loads.push(this.
|
|
824
|
-
if (hasFallback && !fallbackTransferred && preload) loads.push(this.
|
|
825
|
-
silent: true,
|
|
826
|
-
background: true
|
|
827
|
-
}));
|
|
1193
|
+
if (!transferred && preload) loads.push(this._store.load(current));
|
|
1194
|
+
if (hasFallback && !fallbackTransferred && preload) loads.push(this._store.load(fallback, { background: true }));
|
|
828
1195
|
await Promise.all(loads);
|
|
829
1196
|
} finally {
|
|
830
1197
|
this._ready.set(true);
|
|
@@ -855,35 +1222,12 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
855
1222
|
const fallback = defaultValue ?? key;
|
|
856
1223
|
return params ? this.render(fallback, params, this.locale(), key) : fallback;
|
|
857
1224
|
}
|
|
858
|
-
loadedTranslations = this.
|
|
859
|
-
async loadTranslations(languageId
|
|
860
|
-
return (await this.
|
|
1225
|
+
loadedTranslations = this._store.visible;
|
|
1226
|
+
async loadTranslations(languageId) {
|
|
1227
|
+
return (await this._store.load(languageId)).data;
|
|
861
1228
|
}
|
|
862
1229
|
reload(languageId) {
|
|
863
|
-
|
|
864
|
-
if (!this._languages().some((l) => l.id === languageId)) return Promise.resolve(false);
|
|
865
|
-
this._invalidate(languageId);
|
|
866
|
-
return this._reloadOne(languageId);
|
|
867
|
-
}
|
|
868
|
-
const currentId = this._currentLanguageId();
|
|
869
|
-
const fallbackId = this._fallbackId;
|
|
870
|
-
for (const cachedId of [...this._translationsCache.keys()]) if (cachedId !== currentId && cachedId !== fallbackId) this._translationsCache.delete(cachedId);
|
|
871
|
-
for (const language of this._languages()) this._invalidate(language.id);
|
|
872
|
-
const currentLoad = this._reloadOne(currentId);
|
|
873
|
-
if (fallbackId !== null && fallbackId !== currentId) this._reloadOne(fallbackId);
|
|
874
|
-
return currentLoad;
|
|
875
|
-
}
|
|
876
|
-
_reloadOne(languageId) {
|
|
877
|
-
return this._loadTranslations(languageId, {
|
|
878
|
-
force: true,
|
|
879
|
-
background: languageId !== this._currentLanguageId()
|
|
880
|
-
}).then((result) => result.ok);
|
|
881
|
-
}
|
|
882
|
-
_generationOf(languageId) {
|
|
883
|
-
return this._generations.get(languageId) ?? 0;
|
|
884
|
-
}
|
|
885
|
-
_invalidate(languageId) {
|
|
886
|
-
this._generations.set(languageId, this._generationOf(languageId) + 1);
|
|
1230
|
+
return this._store.reload(languageId);
|
|
887
1231
|
}
|
|
888
1232
|
toggle() {
|
|
889
1233
|
const langs = this._languages();
|
|
@@ -914,82 +1258,46 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
914
1258
|
async setLanguageAsync(id) {
|
|
915
1259
|
if (id === this._currentLanguageId()) return;
|
|
916
1260
|
if (!this._languages().some((l) => l.id === id)) return;
|
|
917
|
-
const { ok } = await this.
|
|
1261
|
+
const { ok } = await this._store.load(id);
|
|
918
1262
|
if (!ok) return;
|
|
919
1263
|
this.applyLanguage(id);
|
|
920
1264
|
}
|
|
921
1265
|
formatNumber(value, options) {
|
|
922
|
-
return this.
|
|
1266
|
+
return this._messageFormatter.formatNumber(value, options);
|
|
923
1267
|
}
|
|
924
1268
|
formatDate(value, options) {
|
|
925
|
-
return this.
|
|
1269
|
+
return this._messageFormatter.formatDate(value, options);
|
|
926
1270
|
}
|
|
927
1271
|
locale = computed(() => {
|
|
928
1272
|
return this.currentLanguageObject().locale ?? this._currentLanguageId();
|
|
929
1273
|
}, ...ngDevMode ? [{ debugName: "locale" }] : /* istanbul ignore next */ []);
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
if (!formatter) {
|
|
935
|
-
formatter = new Intl.NumberFormat(this.locale(), options);
|
|
936
|
-
this._formatters.set(key, formatter);
|
|
937
|
-
}
|
|
938
|
-
return formatter;
|
|
939
|
-
}
|
|
940
|
-
dateTimeFormatter(options) {
|
|
941
|
-
const key = `d|${this.locale()}|${JSON.stringify(options ?? null)}`;
|
|
942
|
-
let formatter = this._formatters.get(key);
|
|
943
|
-
if (!formatter) {
|
|
944
|
-
formatter = new Intl.DateTimeFormat(this.locale(), options);
|
|
945
|
-
this._formatters.set(key, formatter);
|
|
946
|
-
}
|
|
947
|
-
return formatter;
|
|
948
|
-
}
|
|
1274
|
+
_messageFormatter = new MessageFormatter({
|
|
1275
|
+
locale: () => this.locale(),
|
|
1276
|
+
onMissingParam: (key, name) => untracked(() => this._reportMissingParam(key, name))
|
|
1277
|
+
});
|
|
949
1278
|
applyLanguage(id, opts) {
|
|
950
1279
|
if (id === this._currentLanguageId()) return;
|
|
951
1280
|
const lang = this._languages().find((l) => l.id === id);
|
|
952
1281
|
if (!lang) return;
|
|
953
|
-
this.
|
|
954
|
-
const apply = () => this._currentLanguageId.set(id);
|
|
955
|
-
const vt = this._config.viewTransition;
|
|
956
|
-
if (vt?.enabled && (!vt.skipInitialTransition || this._applyCount > 1) && this._viewTransition != null) {
|
|
957
|
-
vt.onViewTransitionCreated?.({
|
|
958
|
-
language: id,
|
|
959
|
-
direction: lang.dir
|
|
960
|
-
});
|
|
961
|
-
this._viewTransition.run(apply);
|
|
962
|
-
} else apply();
|
|
1282
|
+
this._direction.apply(lang, () => this._currentLanguageId.set(id));
|
|
963
1283
|
if (!opts?.skipLoad) {
|
|
964
|
-
this.
|
|
965
|
-
this.
|
|
1284
|
+
this._store.load(id);
|
|
1285
|
+
this._store.ensureFallback();
|
|
966
1286
|
}
|
|
967
|
-
this.
|
|
968
|
-
}
|
|
969
|
-
_persistLanguage(id) {
|
|
970
|
-
if (!this._persist) return;
|
|
971
|
-
if (this._readStored() === id) return;
|
|
972
|
-
this._writeStored(id);
|
|
1287
|
+
this._languageStorage.persistAfterSwitch(id);
|
|
973
1288
|
}
|
|
974
1289
|
render(template, params, locale, key) {
|
|
975
|
-
|
|
976
|
-
if (params === void 0 && !quoting) return template;
|
|
977
|
-
if (!quoting && !template.includes("{")) return template;
|
|
978
|
-
try {
|
|
979
|
-
return formatIcu(template, params ?? {}, locale, { onMissingParam: (name) => untracked(() => this._reportMissingParam(key, name)) });
|
|
980
|
-
} catch {
|
|
981
|
-
return template;
|
|
982
|
-
}
|
|
1290
|
+
return this._messageFormatter.render(template, params, locale, key);
|
|
983
1291
|
}
|
|
984
1292
|
_lookupKey(key) {
|
|
985
|
-
const currentValue = this._resolveKey(this.
|
|
1293
|
+
const currentValue = this._resolveKey(this._store.visible(), key);
|
|
986
1294
|
if (currentValue !== void 0) return {
|
|
987
1295
|
text: currentValue,
|
|
988
1296
|
locale: this.locale()
|
|
989
1297
|
};
|
|
990
1298
|
const fallbackId = this._fallbackId;
|
|
991
1299
|
if (fallbackId === null || fallbackId === this._currentLanguageId()) return void 0;
|
|
992
|
-
const fallbackDict = this.
|
|
1300
|
+
const fallbackDict = this._store.fallback() ?? this._config.translations?.[fallbackId] ?? null;
|
|
993
1301
|
if (!fallbackDict) return void 0;
|
|
994
1302
|
const fallbackValue = this._resolveKey(fallbackDict, key);
|
|
995
1303
|
if (fallbackValue === void 0) return void 0;
|
|
@@ -1001,205 +1309,20 @@ var RiviniaI18n = class RiviniaI18n {
|
|
|
1001
1309
|
_localeOf(languageId) {
|
|
1002
1310
|
return this._languages().find((l) => l.id === languageId)?.locale ?? languageId;
|
|
1003
1311
|
}
|
|
1004
|
-
_ensureFallback() {
|
|
1005
|
-
const fallbackId = this._fallbackId;
|
|
1006
|
-
if (fallbackId === null || fallbackId === this._currentLanguageId()) return;
|
|
1007
|
-
if (this._hasDictionary(fallbackId)) return;
|
|
1008
|
-
this._loadTranslations(fallbackId, {
|
|
1009
|
-
silent: true,
|
|
1010
|
-
background: true
|
|
1011
|
-
});
|
|
1012
|
-
}
|
|
1013
1312
|
_hasDictionary(languageId) {
|
|
1014
|
-
return this.
|
|
1313
|
+
return this._store.has(languageId);
|
|
1015
1314
|
}
|
|
1016
1315
|
_reportMissingKey(key) {
|
|
1017
|
-
|
|
1018
|
-
if (this._reportedMissingKeys.has(key)) return;
|
|
1019
|
-
this._reportedMissingKeys.add(key);
|
|
1020
|
-
const language = this._currentLanguageId();
|
|
1021
|
-
const fallbackLanguage = this._fallbackId;
|
|
1022
|
-
const event = fallbackLanguage === null ? {
|
|
1023
|
-
key,
|
|
1024
|
-
language
|
|
1025
|
-
} : {
|
|
1026
|
-
key,
|
|
1027
|
-
language,
|
|
1028
|
-
fallbackLanguage
|
|
1029
|
-
};
|
|
1030
|
-
this._pendingMissingKeys.push(key);
|
|
1031
|
-
this._pendingMissingEvents.push(event);
|
|
1032
|
-
this._queueMissingFlush();
|
|
1033
|
-
if (this._warnOnMissingKey) console.warn(`[rivinia/i18n] Missing key "${key}" for language "${language}"` + (fallbackLanguage === null ? "." : ` (fallback "${fallbackLanguage}" has no value either).`));
|
|
1034
|
-
}
|
|
1035
|
-
_queueMissingFlush() {
|
|
1036
|
-
if (this._missingFlushQueued) return;
|
|
1037
|
-
this._missingFlushQueued = true;
|
|
1038
|
-
queueMicrotask(() => {
|
|
1039
|
-
this._missingFlushQueued = false;
|
|
1040
|
-
const pending = this._pendingMissingKeys.splice(0);
|
|
1041
|
-
if (pending.length === 0) return;
|
|
1042
|
-
this._missingKeys.update((current) => [...current, ...pending]);
|
|
1043
|
-
const events = this._pendingMissingEvents.splice(0);
|
|
1044
|
-
for (const event of events) this._config.onMissingKey?.(event);
|
|
1045
|
-
});
|
|
1316
|
+
this._missingKeyReporter.reportKey(key);
|
|
1046
1317
|
}
|
|
1047
1318
|
_reportMissingParam(key, param) {
|
|
1048
|
-
|
|
1049
|
-
if (this._reportedMissingParams.has(dedupe)) return;
|
|
1050
|
-
this._reportedMissingParams.add(dedupe);
|
|
1051
|
-
const language = this._currentLanguageId();
|
|
1052
|
-
this._pendingMissingParams.push({
|
|
1053
|
-
key,
|
|
1054
|
-
param,
|
|
1055
|
-
language
|
|
1056
|
-
});
|
|
1057
|
-
this._queueMissingParamsFlush();
|
|
1058
|
-
if (this._warnOnMissingParam) console.warn(`[rivinia/i18n] Missing parameter "${param}" for key "${key}" and language "${language}"; the placeholder was kept.`);
|
|
1059
|
-
}
|
|
1060
|
-
_queueMissingParamsFlush() {
|
|
1061
|
-
if (this._missingParamsFlushQueued) return;
|
|
1062
|
-
this._missingParamsFlushQueued = true;
|
|
1063
|
-
queueMicrotask(() => {
|
|
1064
|
-
this._missingParamsFlushQueued = false;
|
|
1065
|
-
const events = this._pendingMissingParams.splice(0);
|
|
1066
|
-
for (const event of events) this._config.onMissingParam?.(event);
|
|
1067
|
-
});
|
|
1068
|
-
}
|
|
1069
|
-
async _loadTranslations(languageId, opts) {
|
|
1070
|
-
const background = opts?.background === true;
|
|
1071
|
-
if (!opts?.force) {
|
|
1072
|
-
const cached = this._translationsCache.get(languageId);
|
|
1073
|
-
if (cached) {
|
|
1074
|
-
this._applyDictionary(languageId, cached);
|
|
1075
|
-
return {
|
|
1076
|
-
data: cached,
|
|
1077
|
-
ok: true
|
|
1078
|
-
};
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
let task = opts?.force ? void 0 : this._inflight.get(languageId);
|
|
1082
|
-
if (!task) {
|
|
1083
|
-
task = this._fetchDictionary(languageId, this._generationOf(languageId));
|
|
1084
|
-
this._inflight.set(languageId, task);
|
|
1085
|
-
}
|
|
1086
|
-
if (!background) {
|
|
1087
|
-
this._loadingCount++;
|
|
1088
|
-
this._loadingState.set("loading");
|
|
1089
|
-
}
|
|
1090
|
-
try {
|
|
1091
|
-
const result = await task;
|
|
1092
|
-
if (result.ok) this._applyDictionary(languageId, result.data);
|
|
1093
|
-
else if (!background && result.error !== void 0 && languageId === this._currentLanguageId()) {
|
|
1094
|
-
this._loadingError.set(result.error);
|
|
1095
|
-
this._loadingState.set("error");
|
|
1096
|
-
}
|
|
1097
|
-
return {
|
|
1098
|
-
data: result.data,
|
|
1099
|
-
ok: result.ok
|
|
1100
|
-
};
|
|
1101
|
-
} finally {
|
|
1102
|
-
if (!background) {
|
|
1103
|
-
this._loadingCount = Math.max(0, this._loadingCount - 1);
|
|
1104
|
-
if (this._loadingCount === 0 && this._loadingState() === "loading") this._loadingState.set("idle");
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
_applyDictionary(languageId, data) {
|
|
1109
|
-
if (languageId === this._currentLanguageId()) {
|
|
1110
|
-
this._loadedTranslations.set(data);
|
|
1111
|
-
this._loadingError.set(null);
|
|
1112
|
-
if (this._loadingCount === 0 && this._loadingState() === "error") this._loadingState.set("idle");
|
|
1113
|
-
}
|
|
1114
|
-
if (languageId === this._fallbackId) this._fallbackDict.set(data);
|
|
1115
|
-
}
|
|
1116
|
-
_fetchDictionary(languageId, generation) {
|
|
1117
|
-
let task;
|
|
1118
|
-
task = (async () => {
|
|
1119
|
-
const stale = () => this._generationOf(languageId) !== generation;
|
|
1120
|
-
try {
|
|
1121
|
-
if (!this._resolvedLoader) this._resolvedLoader = await this._getLoader(this.loaderContext);
|
|
1122
|
-
const translations = await this._resolvedLoader.load(languageId, this.loaderContext);
|
|
1123
|
-
if (stale()) return {
|
|
1124
|
-
data: {},
|
|
1125
|
-
ok: false
|
|
1126
|
-
};
|
|
1127
|
-
this._translationsCache.set(languageId, translations);
|
|
1128
|
-
this.writeTransfer(languageId, translations);
|
|
1129
|
-
return {
|
|
1130
|
-
data: translations,
|
|
1131
|
-
ok: true
|
|
1132
|
-
};
|
|
1133
|
-
} catch (error) {
|
|
1134
|
-
if (stale()) return {
|
|
1135
|
-
data: {},
|
|
1136
|
-
ok: false
|
|
1137
|
-
};
|
|
1138
|
-
this._config.onLoadError?.(languageId, error);
|
|
1139
|
-
return {
|
|
1140
|
-
data: {},
|
|
1141
|
-
ok: false,
|
|
1142
|
-
error
|
|
1143
|
-
};
|
|
1144
|
-
} finally {
|
|
1145
|
-
if (this._inflight.get(languageId) === task) this._inflight.delete(languageId);
|
|
1146
|
-
}
|
|
1147
|
-
})();
|
|
1148
|
-
return task;
|
|
1149
|
-
}
|
|
1150
|
-
_readStored() {
|
|
1151
|
-
if (this._storageStrategy === "cookie") {
|
|
1152
|
-
const header = this._request?.headers.get("cookie") ?? null;
|
|
1153
|
-
if (header !== null) return parseCookie(header, this._storageKey);
|
|
1154
|
-
try {
|
|
1155
|
-
return parseCookie(this._doc.cookie, this._storageKey);
|
|
1156
|
-
} catch {
|
|
1157
|
-
return null;
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
|
-
return safeGetItem(this._storageKey, this._storageStrategy);
|
|
1161
|
-
}
|
|
1162
|
-
_writeStored(id) {
|
|
1163
|
-
if (this._storageStrategy === "cookie") {
|
|
1164
|
-
if (this._request) return;
|
|
1165
|
-
try {
|
|
1166
|
-
const location = this._doc?.location;
|
|
1167
|
-
this._doc.cookie = serializeCookie(this._storageKey, id, { secure: shouldSecureCookie(location) });
|
|
1168
|
-
} catch {}
|
|
1169
|
-
return;
|
|
1170
|
-
}
|
|
1171
|
-
safeSetItem(this._storageKey, id, this._storageStrategy);
|
|
1172
|
-
}
|
|
1173
|
-
transferKey(languageId) {
|
|
1174
|
-
return makeStateKey(`rivinia.i18n.dict.${languageId}`);
|
|
1175
|
-
}
|
|
1176
|
-
readTransfer(languageId) {
|
|
1177
|
-
const transfer = this._transfer;
|
|
1178
|
-
if (!transfer) return null;
|
|
1179
|
-
const key = this.transferKey(languageId);
|
|
1180
|
-
if (!transfer.hasKey(key)) return null;
|
|
1181
|
-
const dict = transfer.get(key, null);
|
|
1182
|
-
transfer.remove(key);
|
|
1183
|
-
return dict;
|
|
1184
|
-
}
|
|
1185
|
-
writeTransfer(languageId, dict) {
|
|
1186
|
-
if (!this._request) return;
|
|
1187
|
-
this._transfer?.set(this.transferKey(languageId), dict);
|
|
1319
|
+
this._missingKeyReporter.reportParam(key, param);
|
|
1188
1320
|
}
|
|
1189
1321
|
_resolveKey(obj, path) {
|
|
1190
1322
|
const value = Object.prototype.hasOwnProperty.call(obj, path) ? obj[path] : path.split(".").reduce((acc, segment) => acc && typeof acc === "object" ? acc[segment] : void 0, obj);
|
|
1191
1323
|
if (typeof value === "string") return value;
|
|
1192
1324
|
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
1193
1325
|
}
|
|
1194
|
-
_resolveInitialLanguage() {
|
|
1195
|
-
const stored = this._readStored();
|
|
1196
|
-
if (stored && this._config.languages.some((l) => l.id === stored)) return stored;
|
|
1197
|
-
if (this._config.detectBrowser) {
|
|
1198
|
-
const detected = detectBrowserLanguage(this._config.languages.map((l) => l.id), browserLanguages());
|
|
1199
|
-
if (detected) return detected;
|
|
1200
|
-
}
|
|
1201
|
-
return this._config.defaultLanguage;
|
|
1202
|
-
}
|
|
1203
1326
|
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
1204
1327
|
minVersion: "12.0.0",
|
|
1205
1328
|
version: "22.2.3",
|
|
@@ -1230,6 +1353,22 @@ function normalizeKeyArgs(second, third) {
|
|
|
1230
1353
|
params: third
|
|
1231
1354
|
};
|
|
1232
1355
|
}
|
|
1356
|
+
function resolveDefaultLanguage(config) {
|
|
1357
|
+
const languages = config.languages;
|
|
1358
|
+
if (!Array.isArray(languages) || languages.length === 0) throw new Error("[rivinia/i18n] At least one language must be provided in the languages array.");
|
|
1359
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1360
|
+
languages.forEach((language, index) => {
|
|
1361
|
+
const id = language?.id;
|
|
1362
|
+
if (typeof id !== "string" || id.trim().length === 0) throw new Error(`[rivinia/i18n] language[${index}] needs a non-empty \`id\`.`);
|
|
1363
|
+
if (seen.has(id)) throw new Error(`[rivinia/i18n] Duplicate language id "${id}" in the languages array.`);
|
|
1364
|
+
seen.add(id);
|
|
1365
|
+
});
|
|
1366
|
+
const requested = config.defaultLanguage;
|
|
1367
|
+
if (requested === void 0) return languages[0].id;
|
|
1368
|
+
if (typeof requested !== "string" || requested.trim().length === 0) throw new Error(`[rivinia/i18n] defaultLanguage must be a non-empty string (received ${JSON.stringify(requested) ?? String(requested)}).`);
|
|
1369
|
+
if (!languages.some((language) => language.id === requested)) throw new Error(`[rivinia/i18n] defaultLanguage "${requested}" must be one of the configured language IDs.`);
|
|
1370
|
+
return requested;
|
|
1371
|
+
}
|
|
1233
1372
|
function provideRiviniaI18n(config) {
|
|
1234
1373
|
return makeEnvironmentProviders([{
|
|
1235
1374
|
provide: RIVINIA_I18N_CONFIG,
|
|
@@ -1248,7 +1387,7 @@ function defineRiviniaI18n(config) {
|
|
|
1248
1387
|
inject: () => injectRiviniaI18n()
|
|
1249
1388
|
};
|
|
1250
1389
|
}
|
|
1251
|
-
var
|
|
1390
|
+
var RvTranslatePipe = class RvTranslatePipe {
|
|
1252
1391
|
_i18n = inject(RiviniaI18n);
|
|
1253
1392
|
transform(keyOrTranslations, defaultValueOrParams, maybeParams) {
|
|
1254
1393
|
return this._i18n.translate(keyOrTranslations, defaultValueOrParams, maybeParams);
|
|
@@ -1257,7 +1396,7 @@ var RTranslatePipe = class RTranslatePipe {
|
|
|
1257
1396
|
minVersion: "12.0.0",
|
|
1258
1397
|
version: "22.2.3",
|
|
1259
1398
|
ngImport: i0,
|
|
1260
|
-
type:
|
|
1399
|
+
type: RvTranslatePipe,
|
|
1261
1400
|
deps: [],
|
|
1262
1401
|
target: i0.ɵɵFactoryTarget.Pipe
|
|
1263
1402
|
});
|
|
@@ -1265,9 +1404,9 @@ var RTranslatePipe = class RTranslatePipe {
|
|
|
1265
1404
|
minVersion: "14.0.0",
|
|
1266
1405
|
version: "22.2.3",
|
|
1267
1406
|
ngImport: i0,
|
|
1268
|
-
type:
|
|
1407
|
+
type: RvTranslatePipe,
|
|
1269
1408
|
isStandalone: true,
|
|
1270
|
-
name: "
|
|
1409
|
+
name: "rvTranslate",
|
|
1271
1410
|
pure: false
|
|
1272
1411
|
});
|
|
1273
1412
|
};
|
|
@@ -1275,15 +1414,15 @@ i0.ɵɵngDeclareClassMetadata({
|
|
|
1275
1414
|
minVersion: "12.0.0",
|
|
1276
1415
|
version: "22.2.3",
|
|
1277
1416
|
ngImport: i0,
|
|
1278
|
-
type:
|
|
1417
|
+
type: RvTranslatePipe,
|
|
1279
1418
|
decorators: [{
|
|
1280
1419
|
type: Pipe,
|
|
1281
1420
|
args: [{
|
|
1282
|
-
name: "
|
|
1421
|
+
name: "rvTranslate",
|
|
1283
1422
|
pure: false
|
|
1284
1423
|
}]
|
|
1285
1424
|
}]
|
|
1286
1425
|
});
|
|
1287
|
-
export {
|
|
1426
|
+
export { RiviniaI18n, RiviniaLoadError, RvTranslatePipe, ViewTransitionService, browserLanguages, customLoader, defineRiviniaI18n, detectBrowserLanguage, formatIcu, httpLoader, injectRiviniaI18n, jsonLoader, parseCookie, provideRiviniaI18n, remoteLoader, serializeCookie };
|
|
1288
1427
|
|
|
1289
1428
|
//# sourceMappingURL=rivinia-i18n.mjs.map
|