Open History to see the last commit
1import { getTranslationsSnapshot } from "gt-react";
2
3import {
4 getClientSettings,
5 subscribeClientSettings,
6 updateClientSettings,
7} from "../hooks/useSettings";
8import { initializeI18n } from "./initializeI18n";
9import { DEFAULT_LOCALE, resolveWebLocaleSelection } from "./locales";
10import { setActiveLocale } from "./messages";
11
12type LocaleTranslations = Awaited<ReturnType<typeof getTranslationsSnapshot>>;
13
14export interface I18nSnapshot {
15 readonly locale: string;
16 readonly formattingLocale: string | undefined;
17 readonly translations: LocaleTranslations;
18}
19
20const loadedTranslations: LocaleTranslations = { [DEFAULT_LOCALE]: {} };
21const pendingLoads = new Map<string, Promise<void>>();
22const listeners = new Set<() => void>();
23let snapshot: I18nSnapshot = {
24 locale: DEFAULT_LOCALE,
25 formattingLocale: undefined,
26 translations: { ...loadedTranslations },
27};
28let requestedLocale: ReturnType<typeof resolveWebLocaleSelection> = {
29 catalogLocale: DEFAULT_LOCALE,
30 formattingLocale: undefined,
31};
32
33function loadLocale(locale: string): Promise<void> {
34 if (locale in loadedTranslations) return Promise.resolve();
35 let load = pendingLoads.get(locale);
36 if (!load) {
37 // GT resolves a failed load to an empty catalog. Leave it uncached: English renders now
38 // and the next request retries.
39 load = getTranslationsSnapshot(locale).then((loaded) => {
40 pendingLoads.delete(locale);
41 const translations = loaded[locale];
42 if (translations && Object.keys(translations).length > 0) {
43 loadedTranslations[locale] = translations;
44 }
45 });
46 pendingLoads.set(locale, load);
47 }
48 return load;
49}
50
51function activateLocale(locale: string): void {
52 if (requestedLocale.catalogLocale !== locale) return;
53 const { formattingLocale } = requestedLocale;
54 if (
55 snapshot.locale === locale &&
56 snapshot.formattingLocale === formattingLocale &&
57 snapshot.translations[locale] === loadedTranslations[locale]
58 ) {
59 return;
60 }
61 snapshot = { locale, formattingLocale, translations: { ...loadedTranslations } };
62 setActiveLocale(locale);
63 document.documentElement.lang = locale;
64 for (const listener of listeners) listener();
65}
66
67function applyLanguageSetting(): void {
68 // Not desktopBridge.getSystemLocale(): that is the regional/time locale, not the UI language.
69 requestedLocale = resolveWebLocaleSelection(getClientSettings().language, navigator.languages);
70 void whenLocaleReady();
71}
72
73/** GT's own locale setters write the saved setting, which stays the only source of truth. */
74export function persistGTLocale(locale: string): void {
75 void updateClientSettings({ language: locale });
76}
77
78export function startI18n(): void {
79 initializeI18n(persistGTLocale);
80 subscribeClientSettings(applyLanguageSetting);
81 applyLanguageSetting();
82}
83
84/** Resolves once the requested locale's translations are ready; immediate for English. */
85export function whenLocaleReady(): Promise<void> {
86 const locale = requestedLocale.catalogLocale;
87 return loadLocale(locale).then(() => activateLocale(locale));
88}
89
90export function subscribeI18n(listener: () => void): () => void {
91 listeners.add(listener);
92 return () => {
93 listeners.delete(listener);
94 };
95}
96
97export function getI18nSnapshot(): I18nSnapshot {
98 return snapshot;
99}
100
101export function getFormattingLocale(): string | undefined {
102 return snapshot.formattingLocale;
103}