diff options
author | crupest <crupest@outlook.com> | 2021-06-15 14:14:28 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-15 14:14:28 +0800 |
commit | 47587812b809fee2a95c76266d9d0e42fc4ac1ca (patch) | |
tree | bfaa7320c838e21edf88b5a037263f89a8012222 /FrontEnd/src/i18n.ts | |
parent | da26373c7fc13cded47428b27638b349b0432437 (diff) | |
download | timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.tar.gz timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.tar.bz2 timeline-47587812b809fee2a95c76266d9d0e42fc4ac1ca.zip |
...
Diffstat (limited to 'FrontEnd/src/i18n.ts')
-rw-r--r-- | FrontEnd/src/i18n.ts | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/FrontEnd/src/i18n.ts b/FrontEnd/src/i18n.ts new file mode 100644 index 00000000..8caf51ec --- /dev/null +++ b/FrontEnd/src/i18n.ts @@ -0,0 +1,88 @@ +import i18n, { BackendModule, ResourceKey } from "i18next"; +import LanguageDetector from "i18next-browser-languagedetector"; +import { initReactI18next } from "react-i18next"; + +const backend: BackendModule = { + type: "backend", + async read(language, namespace, callback) { + function error(message: string): void { + callback(new Error(message), false); + } + + function success(result: ResourceKey): void { + callback(null, result); + } + + const promise = (() => { + if (namespace === "translation") { + if (language === "en") { + return import("./locales/en/translation.json"); + } else if (language === "zh") { + return import("./locales/zh/translation.json"); + } else { + error(`Language ${language} is not supported.`); + } + } else if (namespace === "admin") { + if (language === "en") { + return import("./locales/en/admin.json"); + } else if (language === "zh") { + return import("./locales/zh/admin.json"); + } else { + error(`Language ${language} is not supported.`); + } + } else { + error(`Namespace ${namespace} is not supported.`); + } + })(); + + if (promise) { + success((await promise).default); + } + }, + init() {}, // eslint-disable-line @typescript-eslint/no-empty-function + create() {}, // eslint-disable-line @typescript-eslint/no-empty-function +}; + +export const i18nPromise = i18n + .use(LanguageDetector) + .use(backend) + .use(initReactI18next) // bind react-i18next to the instance + .init({ + fallbackLng: false, + lowerCaseLng: true, + + debug: process.env.NODE_ENV === "development", + + interpolation: { + escapeValue: false, // not needed for react!! + }, + + // react i18next special options (optional) + // override if needed - omit if ok with defaults + /* + react: { + bindI18n: 'languageChanged', + bindI18nStore: '', + transEmptyNodeValue: '', + transSupportBasicHtmlNodes: true, + transKeepBasicHtmlNodesFor: ['br', 'strong', 'i'], + useSuspense: true, + } + */ + }); + +if (import.meta.hot) { + import.meta.hot.accept( + [ + "./locales/en/translation.json", + "./locales/zh/translation.json", + "./locales/en/admin.json", + "./locales/zh/admin.json", + ], + () => { + void i18n.reloadResources(); + } + ); +} + +export default i18n; |