-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f22a40
commit 14f3b92
Showing
4 changed files
with
73 additions
and
21 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
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,46 @@ | ||
import { useLocalStorage } from '@mantine/hooks' | ||
import i18n from 'i18next' | ||
import LanguageDetector from 'i18next-browser-languagedetector' | ||
import { useEffect } from 'react' | ||
import { initReactI18next } from 'react-i18next' | ||
import resources from 'virtual:i18next-loader' | ||
|
||
i18n | ||
.use(LanguageDetector) | ||
.use(initReactI18next) | ||
.init({ | ||
resources, | ||
fallbackLng: 'zh_CN', | ||
interpolation: { | ||
escapeValue: false, | ||
}, | ||
detection: { | ||
convertDetectedLanguage: 'Iso15897', | ||
}, | ||
}) | ||
|
||
export const useLanguage = () => { | ||
const [language, setLanguageInner] = useLocalStorage({ | ||
key: 'language', | ||
defaultValue: 'zh_CN', | ||
}) | ||
|
||
useEffect(() => { | ||
i18n.changeLanguage(language) | ||
}, [language]) | ||
|
||
const supportedLanguages = Object.keys(resources) | ||
|
||
const setLanguage = (lang: string) => { | ||
// check if language is supported | ||
if (supportedLanguages.includes(lang)) { | ||
setLanguageInner(lang) | ||
} else { | ||
setLanguageInner('zh_CN') | ||
} | ||
} | ||
|
||
return { language, setLanguage, supportedLanguages } | ||
} | ||
|
||
export default i18n |