Skip to content

Commit 3c376bf

Browse files
siddvlinhe0x0
authored andcommitted
🌐 feat: Add improved i18n support for language selection (#1)
Add language-configurable titles and labels following the Vuepress default theme style: https://github.com/vuejs/vuepress/blob/master/docs/guide/i18n.md
1 parent 5ab1111 commit 3c376bf

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

docs/zh/configurations/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ enhance: true
353353

354354
本站使用的配置:
355355

356-
```js{6-19}
356+
```js{6-31}
357357
module.exports = {
358358
title: 'Hello, World.',
359359
description: '📦 🎨 A api-friendly theme for VuePress.',
@@ -373,6 +373,18 @@ module.exports = {
373373
description: '📦 🎨 一个面向 RESTful API 设计的开箱即用主题。',
374374
},
375375
},
376+
themeConfig: {
377+
locales: {
378+
'/': {
379+
selectText: 'Languages',
380+
label: 'English',
381+
},
382+
'/zh/': {
383+
selectText: '选择语言',
384+
label: '简体中文',
385+
}
386+
}
387+
}
376388
}
377389
378390
```

utils.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,23 @@ export function matchLocalePathFromPath(path, locales) {
7272
export function resolveSidebarItems($page, $site, $localePath) {
7373
const { themeConfig } = $site
7474
const sidebars = {}
75+
let languageSelectText
7576

7677
if ($site.locales) {
77-
sidebars['other-languages'] = {
78-
title: 'other languages',
78+
let localeTheme = {};
79+
80+
if (themeConfig.locales) {
81+
localeTheme = themeConfig.locales[$localePath]
82+
}
83+
84+
languageSelectText = localeTheme.selectText || 'other-languages'
85+
86+
sidebars[languageSelectText] = {
87+
title: languageSelectText,
7988
children: Object.keys($site.locales).map(locale => {
8089
const item = $site.locales[locale]
8190
let path
91+
let languageTitle = item.text || item.lang
8292

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

105+
if(themeConfig.locales && themeConfig.locales[locale] && themeConfig.locales[locale].label) {
106+
languageTitle = themeConfig.locales[locale].label
107+
}
108+
95109
return {
96-
title: item.text || item.lang,
110+
title: languageTitle,
97111
to: path,
98112
isLangNav: true,
99113
}

0 commit comments

Comments
 (0)