国际化
要使用内置的 i18n 功能,需要创建以下目录结构
docs/
├─ es/
│ ├─ foo.md
├─ fr/
│ ├─ foo.md
├─ foo.md然后在 docs/.vitepress/config.ts 中
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...
}
}
})以下属性可以为每个语言环境(包括根语言环境)覆盖
ts
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 来持久保存用户的语言选择
ts
// docs/.vitepress/theme/index.ts
import DefaultTheme from 'vitepress/theme'
import Layout from './Layout.vue'
export default {
extends: DefaultTheme,
Layout
}vue
<!-- docs/.vitepress/theme/Layout.vue -->
<script setup lang="ts">
import DefaultTheme from 'vitepress/theme'
import { useData } from 'vitepress'
import { watchEffect } from 'vue'
const { lang } = useData()
watchEffect(() => {
if (inBrowser) {
document.cookie = `nf_lang=${lang.value}; expires=Mon, 1 Jan 2030 00:00:00 UTC; path=/`
}
})
</script>
<template>
<DefaultTheme.Layout />
</template>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 特定性问题。