refactor: split i18n namespaces
Signed-off-by: Innei <i@innei.in>
This commit is contained in:
parent
a97e60cd00
commit
bda3319e87
|
|
@ -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": [
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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/<module-name>/en.json
|
||||
// Absolute path e.g. /Users/innei/git/follow/locales/<module-name>/en.json
|
||||
|
||||
// 1. parse root language
|
||||
if (!file.includes("locales/namespaces")) {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue