summaryrefslogtreecommitdiff
path: root/src/misc/i18n.ts
blob: c416e6c180c47c9b1755dcb5e26c6cafe938acca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import i18next from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpBackend from 'i18next-http-backend';
import { initReactI18next } from 'react-i18next';

const allLocales = {
  zh_cn: import('~/i18n/zh-cn'),
  zh_tw: import('~/i18n/zh-tw'),
  en: import('~/i18n/en'),
  vi: import('~/i18n/vi'),
  ru: import('~/i18n/ru'),
};

type BackendRequestCallback = (err: null, result: { status: number; data: any }) => void;

i18next
  .use(HttpBackend)
  .use(initReactI18next)
  .use(LanguageDetector)
  .init({
    debug: import.meta.env.DEV,
    // resources,
    backend: {
      loadPath: '/__{{lng}}/{{ns}}.json',
      request: function (
        _options: any,
        url: string,
        _payload: any,
        callback: BackendRequestCallback
      ) {
        let p: PromiseLike<{ data: any }>;

        switch (url) {
          case '/__zh/translation.json':
          case '/__zh-CN/translation.json':
            p = allLocales.zh_cn;
            break;
          case '/__zh-TW/translation.json':
            p = allLocales.zh_tw;
            break;
          case '/__en/translation.json':
            p = allLocales.en;
            break;
          case '/__vi/translation.json':
            p = allLocales.vi;
            break;
          case '/__ru/translation.json':
            p = allLocales.ru;
            break;
          default:
            p = allLocales.zh_cn;
            break;
        }

        if (p) {
          p.then((mod) => {
            callback(null, { status: 200, data: mod.data });
          });
        }
      },
    },
    supportedLngs: ['zh-CN', 'zh-TW', 'en', 'vi', 'ru'],
    load: 'currentOnly',
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
  });

if (import.meta.env.DEV) {
  window.i18n = i18next;
}

export default i18next;