URL-Based I18n Solution
要利用Saber中的多语言支持,首先需要在pages文件夹中使用以下文件结构:
pages/
├─ index.md
├─ foo.md
├─ nested
│ └─ index.md
├─ cn
│ ├─ index.md
│ ├─ foo.md
│ └─ nested
│ └─ index.md
└─ fr
├─ index.md
├─ foo.md
└─ nested
└─ index.md
这样,中文版本的网站就在路径 /cn下生成了,类似地,法国版本在路径为 /fr下生效。
siteConfig:
lang: en
title: My Site
locales:
/cn:
siteConfig:
lang: zh
title: 我的网站
/fr:
siteConfig:
lang: fr
title: Mon Site
siteConfig.lang选项被用来设置 lang 属性给 ==<html>==元素。
locale 选项用于覆盖特定路径的 siteConfig 和 themeConfig。例如,当您访问主页 / 或 /about 页面时,this.$siteConfig.title 组件中将是 My Site ,而如果您访问 /cn 或 /cn/about,则将改为 我的网站。
Access Locale Path in the Component
在你的组件中使用 this.$localepath 发现当前页面的环境,使用 saber-config.yml 我们在上面用到的,当你访问页面的 /fr/about时, this.$localepath 将是 ‘/fr’ ;或者当你访问 /about时,为 ‘/’ 。