国际化
要使用内置的 i18n 功能,需要创建如下目录结构:
docs/
├─ es/
│ ├─ foo.md
├─ fr/
│ ├─ foo.md
├─ foo.md然后在docs/.vitepress/config.ts中:
import { defineConfig } from 'vitepress'
export default defineConfig({
// shared properties and other top-level stuff...
locales: {
root: {
label: 'English',
lang: 'en'
},
fr: {
label: 'French',
lang: 'fr', // optional, will be added as `lang` attribute on `html` tag
link: '/fr/guide' // default /fr/ -- shows on navbar translations menu, can be external
// other locale specific properties...
}
}
})可以为每个区域设置(包括根)覆盖以下属性:
interface LocaleSpecificConfig<ThemeConfig = any> {
lang?: string
dir?: string
title?: string
titleTemplate?: string | boolean
description?: string
head?: HeadConfig[] // will be merged with existing head entries, duplicate meta tags are automatically removed
themeConfig?: ThemeConfig // will be shallow merged, common stuff can be put in top-level themeConfig entry
}有关自定义默认主题占位符文本的详细信息,请参阅 DefaultTheme.Config 接口。不要在语言环境级别覆盖 themeConfig.algolia 或 themeConfig.carbonAds。请参阅 Algolia 文档 以了解如何使用多语言搜索。
专业提示: 配置文件也可以存储在 docs/.vitepress/config/index.ts 中。它可以帮助您通过为每个语言环境创建一个配置文件来组织内容,然后合并并从index.ts导出它们。
每个语言环境的单独目录
下面是一个完美的精细结构:
docs/
├─ en/
│ ├─ foo.md
├─ es/
│ ├─ foo.md
├─ fr/
├─ foo.md但是,VitePress 默认不会将 / 重定向到 /en/。您需要为此配置您的服务器。例如,在 Netlify 上,您可以添加一个 docs/public/_redirects 文件,如下所示:
/* /es/:splat 302 Language=es
/* /fr/:splat 302 Language=fr
/* /en/:splat 302专业提示: 如果使用上述方法,您可以使用 nf_lang cookie 来保留用户的语言选择。一个非常基本的方法是在自定义主题的 setup 函数中注册一个观察者:
// docs/.vitepress/theme/index.ts
import DefaultTheme from 'vitepress/theme'
export default {
...DefaultTheme,
setup() {
const { lang } = useData()
watchEffect(() => {
if (inBrowser) {
document.cookie = `nf_lang=${lang.value}; expires=Mon, 1 Jan 2024 00:00:00 UTC; path=/`
}
})
}
}RTL 支持(实验性)
对于 RTL 支持,请在配置中指定 dir: 'rtl' 并使用一些 RTLCSS PostCSS 插件,例如 https://github.com/MohammadYounes/rtlcss、https://github.com/vkalinichev/postcss-rtl或https://github.com/elchininet/postcss-rtlcss。您需要将 PostCSS 插件配置为使用 :where([dir="ltr"]) 和 :where([dir="rtl"]) 作为前缀,以防止 CSS 特异性问题。