@taiga-ui/i18n
Version:
A package with tools for Taiga UI library i18n
71 lines (65 loc) • 3.12 kB
JavaScript
import * as i0 from '@angular/core';
import { computed, inject, Injectable } from '@angular/core';
import { TUI_LANGUAGE, TUI_LANGUAGE_LOADER, TUI_DEFAULT_LANGUAGE, TUI_LANGUAGE_STORAGE_KEY } from '@taiga-ui/i18n/tokens';
import { toSignal } from '@angular/core/rxjs-interop';
import { WA_LOCAL_STORAGE } from '@ng-web-apis/common';
import { from, map, of, switchAll, BehaviorSubject } from 'rxjs';
function tuiExtractI18n(key) {
return (language = inject(TUI_LANGUAGE)) => computed(() => language()[key]);
}
// CommonJS `module.exports` is wrapped as `default` in ESModule.
async function normalizeCommonJSImport(importPromise) {
return importPromise.then((m) => m.default || m);
}
function tuiLoadLanguage(language, loader) {
return from(normalizeCommonJSImport(loader(language))).pipe(map((module) => module?.[`TUI_${language.toUpperCase()}_LANGUAGE`]));
}
function tuiAsyncLoadLanguage(language, loader, fallback) {
return language && loader ? tuiLoadLanguage(language, loader) : of(fallback);
}
function tuiLanguageSwitcher(loader) {
return [
{
provide: TUI_LANGUAGE_LOADER,
useFactory: () => loader,
},
{
provide: TUI_LANGUAGE,
useFactory: () => toSignal(inject(TuiLanguageSwitcherService).pipe(switchAll()), {
initialValue: inject(TUI_DEFAULT_LANGUAGE),
}),
},
];
}
class TuiLanguageSwitcherService extends BehaviorSubject {
constructor() {
super(tuiAsyncLoadLanguage(inject(WA_LOCAL_STORAGE)?.getItem(inject(TUI_LANGUAGE_STORAGE_KEY)) ||
null, inject(TUI_LANGUAGE_LOADER, { optional: true }), inject(TUI_DEFAULT_LANGUAGE)));
this.fallback = inject(TUI_DEFAULT_LANGUAGE);
this.key = inject(TUI_LANGUAGE_STORAGE_KEY);
this.storage = inject(WA_LOCAL_STORAGE);
this.loader = inject(TUI_LANGUAGE_LOADER, { optional: true });
}
get language() {
return this.storage?.getItem(this.key) || this.fallback.name;
}
setLanguage(language) {
this.storage?.setItem(this.key, language);
this.next(tuiAsyncLoadLanguage(language, this.loader, this.fallback));
}
clear() {
this.storage?.removeItem(this.key);
this.next(of(this.fallback));
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.2.21", ngImport: i0, type: TuiLanguageSwitcherService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.2.21", ngImport: i0, type: TuiLanguageSwitcherService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.2.21", ngImport: i0, type: TuiLanguageSwitcherService, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], ctorParameters: () => [] });
/**
* Generated bundle index. Do not edit.
*/
export { TuiLanguageSwitcherService, tuiAsyncLoadLanguage, tuiExtractI18n, tuiLanguageSwitcher, tuiLoadLanguage };
//# sourceMappingURL=taiga-ui-i18n-utils.mjs.map