Files
suwayomi-material-you-webui/src/i18n/index.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

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
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2023-02-13 20:20:08 +03:30
import { i18n } from '@lingui/core';
import { detectLocale, SELECTED_LANGUAGE_CODE_KEY } from '@/lib/ISOLanguageUtil.ts';
import { AppStorage } from '@/lib/storage/AppStorage.ts';
/**
* 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 = [
2026-03-20 03:21:10 +01:00
'ar',
2026-01-30 21:29:43 +01:00
'de',
2026-06-07 17:01:55 +00:00
'el',
2026-01-30 21:29:43 +01:00
'en',
'es',
'fr',
2026-07-03 22:12:43 +00:00
'he',
2026-01-30 21:29:43 +01:00
'hu',
2026-04-17 00:52:09 +02:00
'id',
2026-01-30 21:29:43 +01:00
'pl',
2026-05-13 22:10:30 +00:00
'pt',
2026-01-30 21:29:43 +01:00
'ru',
'vi',
'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';
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 });
AppStorage.local.setItem(SELECTED_LANGUAGE_CODE_KEY, localeToLoad);
}
export async function initializeLocalization(): Promise<void> {
try {
await loadCatalog(detectLocale());
} catch (e) {
await loadCatalog(DEFAULT_LOCALE);
}
}
export { i18n };