diff options
Diffstat (limited to 'FrontEnd/src/i18n.ts')
-rw-r--r-- | FrontEnd/src/i18n.ts | 43 |
1 files changed, 17 insertions, 26 deletions
diff --git a/FrontEnd/src/i18n.ts b/FrontEnd/src/i18n.ts index 6a38e5b4..9bf8721f 100644 --- a/FrontEnd/src/i18n.ts +++ b/FrontEnd/src/i18n.ts @@ -1,43 +1,34 @@ -import i18n, { BackendModule, ResourceKey } from "i18next"; +import i18n, { BackendModule } from "i18next"; import LanguageDetector from "i18next-browser-languagedetector"; import { initReactI18next } from "react-i18next"; const backend: BackendModule = { type: "backend", read(language, namespace, callback) { - function error(message: string): void { - callback(new Error(message), false); - } - - function success(result: ResourceKey): void { - callback(null, result); - } - - const promise = (() => { + (async () => { if (namespace === "translation") { if (language === "en") { - return import("./locales/en/translation.json"); + return await import("./locales/en/translation.json"); } else if (language === "zh") { - return import("./locales/zh/translation.json"); + return await import("./locales/zh/translation.json"); } else { - error(`Language ${language} is not supported.`); + throw Error(`Language ${language} is not supported.`); } } else if (namespace === "admin") { if (language === "en") { - return import("./locales/en/admin.json"); + return await import("./locales/en/admin.json"); } else if (language === "zh") { - return import("./locales/zh/admin.json"); + return await import("./locales/zh/admin.json"); } else { - error(`Language ${language} is not supported.`); + throw Error(`Language ${language} is not supported.`); } } else { - error(`Namespace ${namespace} is not supported.`); + throw Error(`Namespace ${namespace} is not supported.`); } - })(); - - if (promise) { - void promise.then((d) => success(d.default)); - } + })().then( + (resources) => callback(null, resources.default), + (error: Error) => callback(error, null), + ); }, init() {}, // eslint-disable-line @typescript-eslint/no-empty-function create() {}, // eslint-disable-line @typescript-eslint/no-empty-function @@ -51,7 +42,7 @@ export const i18nPromise = i18n fallbackLng: false, lowerCaseLng: true, - debug: import.meta.env.DEV, + debug: process.env.NODE_ENV === "development", interpolation: { escapeValue: false, // not needed for react!! @@ -71,8 +62,8 @@ export const i18nPromise = i18n */ }); -if (import.meta.hot) { - import.meta.hot.accept( +if (module.hot) { + module.hot.accept( [ "./locales/en/translation.json", "./locales/zh/translation.json", @@ -81,7 +72,7 @@ if (import.meta.hot) { ], () => { void i18n.reloadResources(); - } + }, ); } |