-
Notifications
You must be signed in to change notification settings - Fork 678
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: wait language load then switch to target language
Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
4 changed files
with
96 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { currentSupportedLanguages, dayjsLocaleImportMap } from "@renderer/@types/constants" | ||
import { defaultResources } from "@renderer/@types/default-resource" | ||
import { fallbackLanguage, i18nAtom, LocaleCache } from "@renderer/i18n" | ||
import { jotaiStore } from "@renderer/lib/jotai" | ||
import { isEmptyObject } from "@renderer/lib/utils" | ||
import dayjs from "dayjs" | ||
import i18next from "i18next" | ||
import { toast } from "sonner" | ||
|
||
const loadingLangLock = new Set<string>() | ||
const loadedLangs = new Set<string>([fallbackLanguage]) | ||
|
||
export const loadLanguageAndApply = async (lang: string) => { | ||
const dayjsImport = dayjsLocaleImportMap[lang] | ||
|
||
if (dayjsImport) { | ||
const [locale, loader] = dayjsImport | ||
loader().then(() => { | ||
dayjs.locale(locale) | ||
}) | ||
} | ||
|
||
const { t } = jotaiStore.get(i18nAtom) | ||
if (loadingLangLock.has(lang)) return | ||
const isSupport = currentSupportedLanguages.includes(lang) | ||
if (!isSupport) { | ||
return | ||
} | ||
const loaded = loadedLangs.has(lang) | ||
|
||
if (loaded) { | ||
return | ||
} | ||
|
||
loadingLangLock.add(lang) | ||
|
||
if (import.meta.env.DEV) { | ||
const nsGlobbyMap = import.meta.glob("@locales/*/*.json") | ||
|
||
const namespaces = Object.keys(defaultResources.en) | ||
|
||
const res = await Promise.allSettled( | ||
namespaces.map(async (ns) => { | ||
const loader = nsGlobbyMap[`../../locales/${ns}/${lang}.json`] | ||
|
||
if (!loader) return | ||
const nsResources = await loader().then((m: any) => m.default) | ||
|
||
i18next.addResourceBundle(lang, ns, nsResources, true, true) | ||
}), | ||
) | ||
|
||
for (const r of res) { | ||
if (r.status === "rejected") { | ||
toast.error(`${t("common:tips.load-lng-error")}: ${lang}`) | ||
loadingLangLock.delete(lang) | ||
|
||
return | ||
} | ||
} | ||
} else { | ||
const res = await eval(`import('/locales/${lang}.js')`) | ||
.then((res: any) => res?.default || res) | ||
.catch(() => { | ||
toast.error(`${t("common:tips.load-lng-error")}: ${lang}`) | ||
loadingLangLock.delete(lang) | ||
return {} | ||
}) | ||
|
||
if (isEmptyObject(res)) { | ||
return | ||
} | ||
for (const namespace in res) { | ||
i18next.addResourceBundle(lang, namespace, res[namespace], true, true) | ||
} | ||
} | ||
|
||
await i18next.reloadResources() | ||
await i18next.changeLanguage(lang) | ||
LocaleCache.shared.set(lang) | ||
loadedLangs.add(lang) | ||
loadingLangLock.delete(lang) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters