]> Untitled Git - lemmy-ui.git/blob - src/shared/services/I18NextService.ts
Fix I18 next circular reference
[lemmy-ui.git] / src / shared / services / I18NextService.ts
1 import { isBrowser } from "@utils/browser";
2 import i18next, { Resource } from "i18next";
3 import { UserService } from "../services";
4 import { ar } from "../translations/ar";
5 import { bg } from "../translations/bg";
6 import { ca } from "../translations/ca";
7 import { cs } from "../translations/cs";
8 import { da } from "../translations/da";
9 import { de } from "../translations/de";
10 import { el } from "../translations/el";
11 import { en } from "../translations/en";
12 import { eo } from "../translations/eo";
13 import { es } from "../translations/es";
14 import { eu } from "../translations/eu";
15 import { fa } from "../translations/fa";
16 import { fi } from "../translations/fi";
17 import { fr } from "../translations/fr";
18 import { ga } from "../translations/ga";
19 import { gl } from "../translations/gl";
20 import { hr } from "../translations/hr";
21 import { id } from "../translations/id";
22 import { it } from "../translations/it";
23 import { ja } from "../translations/ja";
24 import { ko } from "../translations/ko";
25 import { nl } from "../translations/nl";
26 import { oc } from "../translations/oc";
27 import { pl } from "../translations/pl";
28 import { pt } from "../translations/pt";
29 import { pt_BR } from "../translations/pt_BR";
30 import { ru } from "../translations/ru";
31 import { sv } from "../translations/sv";
32 import { vi } from "../translations/vi";
33 import { zh } from "../translations/zh";
34 import { zh_Hant } from "../translations/zh_Hant";
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 export class I18NextService {
96   #i18n: typeof i18next;
97   static #instance: I18NextService;
98
99   private constructor() {
100     this.#i18n = i18next;
101     this.#i18n.use(LanguageDetector).init({
102       debug: false,
103       compatibilityJSON: "v3",
104       supportedLngs: languages.map(l => l.code),
105       nonExplicitSupportedLngs: true,
106       // load: 'languageOnly',
107       // initImmediate: false,
108       fallbackLng: "en",
109       resources,
110       interpolation: { format },
111     });
112   }
113
114   static get #Instance() {
115     return this.#instance ?? (this.#instance = new this());
116   }
117
118   public static get i18n() {
119     return this.#Instance.#i18n;
120   }
121 }