2023-02-13 20:20:08 +03:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2023-02-13 20:20:08 +03:30
|
|
|
|
2026-01-10 19:45:02 +01:00
|
|
|
import { i18n } from '@lingui/core';
|
2026-01-23 02:15:26 +01:00
|
|
|
import { detectLocale, SELECTED_LANGUAGE_CODE_KEY } from '@/lib/ISOLanguageUtil.ts';
|
|
|
|
|
import { AppStorage } from '@/lib/storage/AppStorage.ts';
|
2024-04-14 17:32:25 +02:00
|
|
|
|
2026-01-30 21:46:20 +01:00
|
|
|
/**
|
|
|
|
|
* Keys have to match {@link IsoLanguages} codes, they're used for showing the language name in the dropdown in the {@link Settings}.<br/>
|
|
|
|
|
* In case there is no language code for the key in {@link IsoLanguages}, the corresponding language has to be added
|
|
|
|
|
*
|
|
|
|
|
* **IMPORTANT:**
|
|
|
|
|
*
|
|
|
|
|
* This is generated by the `i18n:gen-resources` script
|
|
|
|
|
*/
|
2026-01-30 21:29:43 +01:00
|
|
|
export const i18nResources = [
|
|
|
|
|
'de',
|
|
|
|
|
'en',
|
|
|
|
|
'es',
|
|
|
|
|
'fa',
|
|
|
|
|
'fr',
|
|
|
|
|
'hu',
|
|
|
|
|
'it',
|
|
|
|
|
'ja',
|
|
|
|
|
'ko',
|
|
|
|
|
'pl',
|
|
|
|
|
'pt',
|
|
|
|
|
'ru',
|
|
|
|
|
'ta',
|
|
|
|
|
'tr',
|
|
|
|
|
'vi',
|
2026-01-30 21:57:12 +01:00
|
|
|
'zh-Hans',
|
|
|
|
|
'zh-Hant',
|
2026-01-30 21:29:43 +01:00
|
|
|
] as const;
|
2024-04-24 23:56:06 +02:00
|
|
|
|
|
|
|
|
export type I18nResourceCode = (typeof i18nResources)[number];
|
2023-02-13 20:20:08 +03:30
|
|
|
|
2026-01-30 21:29:43 +01:00
|
|
|
const DEFAULT_LOCALE = 'en';
|
2026-01-10 19:45:02 +01:00
|
|
|
|
|
|
|
|
export async function loadCatalog(locale: string): Promise<void> {
|
|
|
|
|
const localeToLoad = i18nResources.includes(locale as (typeof i18nResources)[number]) ? locale : DEFAULT_LOCALE;
|
|
|
|
|
|
|
|
|
|
const { messages } = await import(`./locales/${localeToLoad}.po`);
|
|
|
|
|
i18n.loadAndActivate({ locale: localeToLoad, messages });
|
2026-01-23 02:15:26 +01:00
|
|
|
|
|
|
|
|
AppStorage.local.setItem(SELECTED_LANGUAGE_CODE_KEY, localeToLoad);
|
2026-01-10 19:45:02 +01:00
|
|
|
}
|
2024-04-14 17:32:25 +02:00
|
|
|
|
2026-01-10 19:45:02 +01:00
|
|
|
export async function initializeLocalization(): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
await loadCatalog(detectLocale());
|
|
|
|
|
} catch (e) {
|
|
|
|
|
await loadCatalog(DEFAULT_LOCALE);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-14 17:32:25 +02:00
|
|
|
|
2026-01-10 19:45:02 +01:00
|
|
|
export { i18n };
|