From bda3319e876626d4e8aa69062f72a5ff9123d6f8 Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 12 Sep 2024 15:23:11 +0800 Subject: [PATCH] refactor: split i18n namespaces Signed-off-by: Innei --- .vscode/settings.json | 3 +++ locales/{ => app}/en.json | 0 locales/{ => app}/zh_CN.json | 0 locales/{namespaces => }/common/en.json | 0 locales/{namespaces => }/common/zh_CN.json | 0 locales/{namespaces => }/lang/en.json | 0 locales/{namespaces => }/lang/zh_CN.json | 0 src/renderer/src/@types/constants.ts | 2 +- src/renderer/src/@types/default-resource.ts | 10 +++++----- src/renderer/src/@types/i18next.d.ts | 4 ++-- src/renderer/src/@types/resources.ts | 20 ------------------- src/renderer/src/i18n.ts | 4 ++-- src/renderer/src/providers/i18n-provider.tsx | 21 ++++++++++---------- 13 files changed, 23 insertions(+), 41 deletions(-) rename locales/{ => app}/en.json (100%) rename locales/{ => app}/zh_CN.json (100%) rename locales/{namespaces => }/common/en.json (100%) rename locales/{namespaces => }/common/zh_CN.json (100%) rename locales/{namespaces => }/lang/en.json (100%) rename locales/{namespaces => }/lang/zh_CN.json (100%) delete mode 100644 src/renderer/src/@types/resources.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index d889a99a0..62eba1548 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,9 @@ // ["tw\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"] ["tw`([^`]*)`", "([^`]*)"] ], + "i18n-ally.namespace": true, + "i18n-ally.pathMatcher": "{namespaces}/{locale}.json", + "i18n-ally.defaultNamespace": "app", // If you do not want to autofix some rules on save // You can put this in your user settings or workspace settings "eslint.codeActionsOnSave.rules": [ diff --git a/locales/en.json b/locales/app/en.json similarity index 100% rename from locales/en.json rename to locales/app/en.json diff --git a/locales/zh_CN.json b/locales/app/zh_CN.json similarity index 100% rename from locales/zh_CN.json rename to locales/app/zh_CN.json diff --git a/locales/namespaces/common/en.json b/locales/common/en.json similarity index 100% rename from locales/namespaces/common/en.json rename to locales/common/en.json diff --git a/locales/namespaces/common/zh_CN.json b/locales/common/zh_CN.json similarity index 100% rename from locales/namespaces/common/zh_CN.json rename to locales/common/zh_CN.json diff --git a/locales/namespaces/lang/en.json b/locales/lang/en.json similarity index 100% rename from locales/namespaces/lang/en.json rename to locales/lang/en.json diff --git a/locales/namespaces/lang/zh_CN.json b/locales/lang/zh_CN.json similarity index 100% rename from locales/namespaces/lang/zh_CN.json rename to locales/lang/zh_CN.json diff --git a/src/renderer/src/@types/constants.ts b/src/renderer/src/@types/constants.ts index 5f2637c10..7a4741352 100644 --- a/src/renderer/src/@types/constants.ts +++ b/src/renderer/src/@types/constants.ts @@ -1,5 +1,5 @@ export const currentSupportedLanguages = (() => { - const langsFiles = import.meta.glob("../../../../locales/*.json") + const langsFiles = import.meta.glob("../../../../locales/app/*.json") const langs = [] as string[] for (const key in langsFiles) { diff --git a/src/renderer/src/@types/default-resource.ts b/src/renderer/src/@types/default-resource.ts index aad03bde4..2b47a7921 100644 --- a/src/renderer/src/@types/default-resource.ts +++ b/src/renderer/src/@types/default-resource.ts @@ -1,7 +1,7 @@ -import en from "../../../../locales/en.json" -import common_en from "../../../../locales/namespaces/common/en.json" -import common_zhCN from "../../../../locales/namespaces/common/zh_CN.json" -import lang_en from "../../../../locales/namespaces/lang/en.json" +import en from "../../../../locales/app/en.json" +import common_en from "../../../../locales/common/en.json" +import common_zhCN from "../../../../locales/common/zh_CN.json" +import lang_en from "../../../../locales/lang/en.json" /** * This file is the language resource that is loaded in full when the app is initialized. @@ -13,7 +13,7 @@ import lang_en from "../../../../locales/namespaces/lang/en.json" export const defaultResources = { en: { - translation: en, + app: en, lang: lang_en, common: common_en, }, diff --git a/src/renderer/src/@types/i18next.d.ts b/src/renderer/src/@types/i18next.d.ts index 880beacad..9b3d0a1c1 100644 --- a/src/renderer/src/@types/i18next.d.ts +++ b/src/renderer/src/@types/i18next.d.ts @@ -1,9 +1,9 @@ -import type resources from "./resources" +import type resources from "./default-resource" declare module "i18next" { interface CustomTypeOptions { resources: (typeof resources)["en"] - defaultNS: "translation" + defaultNS: "app" // if you see an error like: "Argument of type 'DefaultTFuncReturn' is not assignable to parameter of type xyz" // set returnNull to false (and also in the i18next init options) // returnNull: false; diff --git a/src/renderer/src/@types/resources.ts b/src/renderer/src/@types/resources.ts deleted file mode 100644 index 6165e475e..000000000 --- a/src/renderer/src/@types/resources.ts +++ /dev/null @@ -1,20 +0,0 @@ -import en from "../../../../locales/en.json" -import common_en from "../../../../locales/namespaces/common/en.json" -import common_zhCN from "../../../../locales/namespaces/common/zh_CN.json" -import lang_en from "../../../../locales/namespaces/lang/en.json" -import lang_zhCN from "../../../../locales/namespaces/lang/zh_CN.json" -import zhCN from "../../../../locales/zh_CN.json" - -const resources = { - en: { - translation: en, - lang: lang_en, - common: common_en, - }, - zh_CN: { - translation: zhCN, - lang: lang_zhCN, - common: common_zhCN, - }, -} -export default resources diff --git a/src/renderer/src/i18n.ts b/src/renderer/src/i18n.ts index e5fc3b8cb..dfc7227be 100644 --- a/src/renderer/src/i18n.ts +++ b/src/renderer/src/i18n.ts @@ -8,7 +8,7 @@ import { jotaiStore } from "./lib/jotai" export const i18nAtom = atom(i18next) -export const defaultNS = "translation" +export const defaultNS = "app" export const fallbackLanguage = "en" export const initI18n = async () => { @@ -27,7 +27,7 @@ if (import.meta.hot) { const resources = JSON.parse(content) const i18next = jotaiStore.get(i18nAtom) // `file` is absolute path e.g. /Users/innei/git/follow/locales/en.json - // Absolute path e.g. /Users/innei/git/follow/locales/namespaces//en.json + // Absolute path e.g. /Users/innei/git/follow/locales//en.json // 1. parse root language if (!file.includes("locales/namespaces")) { diff --git a/src/renderer/src/providers/i18n-provider.tsx b/src/renderer/src/providers/i18n-provider.tsx index e0bdf4da9..0f1e1a7f1 100644 --- a/src/renderer/src/providers/i18n-provider.tsx +++ b/src/renderer/src/providers/i18n-provider.tsx @@ -28,24 +28,23 @@ const langChangedHandler = async (lang: string) => { loadingLangLock.add(lang) - const rootGlobbyMap = import.meta.glob("@locales/*.json") - const nsGlobbyMap = import.meta.glob("@locales/namespaces/*/*.json") + const nsGlobbyMap = import.meta.glob("@locales/*/*.json") - const rootResources = await rootGlobbyMap[`../../locales/${lang}.json`]() - .then((m: any) => m.default) - .catch(() => { - toast.error(`${t("common:tips.load-lng-error")}: ${lang}`) - }) - - i18next.addResourceBundle(lang, defaultNS, rootResources, true, true) + // const rootGlobbyMap = import.meta.glob("@locales/*.json") + // const rootResources = await rootGlobbyMap[`../../locales/${lang}.json`]() + // .then((m: any) => m.default) + // .catch(() => { + // toast.error(`${t("common:tips.load-lng-error")}: ${lang}`) + // }) + // i18next.addResourceBundle(lang, defaultNS, rootResources, true, true) const namespaces = Object.keys(defaultResources.en) const res = await Promise.allSettled( namespaces.map(async (ns) => { - if (ns === defaultNS) return + // if (ns === defaultNS) return - const loader = nsGlobbyMap[`../../locales/namespaces/${ns}/${lang}.json`] + const loader = nsGlobbyMap[`../../locales/${ns}/${lang}.json`] if (!loader) return const nsResources = await loader().then((m: any) => m.default)