Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix switch language error #103 #106 #104

Merged
merged 3 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/client/theme-default/components/NavBarLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSiteData, useSiteDataByRoute, useRoute } from 'vitepress'
import NavBarLink from './NavBarLink.vue'
import NavDropdownLink from './NavDropdownLink.vue'
import { DefaultTheme } from '../config'
import { inBrowser } from '/@app/utils'

const platforms = ['GitHub', 'GitLab', 'Bitbucket'].map(
(platform) => [platform, new RegExp(platform, 'i')] as const
Expand Down Expand Up @@ -52,19 +53,28 @@ export default {
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) => {
if (v === '/') {
return false
}
return route.path.startsWith(v)
return routerPath.startsWith(v)
})
const currentContentPath = currentLangBase
? route.path.substring(currentLangBase.length - 1)
: route.path
? routerPath.substring(currentLangBase.length - 1)
: routerPath
const candidates = localeKeys.map((v) => {
const localePath = v.endsWith('/') ? v.slice(0, -1) : v
return {
text: locales[v].label || locales[v].lang,
link: `${v}${currentContentPath}`
link: `${localePath}${currentContentPath}`
}
})

Expand All @@ -78,13 +88,11 @@ export default {
}
})

const navData = computed(() => {
return siteDataByRoute.value.themeConfig.nav
})
return {
navData:
process.env.NODE_ENV === 'production'
? // navbar items do not change in production
siteDataByRoute.value.themeConfig.nav
: // use computed in dev for hot reload
computed(() => siteDataByRoute.value.themeConfig.nav),
navData,
repoInfo,
localeCandidates
}
Expand Down
7 changes: 7 additions & 0 deletions src/shared/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SiteData } from '../../types/shared'
const inBrowser = typeof window !== 'undefined'

function findMatchRoot(route: string, roots: string[]) {
// first match to the routes with the most deep level.
Expand Down Expand Up @@ -27,6 +28,12 @@ function resolveLocales<T>(

// this merges the locales data to the main data by the route
export function resolveSiteDataByRoute(siteData: SiteData, route: string) {
if (inBrowser) {
const siteBaseWithoutSuffix = siteData.base.endsWith('/')
? siteData.base.slice(0, -1)
: siteData.base
route = route.slice(siteBaseWithoutSuffix.length)
}
const localeData = resolveLocales(siteData.locales || {}, route) || {}
const localeThemeConfig =
resolveLocales<any>(
Expand Down