feat: add repository and link for external translations

userquin/feat-add-external-translation-repo-links
userquin 1 year ago
parent 6b4439a9e6
commit d4938bc28a

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

@ -8,6 +8,10 @@ defineProps<{
item: DefaultTheme.NavItemWithLink
}>()
defineSlots<{
default: () => void
}>()
const { page } = useData()
</script>

@ -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>
@ -16,10 +20,20 @@ const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
:label="theme.langMenuLabel || 'Change language'"
>
<div class="items">
<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">
<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>
@ -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;

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

@ -4,12 +4,25 @@ 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,
const currentLang = computed(() => {
const lang = site.value.locales[localeIndex.value]
return {
label: lang?.label,
link:
site.value.locales[localeIndex.value]?.link ||
(localeIndex.value === 'root' ? '/' : `/${localeIndex.value}/`)
}))
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]) =>
@ -17,6 +30,18 @@ export function useLangs({ correspondingLink = false } = {}) {
? []
: {
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}/`),

11
types/shared.d.ts vendored

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

Loading…
Cancel
Save