From 313a182a460401dcad942081a4b54bee48f17b64 Mon Sep 17 00:00:00 2001 From: Haishan Date: Mon, 30 Jan 2023 23:10:31 +0800 Subject: [PATCH] Remove unnecessary i18next-http-backend --- package.json | 1 - pnpm-lock.yaml | 45 ------------------------------------- src/misc/i18n.ts | 58 ++++++++++++++++++++++-------------------------- 3 files changed, 27 insertions(+), 77 deletions(-) diff --git a/package.json b/package.json index 03715ef..05efdc8 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "history": "5.3.0", "i18next": "22.4.9", "i18next-browser-languagedetector": "7.0.1", - "i18next-http-backend": "2.1.1", "immer": "9.0.19", "invariant": "^2.2.4", "lodash-es": "^4.17.21", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66703b1..443e9c0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -36,7 +36,6 @@ specifiers: history: 5.3.0 i18next: 22.4.9 i18next-browser-languagedetector: 7.0.1 - i18next-http-backend: 2.1.1 immer: 9.0.19 invariant: ^2.2.4 lodash-es: ^4.17.21 @@ -90,7 +89,6 @@ dependencies: history: 5.3.0 i18next: 22.4.9 i18next-browser-languagedetector: 7.0.1 - i18next-http-backend: 2.1.1 immer: 9.0.19 invariant: 2.2.4 lodash-es: 4.17.21 @@ -4090,14 +4088,6 @@ packages: yaml: 1.10.2 dev: true - /cross-fetch/3.1.5: - resolution: {integrity: sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==} - dependencies: - node-fetch: 2.6.7 - transitivePeerDependencies: - - encoding - dev: false - /cross-spawn/7.0.3: resolution: {integrity: sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==} engines: {node: '>= 8'} @@ -5102,14 +5092,6 @@ packages: '@babel/runtime': 7.20.13 dev: false - /i18next-http-backend/2.1.1: - resolution: {integrity: sha512-jByfUCDVgQ8+/Wens7queQhYYvMcGTW/lR4IJJNEDDXnmqjLrwi8ubXKpmp76/JIWEZHffNdWqnxFJcTVGeaOw==} - dependencies: - cross-fetch: 3.1.5 - transitivePeerDependencies: - - encoding - dev: false - /i18next/22.4.9: resolution: {integrity: sha512-8gWMmUz460KJDQp/ob3MNUX84cVuDRY9PLFPnV8d+Qezz/6dkjxwOaH70xjrCNDO+JrUL25iXfAIN9wUkInNZw==} dependencies: @@ -5633,18 +5615,6 @@ packages: resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==} dev: true - /node-fetch/2.6.7: - resolution: {integrity: sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==} - engines: {node: 4.x || >=6.0.0} - peerDependencies: - encoding: ^0.1.0 - peerDependenciesMeta: - encoding: - optional: true - dependencies: - whatwg-url: 5.0.0 - dev: false - /node-releases/2.0.6: resolution: {integrity: sha512-PiVXnNuFm5+iYkLBNeq5211hvO38y63T0i2KKh2KnUs3RpzJ+JtODFjkD8yjLwnDkTYF1eKXheUwdssR+NRZdg==} dev: true @@ -6534,10 +6504,6 @@ packages: is-number: 7.0.0 dev: true - /tr46/0.0.3: - resolution: {integrity: sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==} - dev: false - /tr46/1.0.1: resolution: {integrity: sha512-dTpowEjclQ7Kgx5SdBkqRzVhERQXov8/l9Ft9dVM9fmg0W0KQSVaXX9T4i6twCPNtYiZM53lpSSUAwJbFPOHxA==} dependencies: @@ -6753,21 +6719,10 @@ packages: loose-envify: 1.4.0 dev: false - /webidl-conversions/3.0.1: - resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} - dev: false - /webidl-conversions/4.0.2: resolution: {integrity: sha512-YQ+BmxuTgd6UXZW3+ICGfyqRyHXVlD5GtQr5+qjiNW7bF0cqrzX500HVXPBOvgXb5YnzDd+h0zqyv61KUD7+Sg==} dev: true - /whatwg-url/5.0.0: - resolution: {integrity: sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==} - dependencies: - tr46: 0.0.3 - webidl-conversions: 3.0.1 - dev: false - /whatwg-url/7.1.0: resolution: {integrity: sha512-WUu7Rg1DroM7oQvGWfOiAK21n74Gg+T4elXEQYkOhtyLeWiJFoOGLXPKI/9gzIie9CtwVLm8wtw6YJdKyxSjeg==} dependencies: diff --git a/src/misc/i18n.ts b/src/misc/i18n.ts index a0bb7b4..13cedd4 100644 --- a/src/misc/i18n.ts +++ b/src/misc/i18n.ts @@ -1,44 +1,40 @@ +import type { ReadCallback } from 'i18next'; import i18next from 'i18next'; import LanguageDetector from 'i18next-browser-languagedetector'; -import HttpBackend from 'i18next-http-backend'; import { initReactI18next } from 'react-i18next'; -type BackendRequestCallback = (err: null, result: { status: number; data: any }) => void; +const LngBackend = { + type: 'backend' as const, + read: ( + lng: string, + _namespace: string, + callback: ReadCallback, + ) => { + let p: PromiseLike<{ data: any }>; + switch (lng) { + case 'zh': + case 'zh-CN': + p = import('src/i18n/zh'); + break; + case 'en': + default: + p = import('src/i18n/en'); + break; + } + if (p) { + p.then(d => callback(null, d.data), err => callback(err, null)); + } else { + callback(new Error(`unable to load translation file for language ${lng}`), null) + } + } +} i18next - .use(HttpBackend) .use(initReactI18next) .use(LanguageDetector) + .use(LngBackend) .init({ debug: process.env.NODE_ENV === 'development', - 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 = import('src/i18n/zh'); - break; - case '/__en/translation.json': - default: - p = import('src/i18n/en'); - break; - } - - if (p) { - p.then((mod) => { - callback(null, { status: 200, data: mod.data }); - }); - } - }, - }, fallbackLng: 'en', interpolation: { escapeValue: false,