]> Untitled Git - lemmy-ui.git/blob - src/shared/i18next.ts
Merge branch 'main' into breakout-role-utils
[lemmy-ui.git] / src / shared / i18next.ts
1 import i18next, { i18nTyped, Resource } from "i18next";
2 import { UserService } from "./services";
3 import { ar } from "./translations/ar";
4 import { bg } from "./translations/bg";
5 import { ca } from "./translations/ca";
6 import { cs } from "./translations/cs";
7 import { da } from "./translations/da";
8 import { de } from "./translations/de";
9 import { el } from "./translations/el";
10 import { en } from "./translations/en";
11 import { eo } from "./translations/eo";
12 import { es } from "./translations/es";
13 import { eu } from "./translations/eu";
14 import { fa } from "./translations/fa";
15 import { fi } from "./translations/fi";
16 import { fr } from "./translations/fr";
17 import { ga } from "./translations/ga";
18 import { gl } from "./translations/gl";
19 import { hr } from "./translations/hr";
20 import { id } from "./translations/id";
21 import { it } from "./translations/it";
22 import { ja } from "./translations/ja";
23 import { ko } from "./translations/ko";
24 import { nl } from "./translations/nl";
25 import { oc } from "./translations/oc";
26 import { pl } from "./translations/pl";
27 import { pt } from "./translations/pt";
28 import { pt_BR } from "./translations/pt_BR";
29 import { ru } from "./translations/ru";
30 import { sv } from "./translations/sv";
31 import { vi } from "./translations/vi";
32 import { zh } from "./translations/zh";
33 import { zh_Hant } from "./translations/zh_Hant";
34 import { isBrowser } from "./utils";
35
36 export const languages = [
37   { resource: ar, code: "ar", name: "العربية" },
38   { resource: bg, code: "bg", name: "Български" },
39   { resource: ca, code: "ca", name: "Català" },
40   { resource: cs, code: "cs", name: "Česky" },
41   { resource: da, code: "da", name: "Dansk" },
42   { resource: de, code: "de", name: "Deutsch" },
43   { resource: el, code: "el", name: "Ελληνικά" },
44   { resource: en, code: "en", name: "English" },
45   { resource: eo, code: "eo", name: "Esperanto" },
46   { resource: es, code: "es", name: "Español" },
47   { resource: eu, code: "eu", name: "Euskara" },
48   { resource: fa, code: "fa", name: "فارسی" },
49   { resource: fi, code: "fi", name: "Suomi" },
50   { resource: fr, code: "fr", name: "Français" },
51   { resource: ga, code: "ga", name: "Gaeilge" },
52   { resource: gl, code: "gl", name: "Galego" },
53   { resource: hr, code: "hr", name: "Hrvatski" },
54   { resource: id, code: "id", name: "Bahasa Indonesia" },
55   { resource: it, code: "it", name: "Italiano" },
56   { resource: ja, code: "ja", name: "日本語" },
57   { resource: ko, code: "ko", name: "한국어" },
58   { resource: nl, code: "nl", name: "Nederlands" },
59   { resource: oc, code: "oc", name: "Occitan" },
60   { resource: pl, code: "pl", name: "Polski" },
61   { resource: pt, code: "pt", name: "Português" },
62   { resource: pt_BR, code: "pt_BR", name: "Português (Brasil)" },
63   { resource: ru, code: "ru", name: "Русский" },
64   { resource: sv, code: "sv", name: "Svenska" },
65   { resource: vi, code: "vi", name: "Tiếng Việt" },
66   { resource: zh, code: "zh", name: "中文 (简体)" },
67   { resource: zh_Hant, code: "zh-TW", name: "中文 (繁體)" },
68 ];
69
70 const resources: Resource = {};
71 languages.forEach(l => (resources[l.code] = l.resource));
72
73 function format(value: any, format: any): any {
74   return format === "uppercase" ? value.toUpperCase() : value;
75 }
76
77 class LanguageDetector {
78   static readonly type = "languageDetector";
79
80   detect() {
81     const langs: string[] = [];
82
83     const myLang =
84       UserService.Instance.myUserInfo?.local_user_view.local_user
85         .interface_language ?? "browser";
86
87     if (myLang !== "browser") langs.push(myLang);
88
89     if (isBrowser()) langs.push(...navigator.languages);
90
91     return langs;
92   }
93 }
94
95 i18next.use(LanguageDetector).init({
96   debug: false,
97   compatibilityJSON: "v3",
98   supportedLngs: languages.map(l => l.code),
99   nonExplicitSupportedLngs: true,
100   // load: 'languageOnly',
101   // initImmediate: false,
102   fallbackLng: "en",
103   resources,
104   interpolation: { format },
105 });
106
107 export const i18n = i18next as i18nTyped;