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

feat(i18n): nav dropdown language selector #91

Merged
merged 7 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
43 changes: 41 additions & 2 deletions src/client/theme-default/components/NavBarLinks.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand All @@ -40,14 +41,52 @@ export default {
}
return null
})

const localeCandidates = computed(() => {
const locales = siteData.value.themeConfig.locales
if (!locales) {
return null
}
const localeKeys = Object.keys(locales)
kiaking marked this conversation as resolved.
Show resolved Hide resolved
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'
? // navbar items do not change in production
siteDataByRoute.value.themeConfig.nav
: // use computed in dev for hot reload
computed(() => siteDataByRoute.value.themeConfig.nav),
repoInfo
repoInfo,
localeCandidates
}
}
}
1 change: 1 addition & 0 deletions src/client/theme-default/components/NavBarLinks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<NavBarLink v-else :item="item" />
</template>
</template>
<NavDropdownLink v-if="localeCandidates" :item="localeCandidates" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets move this one above the GitHub repo nav 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved!

<NavBarLink v-if="repoInfo" :item="repoInfo" />
</nav>
</template>
Expand Down
2 changes: 2 additions & 0 deletions types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

export interface LocaleConfig {
lang: string
selectText?: string
label?: string
title?: string
description?: string
head?: HeadConfig[]
Expand Down