diff --git a/src/client/app/data.ts b/src/client/app/data.ts index 143b3f5a..26f22c13 100644 --- a/src/client/app/data.ts +++ b/src/client/app/data.ts @@ -46,9 +46,11 @@ export function initData(route: Route): VitePressData { frontmatter: computed(() => route.data.frontmatter), lang: computed(() => site.value.lang), localePath: computed(() => { - const { locales, lang } = site.value - const path = Object.keys(locales).find((lp) => locales[lp].lang === lang) - return withBase((locales && path) || '/') + const { langs } = site.value + const path = Object.keys(langs).find( + (lp) => langs[lp].lang === site.value.lang + ) + return withBase(path || '/') }), title: computed(() => { return route.data.title diff --git a/src/client/theme-default/composables/nav.ts b/src/client/theme-default/composables/nav.ts index 4cef78f2..224fc8ae 100644 --- a/src/client/theme-default/composables/nav.ts +++ b/src/client/theme-default/composables/nav.ts @@ -1,5 +1,5 @@ import { computed } from 'vue' -import { useData } from 'vitepress' +import { useData, useRoute } from 'vitepress' import type { DefaultTheme } from '../config' export function useLanguageLinks() { @@ -13,16 +13,15 @@ export function useLanguageLinks() { return null } - const routerPath = localePath.value + const route = useRoute() - const candidates = localePaths.map((v) => { - const localePath = v.endsWith('/') ? v.slice(0, -1) : v + // intentionally remove the leading slash because each locale has one + const currentPath = route.path.replace(localePath.value, '') - return { - text: langs[v], - link: `${localePath}${routerPath}` - } - }) + const candidates = localePaths.map((v) => ({ + text: langs[v].label, + link: `${v}${currentPath}` + })) const selectText = theme.value.selectText || 'Languages' diff --git a/src/node/config.ts b/src/node/config.ts index f86b9272..a7b052b5 100644 --- a/src/node/config.ts +++ b/src/node/config.ts @@ -147,9 +147,7 @@ export async function resolveSiteData( head: userConfig.head || [], themeConfig: userConfig.themeConfig || {}, locales: userConfig.locales || {}, - langs: createLangDictionary( - userConfig.themeConfig && userConfig.themeConfig.locales - ), + langs: createLangDictionary(userConfig), customData: userConfig.customData || {} } } diff --git a/src/shared/shared.ts b/src/shared/shared.ts index 25e1c6cd..bdfe89f6 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -1,4 +1,4 @@ -import { SiteData } from '../../types/shared' +import { LocaleConfig, SiteData } from '../../types/shared' export type { SiteData, @@ -36,12 +36,20 @@ function resolveLocales( return localeRoot ? locales[localeRoot] : undefined } -export function createLangDictionary(locales: any | undefined) { - return locales +export function createLangDictionary(siteData: { + themeConfig?: any + locales?: Record +}) { + const { locales } = siteData.themeConfig + const siteLocales = siteData.locales + return locales && siteLocales ? Object.keys(locales).reduce((langs, path) => { - langs[path] = locales![path].label + langs[path] = { + label: locales![path].label, + lang: siteLocales[path].lang + } return langs - }, {} as Record) + }, {} as Record) : {} } @@ -70,7 +78,7 @@ export function resolveSiteDataByRoute( lang: (localeData || siteData).lang, // clean the locales to reduce the bundle size locales: {}, - langs: createLangDictionary(siteData.themeConfig.locales) + langs: createLangDictionary(siteData) } } diff --git a/types/shared.d.ts b/types/shared.d.ts index 4c96c62d..6b953f85 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -17,7 +17,7 @@ export interface SiteData { head: HeadConfig[] themeConfig: ThemeConfig locales: Record - langs: Record + langs: Record customData: any }