From c7682ea14887856713768658e86090a949aac055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=80=89=E5=84=AA=E6=82=9F?= Date: Sat, 19 Sep 2020 17:08:50 +0900 Subject: [PATCH 1/6] feat: set language selector on navbar --- .../theme-default/components/NavBarLinks.ts | 32 +++++++++++++++++-- .../theme-default/components/NavBarLinks.vue | 1 + types/shared.d.ts | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index f1133d63cf43..0c6d1152fbe8 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,32 @@ export default { } return null }) + + const localeCandidates = computed(() => { + const localeKeys = Object.keys(siteData.value.locales) + 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: + siteData.value.locales[v].label || siteData.value.locales[v].lang, + link: `${v}${currentContentPath}` + } + }) + return { + // TODO i18n text + text: 'Languages', + items: candidates + } + }) + return { navData: process.env.NODE_ENV === 'production' @@ -47,7 +74,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 ab02e2b4928c..95732c2ec429 100644 --- a/src/client/theme-default/components/NavBarLinks.vue +++ b/src/client/theme-default/components/NavBarLinks.vue @@ -7,6 +7,7 @@ + diff --git a/types/shared.d.ts b/types/shared.d.ts index 840e6aebbea5..850529b13494 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -2,6 +2,7 @@ export interface LocaleConfig { lang: string + label?: string title?: string description?: string head?: HeadConfig[] From 690325074c69ba48550469ae28a4b91b1752971d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=80=89=E5=84=AA=E6=82=9F?= Date: Sat, 19 Sep 2020 17:14:53 +0900 Subject: [PATCH 2/6] feat: hide language selector if no other langages contents --- src/client/theme-default/components/NavBarLinks.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index 0c6d1152fbe8..6ea874a970c3 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -44,6 +44,10 @@ export default { const localeCandidates = computed(() => { const localeKeys = Object.keys(siteData.value.locales) + if (localeKeys.length <= 1) { + return null + } + const currentLangBase = localeKeys.find((v) => { if (v === '/') { return false From da6df7a12c4ddee16762fb07c8b8c9a5ad42067f Mon Sep 17 00:00:00 2001 From: Spice-Z Date: Thu, 1 Oct 2020 20:37:58 +0900 Subject: [PATCH 3/6] feat: i18n selectText --- src/client/theme-default/components/NavBarLinks.ts | 8 ++++++-- types/shared.d.ts | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index 6ea874a970c3..11e220d56849 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -64,9 +64,13 @@ export default { link: `${v}${currentContentPath}` } }) + + const currentLangKey = currentLangBase ? currentLangBase : '/' + const selectText = siteData.value.locales[currentLangKey].selectText + ? siteData.value.locales[currentLangKey].selectText + : 'Languages' return { - // TODO i18n text - text: 'Languages', + text: selectText, items: candidates } }) diff --git a/types/shared.d.ts b/types/shared.d.ts index 850529b13494..08fb259a2609 100644 --- a/types/shared.d.ts +++ b/types/shared.d.ts @@ -2,6 +2,7 @@ export interface LocaleConfig { lang: string + selectText?: string label?: string title?: string description?: string From 4501465bf2f342cdfdcdfcdf6b3156bef43dedc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=80=89=E5=84=AA=E6=82=9F?= Date: Sun, 11 Oct 2020 18:58:15 +0900 Subject: [PATCH 4/6] fix: move language selector before repo link --- src/client/theme-default/components/NavBarLinks.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/theme-default/components/NavBarLinks.vue b/src/client/theme-default/components/NavBarLinks.vue index 95732c2ec429..9d1d5df22687 100644 --- a/src/client/theme-default/components/NavBarLinks.vue +++ b/src/client/theme-default/components/NavBarLinks.vue @@ -6,8 +6,8 @@ - + From 03264be895d6065f070f1384175510a28e4d945d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=80=89=E5=84=AA=E6=82=9F?= Date: Sun, 11 Oct 2020 19:29:51 +0900 Subject: [PATCH 5/6] fix: change reference of locales --- src/client/theme-default/components/NavBarLinks.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index 11e220d56849..88075bb5b971 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -43,7 +43,8 @@ export default { }) const localeCandidates = computed(() => { - const localeKeys = Object.keys(siteData.value.locales) + const locales = siteData.value.themeConfig.locales + const localeKeys = Object.keys(locales) if (localeKeys.length <= 1) { return null } @@ -59,15 +60,14 @@ export default { : route.path const candidates = localeKeys.map((v) => { return { - text: - siteData.value.locales[v].label || siteData.value.locales[v].lang, + text: locales[v].label || locales[v].lang, link: `${v}${currentContentPath}` } }) const currentLangKey = currentLangBase ? currentLangBase : '/' - const selectText = siteData.value.locales[currentLangKey].selectText - ? siteData.value.locales[currentLangKey].selectText + const selectText = locales[currentLangKey].selectText + ? locales[currentLangKey].selectText : 'Languages' return { text: selectText, From f0d2b7e121c1fc5d11ac1758a13b62e4a4365218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=80=89=E5=84=AA=E6=82=9F?= Date: Sun, 18 Oct 2020 15:53:31 +0900 Subject: [PATCH 6/6] fix: fix not to fail when no locales --- src/client/theme-default/components/NavBarLinks.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/client/theme-default/components/NavBarLinks.ts b/src/client/theme-default/components/NavBarLinks.ts index 88075bb5b971..d34b8c40cc1a 100644 --- a/src/client/theme-default/components/NavBarLinks.ts +++ b/src/client/theme-default/components/NavBarLinks.ts @@ -44,6 +44,9 @@ export default { const localeCandidates = computed(() => { const locales = siteData.value.themeConfig.locales + if (!locales) { + return null + } const localeKeys = Object.keys(locales) if (localeKeys.length <= 1) { return null