mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-14 17:31:02 +08:00
d8954f945e
* chore: make eslints work in docs folder * refactor(dev): improve eslint * fix eslint issue Co-authored-by: Kevin <sxzz@sxzz.moe> Co-authored-by: jeremywuuuuu <15975785+JeremyWuuuuu@users.noreply.github.com>
42 lines
1006 B
TypeScript
42 lines
1006 B
TypeScript
import { computed } from 'vue'
|
|
import { useRouter, useRoute } from 'vitepress'
|
|
import { PREFERRED_LANG_KEY } from '../constant'
|
|
|
|
import langs from '../../i18n/lang.json'
|
|
import translationLocale from '../../i18n/component/translation.json'
|
|
import { useLang } from './lang'
|
|
|
|
export const useTranslation = () => {
|
|
const route = useRoute()
|
|
const router = useRouter()
|
|
const lang = useLang()
|
|
|
|
const languageMap = {
|
|
'en-US': 'English',
|
|
'zh-CN': '中文',
|
|
'es-ES': 'Español',
|
|
'fr-FR': 'Français',
|
|
'ja-JP': '日本語',
|
|
}
|
|
|
|
const helpTranslate = computed(() => translationLocale[lang.value].help)
|
|
|
|
const switchLang = (targetLang: string) => {
|
|
if (lang.value === targetLang) return
|
|
localStorage.setItem(PREFERRED_LANG_KEY, targetLang)
|
|
const firstSlash = route.path.indexOf('/', 1)
|
|
|
|
const goTo = `/${targetLang}/${route.path.slice(firstSlash + 1)}`
|
|
|
|
router.go(goTo)
|
|
}
|
|
|
|
return {
|
|
helpTranslate,
|
|
languageMap,
|
|
langs,
|
|
lang,
|
|
switchLang,
|
|
}
|
|
}
|