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
30 changes: 30 additions & 0 deletions __tests__/e2e/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@ export default defineConfig({
lazyLoading: true
}
},
locales: {
root: {
label: 'English',
link: 'https://vitepress.dev',
repository: {
link: 'https://github.com/vuejs/vitepress',
title: 'English Repository'
}
},
es: {
label: 'Español',
link: 'https://vitepress.dev/es/',
repository: {
// for testing purposes
link: 'https://github.com/vuejs/core',
icon: 'gitlab',
title: 'Repositorio en Español'
}
},
zh: {
label: '简体中文',
link: 'https://vitepress.dev/zh/',
repository: {
// for testing purposes
link: 'https://github.com/vitejs/vite',
icon: 'bitbucket',
title: '中文存储库'
}
}
},
themeConfig: {
nav,
sidebar,
Expand Down
29 changes: 27 additions & 2 deletions src/client/theme-default/components/VPNavBarTranslations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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";

const { theme } = useData()
const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
Expand All @@ -16,10 +17,28 @@ const { localeLinks, currentLang } = useLangs({ correspondingLink: true })
:label="theme.langMenuLabel || 'Change language'"
>
<div class="items">
<p class="title">{{ currentLang.label }}</p>
<div v-if="currentLang.repository">
<div class="menu-item">
<p class="title">{{ currentLang.label }}</p>
<VPSocialLink
:icon="currentLang.repository.icon ?? 'github'"
:link="currentLang.repository.link"
:ariaLabel="currentLang.repository.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.repository" class="menu-item">
<VPMenuLink :item="locale" />
<VPSocialLink
:icon="locale.repository.icon ?? 'github'"
:link="locale.repository.link"
:ariaLabel="locale.repository.title"
/>
</div>
<VPMenuLink v-else :item="locale" />
</template>
</div>
</VPFlyout>
Expand All @@ -30,6 +49,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
67 changes: 59 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.repository || (localeLinks.value.length > 1 && localeLinks.value.some(l => !!l.repository)))
userquin marked this conversation as resolved.
Show resolved Hide resolved

function toggle() {
isOpen.value = !isOpen.value
Expand All @@ -15,17 +17,41 @@ 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.repository }" @click="toggle">
<span v-if="currentLang.repository" 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.repository"
:icon="currentLang.repository.icon ?? 'github'"
:link="currentLang.repository.link"
:ariaLabel="currentLang.repository.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.repository }"
class="item"
>
<VPLink class="link" :href="locale.link">{{ locale.text }}</VPLink>
<VPSocialLink
v-if="locale.repository"
:icon="locale.repository.icon ?? 'github'"
:link="locale.repository.link"
:ariaLabel="locale.repository.title"
/>
</li>
</ul>
</div>
Expand All @@ -37,16 +63,35 @@ 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);

}

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


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

.icon {
Expand All @@ -65,6 +110,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
18 changes: 12 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,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,
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,
repository: lang?.repository,
link:
lang?.link || localeIndex.value === 'root'
? '/'
: `/${localeIndex.value}/`
}
})

const localeLinks = computed(() =>
Object.entries(site.value.locales).flatMap(([key, value]) =>
currentLang.value.label === value.label
? []
: {
text: value.label,
repository: value.repository,
link:
normalizeLink(
value.link || (key === 'root' ? '/' : `/${key}/`),
Expand Down
8 changes: 8 additions & 0 deletions src/client/theme-default/styles/icons.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions types/default-theme.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,11 @@ export namespace DefaultTheme {
}

export type SocialLinkIcon =
| 'bitbucket'
| 'discord'
| 'facebook'
| 'github'
| 'gitlab'
| 'instagram'
| 'linkedin'
| 'mastodon'
Expand Down
17 changes: 16 additions & 1 deletion types/shared.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,24 @@ export interface LocaleSpecificConfig<ThemeConfig = any> {
themeConfig?: ThemeConfig
}

export type RepositoryLinkIcon = 'github' | 'gitlab' | 'bitbucket'

export type LocaleConfig<ThemeConfig = any> = Record<
string,
LocaleSpecificConfig<ThemeConfig> & { label: string; link?: string }
LocaleSpecificConfig<ThemeConfig> & {
label: string
link?: string
repository?: {
link: string
title: string
/** @default 'github' */
icon?: RepositoryLinkIcon
help?: {
link: string
text: string
}
}
}
>

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