1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 17:53:39 +00:00
Files
browser/libs/common/src/platform/services/i18n.service.ts
Matt Gibson 25f89e2a1c [PM-6769] [SM-1158] Fix Translation pipe issues on main (#8319)
* Require init in i18n service.

this is needed to load translations and set translation locale

* No longer need to cast i18n

* Expose user preferred locale in i18nService

This is for correctly displaying `default` when no locale has been set in preferences components. The `locale$` observable should always resolve to the currently locale currently being translated to.
2024-03-13 11:35:46 -04:00

39 lines
1.4 KiB
TypeScript

import { Observable, firstValueFrom, map } from "rxjs";
import { I18nService as I18nServiceAbstraction } from "../abstractions/i18n.service";
import { GlobalState, GlobalStateProvider, KeyDefinition, TRANSLATION_DISK } from "../state";
import { TranslationService } from "./translation.service";
const LOCALE_KEY = new KeyDefinition<string>(TRANSLATION_DISK, "locale", {
deserializer: (value) => value,
});
export class I18nService extends TranslationService implements I18nServiceAbstraction {
translationLocale: string;
protected translationLocaleState: GlobalState<string>;
userSetLocale$: Observable<string | undefined>;
locale$: Observable<string>;
constructor(
protected systemLanguage: string,
protected localesDirectory: string,
protected getLocalesJson: (formattedLocale: string) => Promise<any>,
globalStateProvider: GlobalStateProvider,
) {
super(systemLanguage, localesDirectory, getLocalesJson);
this.translationLocaleState = globalStateProvider.get(LOCALE_KEY);
this.userSetLocale$ = this.translationLocaleState.state$;
this.locale$ = this.userSetLocale$.pipe(map((locale) => locale ?? this.translationLocale));
}
async setLocale(locale: string): Promise<void> {
await this.translationLocaleState.update(() => locale);
}
override async init() {
const storedLocale = await firstValueFrom(this.translationLocaleState.state$);
await super.init(storedLocale);
}
}