Skip to content

Commit

Permalink
feat: 解耦i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Jul 13, 2023
1 parent 535a2a2 commit 3dc9b40
Show file tree
Hide file tree
Showing 4 changed files with 1,189 additions and 1,138 deletions.
1 change: 1 addition & 0 deletions playground/spa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@types/react": "^18.0.28",
"@types/react-dom": "^18.0.11",
"@vitejs/plugin-react": "^3.1.0",
"cross-env": "^7.0.3",
"typescript": "^4.9.5",
"vite": "^4.2.1"
}
Expand Down
26 changes: 25 additions & 1 deletion playground/spa/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ i18next
},
})

setupI18n({

const { loadResource, onLanguageChanged } = setupI18n({
i18n: i18next,
language: i18next.language,
addResource: (langs, currentLang) => {
Object.keys(langs).forEach((ns) => {
i18next.addResourceBundle(currentLang, ns, langs[ns])
})
},
onLocaleChange: () => {
root.render(
<React.StrictMode>
Expand All @@ -50,3 +57,20 @@ setupI18n({
},
fallbackLng,
})

const _changeLanguage = i18next.changeLanguage
i18next.changeLanguage = async (lang: string | undefined, ...args) => {
let currentLng = i18next.language
// If language did't change, return
if (currentLng === lang) return undefined as any
currentLng = lang || currentLng
await loadResource(lang)
return _changeLanguage(lang, ...args)
}



i18next.on('languageChanged', (lang) => {
onLanguageChanged(lang)
})

Loading

0 comments on commit 3dc9b40

Please sign in to comment.