perf: merge all namespace of i18n resource in prod
Signed-off-by: Innei <i@innei.in>
This commit is contained in:
@@ -29,16 +29,7 @@ if (import.meta.hot) {
|
||||
async ({ file, content }: { file: string; content: string }) => {
|
||||
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/<module-name>/en.json
|
||||
|
||||
// 1. parse root language
|
||||
// if (!file.includes("locales/namespaces")) {
|
||||
// const lang = file.split("/").pop()?.replace(".json", "")
|
||||
// if (!lang) return
|
||||
// i18next.addResourceBundle(lang, defaultNS, resources, true, true)
|
||||
// i18next.reloadResources(lang, defaultNS)
|
||||
// } else {
|
||||
const nsName = file.match(/locales\/(.+?)\//)?.[1]
|
||||
|
||||
if (!nsName) return
|
||||
@@ -48,7 +39,6 @@ if (import.meta.hot) {
|
||||
|
||||
console.info("reload", lang, nsName)
|
||||
await i18next.reloadResources(lang, nsName)
|
||||
// }
|
||||
|
||||
import.meta.env.DEV && EventBus.dispatch("I18N_UPDATE", "")
|
||||
},
|
||||
|
||||
@@ -42,37 +42,35 @@ const langChangedHandler = async (lang: string) => {
|
||||
|
||||
loadingLangLock.add(lang)
|
||||
|
||||
const nsGlobbyMap = import.meta.glob("@locales/*/*.json")
|
||||
if (import.meta.env.DEV) {
|
||||
const nsGlobbyMap = import.meta.glob("@locales/*/*.json")
|
||||
|
||||
// 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 namespaces = Object.keys(defaultResources.en)
|
||||
const res = await Promise.allSettled(
|
||||
namespaces.map(async (ns) => {
|
||||
const loader = nsGlobbyMap[`../../locales/${ns}/${lang}.json`]
|
||||
|
||||
const res = await Promise.allSettled(
|
||||
namespaces.map(async (ns) => {
|
||||
// if (ns === defaultNS) return
|
||||
if (!loader) return
|
||||
const nsResources = await loader().then((m: any) => m.default)
|
||||
|
||||
const loader = nsGlobbyMap[`../../locales/${ns}/${lang}.json`]
|
||||
i18next.addResourceBundle(lang, ns, nsResources, true, true)
|
||||
}),
|
||||
)
|
||||
|
||||
if (!loader) return
|
||||
const nsResources = await loader().then((m: any) => m.default)
|
||||
for (const r of res) {
|
||||
if (r.status === "rejected") {
|
||||
toast.error(`${t("common:tips.load-lng-error")}: ${lang}`)
|
||||
loadingLangLock.delete(lang)
|
||||
|
||||
i18next.addResourceBundle(lang, ns, nsResources, true, true)
|
||||
}),
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const res = await eval(`import('/locales/${lang}.js').then((res) => res?.default || res)`)
|
||||
|
||||
for (const r of res) {
|
||||
if (r.status === "rejected") {
|
||||
toast.error(`${t("common:tips.load-lng-error")}: ${lang}`)
|
||||
loadingLangLock.delete(lang)
|
||||
|
||||
return
|
||||
for (const namespace in res) {
|
||||
i18next.addResourceBundle(lang, namespace, res[namespace], true, true)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user