Skip to content

Commit

Permalink
feat: add repository and link for external translations
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Sep 6, 2024
1 parent 6b4439a commit d4938bc
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
8 changes: 8 additions & 0 deletions __tests__/e2e/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ export default defineConfig({
lazyLoading: true
}
},
locales: {
root: { label: 'English', repo: 'https://github.com/vuejs/vitepress' },
zh: {
label: '简体中文',
link: 'https://vitepress.dev/zh/',
repo: 'https://github.com/vitejs/vite'
}
},
themeConfig: {
nav,
sidebar,
Expand Down
4 changes: 4 additions & 0 deletions src/client/theme-default/components/VPMenuLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ defineProps<{
item: DefaultTheme.NavItemWithLink
}>()
defineSlots<{
default: () => void
}>()
const { page } = useData()
</script>

Expand Down
25 changes: 23 additions & 2 deletions src/client/theme-default/components/VPNavBarTranslations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import VPFlyout from './VPFlyout.vue'
import VPMenuLink from './VPMenuLink.vue'
import { useData } from '../composables/data'
import { useLangs } from '../composables/langs'
import VPSocialLink from "./VPSocialLink.vue";
// import { computed } from 'vue'
const { theme } = useData()
const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
// for helping translate
// const repoLink = computed(() => currentLang.value.repo || (localeLinks.value.length > 1 && localeLinks.value.some(l => !!l.repo)))
</script>

<template>
Expand All @@ -16,10 +20,20 @@ const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
:label="theme.langMenuLabel || 'Change language'"
>
<div class="items">
<p class="title">{{ currentLang.label }}</p>
<div v-if="currentLang.repo">
<div class="menu-item">
<p class="title">{{ currentLang.label }}</p>
<VPSocialLink icon="github" :link="currentLang.repo.link" :ariaLabel="currentLang.repo.title" />
</div>
</div>
<p v-else class="title">{{ currentLang.label }}</p>

<template v-for="locale in localeLinks" :key="locale.link">
<VPMenuLink :item="locale" />
<div v-if="locale.repo" class="menu-item">
<VPMenuLink :item="locale" />
<VPSocialLink icon="github" :link="locale.repo.link" :ariaLabel="locale.repo.title" />
</div>
<VPMenuLink v-else :item="locale" />
</template>
</div>
</VPFlyout>
Expand All @@ -30,6 +44,13 @@ const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
display: none;
}
.menu-item {
display: flex;
align-items: center;
justify-content: space-between;
position: relative;
}
@media (min-width: 1280px) {
.VPNavBarTranslations {
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import VPLink from './VPLink.vue'
const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
const isOpen = ref(false)
// for helping translate
// const repoLink = computed(() => currentLang.value.repo || (localeLinks.value.length > 1 && localeLinks.value.some(l => !!l.repo)))
function toggle() {
isOpen.value = !isOpen.value
Expand Down
37 changes: 31 additions & 6 deletions src/client/theme-default/composables/langs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,44 @@ import { useData } from './data'

export function useLangs({ correspondingLink = false } = {}) {
const { site, localeIndex, page, theme, hash } = useData()
const currentLang = computed(() => ({
label: site.value.locales[localeIndex.value]?.label,
link:
site.value.locales[localeIndex.value]?.link ||
(localeIndex.value === 'root' ? '/' : `/${localeIndex.value}/`)
}))
const currentLang = computed(() => {
const lang = site.value.locales[localeIndex.value]
return {
label: lang?.label,
link:
lang?.link || localeIndex.value === 'root'
? '/'
: `/${localeIndex.value}/`,
repo: lang?.repo
? {
link: typeof lang.repo === 'string' ? lang.repo : lang.repo.link,
title:
typeof lang.repo === 'string'
? `${lang.label} repository`
: lang.repo.title
}
: undefined
}
})

const localeLinks = computed(() =>
Object.entries(site.value.locales).flatMap(([key, value]) =>
currentLang.value.label === value.label
? []
: {
text: value.label,
repo: value.repo
? {
link:
typeof value.repo === 'string'
? value.repo
: value.repo.link,
title:
typeof value.repo === 'string'
? `${value.label} repository`
: value.repo.title
}
: undefined,
link:
normalizeLink(
value.link || (key === 'root' ? '/' : `/${key}/`),
Expand Down
11 changes: 10 additions & 1 deletion types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,16 @@ export interface LocaleSpecificConfig<ThemeConfig = any> {

export type LocaleConfig<ThemeConfig = any> = Record<
string,
LocaleSpecificConfig<ThemeConfig> & { label: string; link?: string }
LocaleSpecificConfig<ThemeConfig> & {
label: string
link?: string
repo?:
| string
| {
link: string
title: string
}
}
>

// Manually declaring all properties as rollup-plugin-dts
Expand Down

0 comments on commit d4938bc

Please sign in to comment.