fix: fix switch language error #103

pull/104/head
zuofenghua 5 years ago
parent 123cc73837
commit e937fc0f1a

@ -3,6 +3,7 @@ import { useSiteData, useSiteDataByRoute, useRoute } from 'vitepress'
import NavBarLink from './NavBarLink.vue' import NavBarLink from './NavBarLink.vue'
import NavDropdownLink from './NavDropdownLink.vue' import NavDropdownLink from './NavDropdownLink.vue'
import { DefaultTheme } from '../config' import { DefaultTheme } from '../config'
import { inBrowser } from '/@app/utils'
const platforms = ['GitHub', 'GitLab', 'Bitbucket'].map( const platforms = ['GitHub', 'GitLab', 'Bitbucket'].map(
(platform) => [platform, new RegExp(platform, 'i')] as const (platform) => [platform, new RegExp(platform, 'i')] as const
@ -52,19 +53,28 @@ export default {
return null return null
} }
// handle site base
const siteBase = inBrowser ? siteData.value.base : '/'
const siteBaseWithoutSuffix = siteBase.endsWith('/')
? siteBase.slice(0, -1)
: siteBase
// remove site base in browser env
const routerPath = route.path.slice(siteBaseWithoutSuffix.length)
const currentLangBase = localeKeys.find((v) => { const currentLangBase = localeKeys.find((v) => {
if (v === '/') { if (v === '/') {
return false return false
} }
return route.path.startsWith(v) return routerPath.startsWith(v)
}) })
const currentContentPath = currentLangBase const currentContentPath = currentLangBase
? route.path.substring(currentLangBase.length - 1) ? routerPath.substring(currentLangBase.length - 1)
: route.path : routerPath
const candidates = localeKeys.map((v) => { const candidates = localeKeys.map((v) => {
const localePath = v.endsWith('/') ? v.slice(0, -1) : v
return { return {
text: locales[v].label || locales[v].lang, text: locales[v].label || locales[v].lang,
link: `${v}${currentContentPath}` link: `${localePath}${currentContentPath}`
} }
}) })

Loading…
Cancel
Save