From 294836ce40afcb9e3af8146575d06bf386bfe1a1 Mon Sep 17 00:00:00 2001 From: Yugo Ogura Date: Mon, 19 Oct 2020 11:24:52 +0900 Subject: [PATCH] feat(i18n): add nav dropdown language selector feature (#91) * feat: set language selector on navbar * feat: hide language selector if no other langages contents * feat: i18n selectText * fix: move language selector before repo link * fix: change reference of locales * fix: fix not to fail when no locales --- .../theme-default/components/NavBarLinks.ts | 43 ++++++++++++++++++- .../theme-default/components/NavBarLinks.vue | 1 + types/shared.d.ts | 2 + 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index f1133d63..d34b8c40 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -1,5 +1,5 @@ import { computed } from 'vue' -import { useSiteData, useSiteDataByRoute } from 'vitepress' +import { useSiteData, useSiteDataByRoute, useRoute } from 'vitepress' import NavBarLink from './NavBarLink.vue' import NavDropdownLink from './NavDropdownLink.vue' import { DefaultTheme } from '../config' @@ -17,6 +17,7 @@ export default { setup() { const siteDataByRoute = useSiteDataByRoute() const siteData = useSiteData() + const route = useRoute() const repoInfo = computed(() => { const theme = siteData.value.themeConfig as DefaultTheme.Config const repo = theme.docsRepo || theme.repo @@ -40,6 +41,43 @@ export default { } return null }) + + const localeCandidates = computed(() => { + const locales = siteData.value.themeConfig.locales + if (!locales) { + return null + } + const localeKeys = Object.keys(locales) + if (localeKeys.length <= 1) { + return null + } + + const currentLangBase = localeKeys.find((v) => { + if (v === '/') { + return false + } + return route.path.startsWith(v) + }) + const currentContentPath = currentLangBase + ? route.path.substring(currentLangBase.length - 1) + : route.path + const candidates = localeKeys.map((v) => { + return { + text: locales[v].label || locales[v].lang, + link: `${v}${currentContentPath}` + } + }) + + const currentLangKey = currentLangBase ? currentLangBase : '/' + const selectText = locales[currentLangKey].selectText + ? locales[currentLangKey].selectText + : 'Languages' + return { + text: selectText, + items: candidates + } + }) + return { navData: process.env.NODE_ENV === 'production' @@ -47,7 +85,8 @@ export default { siteDataByRoute.value.themeConfig.nav : // use computed in dev for hot reload computed(() => siteDataByRoute.value.themeConfig.nav), - repoInfo + repoInfo, + localeCandidates } } } diff --git a/src/client/theme-default/components/NavBarLinks.vue b/src/client/theme-default/components/NavBarLinks.vue index ab02e2b4..9d1d5df2 100644 --- a/src/client/theme-default/components/NavBarLinks.vue +++ b/src/client/theme-default/components/NavBarLinks.vue @@ -6,6 +6,7 @@ + diff --git a/types/shared.d.ts b/types/shared.d.ts index 840e6aeb..08fb259a 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -2,6 +2,8 @@ export interface LocaleConfig { lang: string + selectText?: string + label?: string title?: string description?: string head?: HeadConfig[]