本项目已经可以切换中英文,做法跟 Angular-iSharkFly-Com 一样:中文页面还在原来的路径,英文页面放在 /en/ 下面。 #14

Merged
honeymoose merged 1 commits from feature/2026 into main 2026-12-04 12:39:05 -05:00
6 changed files with 526 additions and 104 deletions
+236 -104
View File
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,72 @@
<script setup lang="ts">
import VPFlyout from 'vitepress/dist/client/theme-default/components/VPFlyout.vue';
import { VPLink } from 'vitepress/theme';
import { useLanguageSwitcher } from '../languageSwitcher';
const { readingLanguages, labels } = useLanguageSwitcher();
</script>
<template>
<VPFlyout class="NavBarLanguageSwitcher" icon="vpi-languages" :label="labels.menu">
<div class="items">
<p class="title">{{ labels.reading }}</p>
<VPLink
v-for="item in readingLanguages"
:key="item.text"
class="link"
:class="{ active: item.active }"
:href="item.link"
:lang="item.lang"
:hreflang="item.lang"
rel="alternate"
>{{ item.text }}</VPLink>
</div>
</VPFlyout>
</template>
<style scoped>
.NavBarLanguageSwitcher {
display: none;
}
/* 从 768px 起显示:默认主题“…”菜单里的翻译分组已在 style.css 中隐藏,由本组件代替。 */
@media (min-width: 768px) {
.NavBarLanguageSwitcher {
display: flex;
align-items: center;
}
}
.items {
min-width: 180px;
}
.title {
padding: 0 12px;
line-height: 32px;
font-size: 12px;
font-weight: 600;
color: var(--vp-c-text-2);
}
.link {
display: block;
border-radius: 6px;
padding: 0 12px;
line-height: 32px;
font-size: 14px;
font-weight: 500;
color: var(--vp-c-text-1);
white-space: nowrap;
transition: background-color 0.25s, color 0.25s;
}
.link:hover {
color: var(--vp-c-brand-1);
background-color: var(--vp-c-default-soft);
}
.link.active {
color: var(--vp-c-brand-1);
}
</style>
@@ -0,0 +1,85 @@
<script setup lang="ts">
import { ref } from 'vue';
import { VPLink } from 'vitepress/theme';
import { useLanguageSwitcher } from '../languageSwitcher';
const { readingLanguages, labels, currentLabel } = useLanguageSwitcher();
const isOpen = ref(false);
</script>
<template>
<div class="NavScreenLanguageSwitcher" :class="{ open: isOpen }">
<button class="toggle" :aria-expanded="isOpen" @click="isOpen = !isOpen">
<span class="vpi-languages icon lang" />
{{ currentLabel }}
<span class="vpi-chevron-down icon chevron" />
</button>
<div class="groups">
<p class="title">{{ labels.reading }}</p>
<ul class="list">
<li v-for="item in readingLanguages" :key="item.text">
<VPLink class="link" :class="{ active: item.active }" :href="item.link" :lang="item.lang">
{{ item.text }}
</VPLink>
</li>
</ul>
</div>
</div>
</template>
<style scoped>
.NavScreenLanguageSwitcher {
height: 24px;
overflow: hidden;
}
.NavScreenLanguageSwitcher.open {
height: auto;
}
.toggle {
display: flex;
align-items: center;
font-size: 14px;
font-weight: 500;
color: var(--vp-c-text-1);
}
.icon {
font-size: 16px;
}
.icon.lang {
margin-right: 8px;
}
.icon.chevron {
margin-left: 4px;
}
.groups {
padding: 4px 0 0 24px;
}
.title {
margin-top: 8px;
font-size: 12px;
font-weight: 600;
color: var(--vp-c-text-2);
}
.list {
padding-left: 8px;
}
.link {
line-height: 32px;
font-size: 13px;
color: var(--vp-c-text-1);
}
.link.active {
color: var(--vp-c-brand-1);
}
</style>
+62
View File
@@ -0,0 +1,62 @@
import { computed } from 'vue';
import { useData } from 'vitepress';
type Reading = 'zh' | 'en';
type SwitcherConfig = {
enPrefix: string;
// 每种语言已有的页面(相对该语言根目录的 .md 路径),用来判断切换后对应页面是否存在。
pages: Record<Reading, string[]>;
};
export type SwitcherLink = { text: string; link: string; lang?: string; active: boolean };
const READING_LANGUAGES = [
{ key: 'zh', text: '简体中文', lang: 'zh-CN' },
{ key: 'en', text: 'English', lang: 'en-US' },
] as const;
function pageUrl(base: string, page: string) {
return base + page.replace(/(^|\/)index\.md$/, '$1').replace(/\.md$/, '');
}
export function useLanguageSwitcher() {
const { page, theme, hash } = useData();
const config = computed(() => theme.value.languageSwitcher as SwitcherConfig);
const current = computed(() => {
let rest = page.value.relativePath;
const enDir = config.value.enPrefix.replace(/^\//, '') + '/';
const reading: Reading = rest.startsWith(enDir) ? 'en' : 'zh';
if (reading === 'en')
rest = rest.slice(enDir.length);
return { reading, page: rest };
});
// 对应语言有同一页面就跳到该页面,否则跳到该语言的首页。
function linkFor(reading: Reading, keepHash: boolean) {
const base = reading === 'en' ? config.value.enPrefix + '/' : '/';
const pages = config.value.pages[reading] ?? [];
if (!pages.includes(current.value.page))
return base;
return pageUrl(base, current.value.page) + (keepHash ? hash.value : '');
}
const isZh = computed(() => current.value.reading === 'zh');
const readingLanguages = computed<SwitcherLink[]>(() => READING_LANGUAGES.map(reading => ({
text: reading.text,
lang: reading.lang,
link: linkFor(reading.key, true),
active: reading.key === current.value.reading,
})));
const labels = computed(() => isZh.value
? { menu: '切换语言', reading: '阅读语言' }
: { menu: 'Change language', reading: 'Language' });
const currentLabel = computed(() =>
READING_LANGUAGES.find(reading => reading.key === current.value.reading)!.text);
return { readingLanguages, labels, currentLabel, isZh };
}
+10
View File
@@ -137,3 +137,13 @@
--docsearch-primary-color: var(--vp-c-brand-1) !important;
}
/**
* Language switcher
* 默认主题“…”菜单里的翻译分组会直接链接到另一语言的同名页面(英文页面多数还没有,会 404),
* 这里把它隐藏,改由 NavBarLanguageSwitcher 显示。
* -------------------------------------------------------------------------- */
.VPNavBarExtra .group.translations {
display: none;
}
+61
View File
@@ -0,0 +1,61 @@
---
# https://vitepress.dev/reference/default-theme-home-page
layout: home
hero:
name: "iSharkFly Docs"
text: "Docs"
tagline: Welcome to iSharkFly's documentation and handbooks
actions:
- theme: brand
text: Programming Languages
link: /programming-language/index
- theme: alt
text: Frameworks & Libraries
link: /framework
- theme: alt
text: Products
link: /product
- theme: alt
text: DevOps
link: /dev-ops/
- theme: alt
text: Career & Interviews
link: /work
- theme: alt
text: Culture
link: /misc/culture
- theme: alt
text: Essays
link: /misc/essay
features:
- title: iSharkFly Docs Project
details: Notes, handbooks and tutorials on software development, collected by iSharkFly.
- title: Built with VitePress
details: The site is generated with VitePress and deployed on Cloudflare Workers.
- title: Quick Navigation
details: See the quick navigation links at the bottom of this page for related sites.
link: /navigation
---
::: info
Most articles are currently available in Chinese only. Links on this page open the Chinese version.
:::
## Contact
| Channel | Contact |
|---------|---------|
| Email | [[email protected]](mailto:[email protected]) |
| QQ / WeChat | 103899765 |
| Forum | https://www.isharkfly.com/ |
## Quick Navigation
| Site | URL | Note |
|------|-----|------|
| DOCS.ISHARKFLY.COM | [https://docs.isharkfly.com/](https://docs.isharkfly.com/) | This handbook |
| iSharkFly | https://www.isharkfly.com/c/software-development/5 | Front-end frameworks and software development discussions |
| TypeScript | https://typescript.ossez.com/ | TypeScript documentation and handbook |
| Mumps | https://mumps.sharkfly.com/ | Mumps documentation and handbook |