summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
authorHaishan <[email protected]>2020-12-06 14:57:59 +0800
committerHaishan <[email protected]>2020-12-06 20:19:51 +0800
commit8a50ef4ef2f6f6044d36ea2f4fe06e663083972e (patch)
treeda098c1434b5f745f391330dde37b6468deec45b /src/misc
parenta8c6cd23ce2b585362f515080b2167990c554fed (diff)
feat: initial Chinese UI language support
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/i18n.ts61
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;