lemmy-ui/src/shared/i18next.ts

108 lines
3.9 KiB
TypeScript
Raw Normal View History

import i18next, { i18nTyped, Resource } from "i18next";
import { UserService } from "./services";
import { ar } from "./translations/ar";
import { bg } from "./translations/bg";
import { ca } from "./translations/ca";
2021-09-28 14:56:38 +00:00
import { cs } from "./translations/cs";
import { da } from "./translations/da";
import { de } from "./translations/de";
2021-02-22 02:39:04 +00:00
import { el } from "./translations/el";
import { en } from "./translations/en";
2021-02-22 02:39:04 +00:00
import { eo } from "./translations/eo";
import { es } from "./translations/es";
import { eu } from "./translations/eu";
2021-02-22 02:39:04 +00:00
import { fa } from "./translations/fa";
import { fi } from "./translations/fi";
import { fr } from "./translations/fr";
import { ga } from "./translations/ga";
import { gl } from "./translations/gl";
import { hr } from "./translations/hr";
import { id } from "./translations/id";
import { it } from "./translations/it";
2021-02-22 02:39:04 +00:00
import { ja } from "./translations/ja";
import { ko } from "./translations/ko";
import { nl } from "./translations/nl";
import { oc } from "./translations/oc";
import { pl } from "./translations/pl";
2021-09-03 03:12:08 +00:00
import { pt } from "./translations/pt";
import { pt_BR } from "./translations/pt_BR";
import { ru } from "./translations/ru";
import { sv } from "./translations/sv";
import { vi } from "./translations/vi";
import { zh } from "./translations/zh";
import { zh_Hant } from "./translations/zh_Hant";
import { isBrowser } from "./utils";
export const languages = [
{ resource: ar, code: "ar", name: "العربية" },
{ resource: bg, code: "bg", name: "Български" },
{ resource: ca, code: "ca", name: "Català" },
{ resource: cs, code: "cs", name: "Česky" },
{ resource: da, code: "da", name: "Dansk" },
{ resource: de, code: "de", name: "Deutsch" },
{ resource: el, code: "el", name: "Ελληνικά" },
{ resource: en, code: "en", name: "English" },
{ resource: eo, code: "eo", name: "Esperanto" },
{ resource: es, code: "es", name: "Español" },
{ resource: eu, code: "eu", name: "Euskara" },
{ resource: fa, code: "fa", name: "فارسی" },
{ resource: fi, code: "fi", name: "Suomi" },
{ resource: fr, code: "fr", name: "Français" },
{ resource: ga, code: "ga", name: "Gaeilge" },
{ resource: gl, code: "gl", name: "Galego" },
{ resource: hr, code: "hr", name: "Hrvatski" },
{ resource: id, code: "id", name: "Bahasa Indonesia" },
{ resource: it, code: "it", name: "Italiano" },
{ resource: ja, code: "ja", name: "日本語" },
{ resource: ko, code: "ko", name: "한국어" },
{ resource: nl, code: "nl", name: "Nederlands" },
{ resource: oc, code: "oc", name: "Occitan" },
{ resource: pl, code: "pl", name: "Polski" },
{ resource: pt, code: "pt", name: "Português" },
{ resource: pt_BR, code: "pt_BR", name: "Português (Brasil)" },
{ resource: ru, code: "ru", name: "Русский" },
{ resource: sv, code: "sv", name: "Svenska" },
{ resource: vi, code: "vi", name: "Tiếng Việt" },
2022-05-28 19:17:55 +00:00
{ resource: zh, code: "zh", name: "中文 (简体)" },
{ resource: zh_Hant, code: "zh-TW", name: "中文 (繁體)" },
];
const resources: Resource = {};
languages.forEach(l => (resources[l.code] = l.resource));
2020-10-24 17:12:13 +00:00
function format(value: any, format: any): any {
2021-02-22 02:39:04 +00:00
return format === "uppercase" ? value.toUpperCase() : value;
}
class LanguageDetector {
static readonly type = "languageDetector";
detect() {
const langs: string[] = [];
const myLang =
UserService.Instance.myUserInfo?.local_user_view.local_user
.interface_language ?? "browser";
if (myLang !== "browser") langs.push(myLang);
if (isBrowser()) langs.push(...navigator.languages);
return langs;
}
}
i18next.use(LanguageDetector).init({
debug: false,
2021-10-19 00:42:24 +00:00
compatibilityJSON: "v3",
supportedLngs: languages.map(l => l.code),
nonExplicitSupportedLngs: true,
// load: 'languageOnly',
// initImmediate: false,
2021-02-22 02:39:04 +00:00
fallbackLng: "en",
resources,
interpolation: { format },
});
2021-03-01 20:33:57 +00:00
export const i18n = i18next as i18nTyped;