Prevent language switch on initial load

The change of bb36a46a24 causes a "language blink" on the initial load.
This happens due to having to wait till the selected language has been loaded from the server. Until this request is resolved, lingui initializes with the detected language, which might not match the selected one. This then causes the language to potentially be changed shortly after the initial load to a different language.
This commit is contained in:
schroda
2026-01-23 02:15:26 +01:00
parent bb36a46a24
commit 12cac252de
2 changed files with 12 additions and 2 deletions

View File

@@ -7,8 +7,9 @@
*/
import { i18n } from '@lingui/core';
import { detectLocale } from '@/lib/ISOLanguageUtil.ts';
import { detectLocale, SELECTED_LANGUAGE_CODE_KEY } from '@/lib/ISOLanguageUtil.ts';
import localesConfig from '@/i18n/locales.json';
import { AppStorage } from '@/lib/storage/AppStorage.ts';
export const i18nResources = localesConfig.locales;
@@ -21,6 +22,8 @@ export async function loadCatalog(locale: string): Promise<void> {
const { messages } = await import(`./locales/${localeToLoad}.po`);
i18n.loadAndActivate({ locale: localeToLoad, messages });
AppStorage.local.setItem(SELECTED_LANGUAGE_CODE_KEY, localeToLoad);
}
export async function initializeLocalization(): Promise<void> {

View File

@@ -8,11 +8,14 @@
import { ISOLanguage, IsoLanguages } from '@/base/IsoLanguages.ts';
import { i18nResources } from '@/i18n';
import { AppStorage } from '@/lib/storage/AppStorage.ts';
export type LanguageObject = ISOLanguage & { orgCode: string; isoCode: string };
export const DEFAULT_LANGUAGE = 'en';
export const SELECTED_LANGUAGE_CODE_KEY = 'i18n:selected-language';
// source: https://github.com/i18next/i18next/blob/485b4ec8183952b3de8fe5e79dff6467c3afd9d3/src/i18next.js#L561
const RTL_LANGUAGES = [
'ar',
@@ -140,7 +143,11 @@ export function getLanguageReadingDirection(code: string): 'ltr' | 'rtl' {
}
export function detectLocale(): string {
const normalizedLanguageCodes = getPreferredISOLanguageCodes()
const storedLanguage = AppStorage.local.getItem(SELECTED_LANGUAGE_CODE_KEY);
const preferredLanguages = getPreferredISOLanguageCodes();
const languageCodes = [storedLanguage, ...preferredLanguages];
const normalizedLanguageCodes = languageCodes
.filter((code) => code !== null)
.filter((code) => getISOLanguage(code))
.filter((code) => i18nResources.includes(code as (typeof i18nResources)[number]));