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
pull/99/head
Yugo Ogura 4 years ago committed by GitHub
parent 2eb31358bf
commit 294836ce40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
}
}
}

@ -6,6 +6,7 @@
<NavBarLink v-else :item="item" />
</template>
</template>
<NavDropdownLink v-if="localeCandidates" :item="localeCandidates" />
<NavBarLink v-if="repoInfo" :item="repoInfo" />
</nav>
</template>

2
types/shared.d.ts vendored

@ -2,6 +2,8 @@
export interface LocaleConfig {
lang: string
selectText?: string
label?: string
title?: string
description?: string
head?: HeadConfig[]

Loading…
Cancel
Save