Files
jellyfin-audio-player/src/localisation/index.ts

61 lines
2.0 KiB
TypeScript
Raw Normal View History

2024-02-08 22:11:43 +01:00
import { I18n } from 'i18n-js';
import { findBestLanguageTag } from 'react-native-localize';
2020-11-02 22:43:11 +01:00
import { LocaleKeys } from './types';
2024-02-08 22:11:43 +01:00
const i18n = new I18n();
2020-11-02 22:43:11 +01:00
// Lazy loaders for locale
const localeGetters: Record<string, () => object> = {
de: () => require('./lang/de/locale.json'),
en: () => require('./lang/en/locale.json'),
2023-06-19 22:30:02 +02:00
es: () => require('./lang/es/locale.json'),
fr: () => require('./lang/fr/locale.json'),
2023-06-19 22:43:31 +02:00
it: () => require('./lang/it/locale.json'),
2023-06-19 22:30:02 +02:00
ja: () => require('./lang/ja/locale.json'),
nb_NO: () => require('./lang/nb_NO/locale.json'),
2020-11-03 23:10:21 +01:00
nl: () => require('./lang/nl/locale.json'),
2023-06-19 22:30:02 +02:00
pl: () => require('./lang/pl/locale.json'),
ru: () => require('./lang/ru/locale.json'),
sv: () => require('./lang/sv/locale.json'),
2023-06-19 22:30:02 +02:00
uk: () => require('./lang/uk/locale.json'),
2021-03-14 21:07:50 +08:00
zh: () => require('./lang/zh/locale.json'),
2020-11-02 22:43:11 +01:00
};
// Have RNLocalize pick the best locale from the languages on offer
2024-02-08 22:11:43 +01:00
let locale = findBestLanguageTag(Object.keys(localeGetters));
2020-11-02 22:43:11 +01:00
// Check if the locale is correctly picked
if (!locale || !locale.languageTag) {
// Some users might not list English as a fallback language, and hence might not
// be assigned a locale. In this case, we'll just default to English.
locale = {
languageTag: 'en',
isRTL: false,
};
2020-11-02 22:43:11 +01:00
}
// Set the key-value pairs for the different languages you want to support.
i18n.translations = {
en: localeGetters.en(),
};
// If the locale is not english, we add it to the translations key s well
if (locale.languageTag !== 'en') {
i18n.translations[locale.languageTag] = localeGetters[locale.languageTag as string]();
}
// Set the locale once at the beginning of your app.
i18n.locale = locale.languageTag;
// Fallback to the default language for missing translation strings
2024-02-08 22:11:43 +01:00
i18n.enableFallback = true;
2020-11-02 22:43:11 +01:00
/**
* An i18n Typescript helper with autocomplete for the key argument
* @param key string
*/
export function t(key: LocaleKeys) {
return i18n.t(key);
}
export default i18n;