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: add repository and link for external translations #4184

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
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
}>()

userquin marked this conversation as resolved.
Show resolved Hide resolved
const { page } = useData()
</script>

Expand Down
24 changes: 22 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)))
userquin marked this conversation as resolved.
Show resolved Hide resolved
</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,12 @@ const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
display: none;
}

.menu-item {
display: flex;
align-items: center;
justify-content: space-between;
}

@media (min-width: 1280px) {
.VPNavBarTranslations {
display: flex;
Expand Down
51 changes: 43 additions & 8 deletions src/client/theme-default/components/VPNavScreenTranslations.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script setup lang="ts">
import { ref } from 'vue'
import { computed, ref } from 'vue'
import { useLangs } from '../composables/langs'
import VPLink from './VPLink.vue'
import VPSocialLink from "./VPSocialLink.vue";

const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
const isOpen = ref(false)
const repo = computed(() => !!currentLang.value.repo || (localeLinks.value.length > 1 && localeLinks.value.some(l => !!l.repo)))

function toggle() {
isOpen.value = !isOpen.value
Expand All @@ -15,17 +17,26 @@ function toggle() {
<div
v-if="localeLinks.length && currentLang.label"
class="VPNavScreenTranslations"
:class="{ open: isOpen }"
:class="{ open: isOpen, repo }"
>
<button class="title" @click="toggle">
<span class="vpi-languages icon lang" />
{{ currentLang.label }}
<span class="vpi-chevron-down icon chevron" />
<button class="title" :class="{ repo: !!currentLang.repo }" @click="toggle">
<span v-if="currentLang.repo" class="repo">
<span class="vpi-languages icon lang" />
<span>{{ currentLang.label }}</span>
<span class="vpi-chevron-down icon chevron" />
</span>
<template v-else>
<span class="vpi-languages icon lang" />
<span>{{ currentLang.label }}</span>
<span class="vpi-chevron-down icon chevron" />
</template>
<VPSocialLink v-if="currentLang.repo" icon="github" :link="currentLang.repo.link" :ariaLabel="currentLang.repo.title" />
</button>

<ul class="list">
<li v-for="locale in localeLinks" :key="locale.link" class="item">
<li v-for="locale in localeLinks" :key="locale.link" :class="{ repo: !!locale.repo }" class="item">
<VPLink class="link" :href="locale.link">{{ locale.text }}</VPLink>
<VPSocialLink v-if="locale.repo" icon="github" :link="locale.repo.link" :ariaLabel="locale.repo.title" />
</li>
</ul>
</div>
Expand All @@ -37,16 +48,34 @@ function toggle() {
overflow: hidden;
}

.VPNavScreenTranslations.repo {
height: 40px;
}

.VPNavScreenTranslations.open {
height: auto;
}

.title {
.title, .title.repo .repo {
display: flex;
align-items: center;
font-size: 14px;
font-weight: 500;
color: var(--vp-c-text-1);

}

.VPNavScreenTranslations .title .vpi-chevron-down {
transition: transform 0.25s;
transform: rotate(90deg);
}
.VPNavScreenTranslations.open .title .vpi-chevron-down {
transform: rotate(-90deg);
}

.title.repo {
width: 100%;
justify-content: space-between;
}

.icon {
Expand All @@ -65,6 +94,12 @@ function toggle() {
padding: 4px 0 0 24px;
}

.list .item.repo {
display: flex;
align-items: center;
justify-content: space-between;
}

.link {
line-height: 32px;
font-size: 13px;
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