diff options
Diffstat (limited to 'src/misc')
| -rw-r--r-- | src/misc/i18n.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/misc/i18n.ts b/src/misc/i18n.ts new file mode 100644 index 0000000..023c5d7 --- /dev/null +++ b/src/misc/i18n.ts @@ -0,0 +1,61 @@ +import i18next from 'i18next'; +import LanguageDetector from 'i18next-browser-languagedetector'; +import HttpBackend from 'i18next-http-backend'; +import { initReactI18next } from 'react-i18next'; + +const allLocales = { + zh: import('src/i18n/zh'), + en: import('src/i18n/en'), +}; + +type BackendRequestCallback = ( + err: null, + result: { status: number; data: any } +) => void; + +i18next + .use(HttpBackend) + .use(initReactI18next) + .use(LanguageDetector) + .init({ + debug: process.env.NODE_ENV === 'development', + // 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': + p = allLocales.zh; + break; + case '/__en/translation.json': + default: + p = allLocales.en; + break; + } + + if (p) { + p.then((mod) => { + callback(null, { status: 200, data: mod.data }); + }); + } + }, + }, + supportedLngs: ['en', 'zh'], + fallbackLng: 'en', + interpolation: { + escapeValue: false, + }, + }); + +if (process.env.NODE_ENV === 'development') { + window.i18n = i18next; +} + +export default i18next; |
