Skip to content

Commit

Permalink
🌐 feat: Add improved i18n support for language selection (#1)
Browse files Browse the repository at this point in the history
Add language-configurable titles and labels following the Vuepress default theme style: https://github.com/vuejs/vuepress/blob/master/docs/guide/i18n.md
  • Loading branch information
siddv authored and linhe0x0 committed Jul 22, 2018
1 parent 5ab1111 commit 3c376bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
14 changes: 13 additions & 1 deletion docs/zh/configurations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ enhance: true

本站使用的配置:

```js{6-19}
```js{6-31}
module.exports = {
title: 'Hello, World.',
description: '📦 🎨 A api-friendly theme for VuePress.',
Expand All @@ -373,6 +373,18 @@ module.exports = {
description: '📦 🎨 一个面向 RESTful API 设计的开箱即用主题。',
},
},
themeConfig: {
locales: {
'/': {
selectText: 'Languages',
label: 'English',
},
'/zh/': {
selectText: '选择语言',
label: '简体中文',
}
}
}
}
```
Expand Down
20 changes: 17 additions & 3 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,23 @@ export function matchLocalePathFromPath(path, locales) {
export function resolveSidebarItems($page, $site, $localePath) {
const { themeConfig } = $site
const sidebars = {}
let languageSelectText

if ($site.locales) {
sidebars['other-languages'] = {
title: 'other languages',
let localeTheme = {};

if (themeConfig.locales) {
localeTheme = themeConfig.locales[$localePath]
}

languageSelectText = localeTheme.selectText || 'other-languages'

sidebars[languageSelectText] = {
title: languageSelectText,
children: Object.keys($site.locales).map(locale => {
const item = $site.locales[locale]
let path
let languageTitle = item.text || item.lang

if (item.path === $localePath) {
path = $page.path // Stay on the current page
Expand All @@ -92,8 +102,12 @@ export function resolveSidebarItems($page, $site, $localePath) {
}
}

if(themeConfig.locales && themeConfig.locales[locale] && themeConfig.locales[locale].label) {
languageTitle = themeConfig.locales[locale].label
}

return {
title: item.text || item.lang,
title: languageTitle,
to: path,
isLangNav: true,
}
Expand Down

0 comments on commit 3c376bf

Please sign in to comment.