Skip to content

Commit d666872

Browse files
committed
tweak: i18n config format
1 parent be42da5 commit d666872

File tree

6 files changed

+48
-44
lines changed

6 files changed

+48
-44
lines changed

docs/.vuepress/config.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
module.exports = {
22
dest: 'vuepress',
3-
langs: [
4-
{ lang: 'en', label: 'English', path: '/', selectText: 'Languages' },
5-
{ lang: 'zh-CN', label: '简体中文', path: '/zh/', selectText: '选择语言' }
6-
],
7-
title: {
8-
'/': 'VuePress',
9-
'/zh/': 'VuePress'
10-
},
11-
description: {
12-
'/': 'Vue-powered Static Site Generator',
13-
'/zh/': 'Vue 驱动的静态网站生成器'
3+
langs: {
4+
'/': {
5+
lang: 'en-US',
6+
label: 'English',
7+
selectText: 'Languages',
8+
title: 'VuePress',
9+
description: 'Vue-powered Static Site Generator'
10+
},
11+
'/zh/': {
12+
lang: 'zh-CN',
13+
label: '简体中文',
14+
selectText: '选择语言',
15+
title: 'VuePress',
16+
description: 'Vue 驱动的静态网站生成器'
17+
}
1418
},
1519
head: [
1620
['link', { rel: 'icon', href: `/logo.png` }],

lib/app/dataMixin.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ function prepare (siteData) {
88
page.frontmatter = {}
99
}
1010
})
11+
if (siteData.langs) {
12+
Object.keys(siteData.langs).forEach(path => {
13+
siteData.langs[path].path = path
14+
})
15+
}
1116
Object.freeze(siteData)
1217
}
1318

@@ -28,36 +33,30 @@ export default {
2833
$site () {
2934
return store.siteData
3035
},
31-
$title () {
32-
const title = this.$site.title
33-
return typeof title === 'object'
34-
? title[this.$basepath]
35-
: title
36-
},
37-
$description () {
38-
const description = this.$site.description
39-
return typeof description === 'object'
40-
? description[this.$basepath]
41-
: description
42-
},
4336
$langConfig () {
4437
const { langs } = this.$site
4538
let targetLang
4639
let defaultLang
47-
(langs || []).forEach((lang, index) => {
48-
if (lang.path === '/') {
49-
defaultLang = langs[index]
50-
} else if (this.$page.path.indexOf(lang.path) === 0) {
51-
targetLang = langs[index]
40+
Object.keys(langs).forEach(path => {
41+
if (path === '/') {
42+
defaultLang = langs[path]
43+
} else if (this.$page.path.indexOf(path) === 0) {
44+
targetLang = langs[path]
5245
}
5346
})
54-
return targetLang || defaultLang
47+
return targetLang || defaultLang || {}
48+
},
49+
$title () {
50+
return this.$langConfig.title || this.$site.title || ''
51+
},
52+
$description () {
53+
return this.$langConfig.description || this.$site.description || ''
5554
},
5655
$lang () {
57-
return this.$langConfig && this.$langConfig.lang || 'en'
56+
return this.$langConfig.lang || 'en-US'
5857
},
59-
$basepath () {
60-
return this.$langConfig && this.$langConfig.path || '/'
58+
$localePath () {
59+
return this.$langConfig.path || '/'
6160
},
6261
$page () {
6362
return findPageForPath(

lib/default-theme/Home.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
<div class="home">
33
<div class="hero">
44
<img v-if="data.heroImage" :src="$withBase(data.heroImage)" alt="hero">
5-
<h1>{{ data.heroText || $title }}</h1>
5+
<h1>{{ data.heroText || $title || 'Hello' }}</h1>
66
<p class="description">
7-
{{ data.tagline || $description }}
7+
{{ data.tagline || $description || 'Welcome to your VuePress site' }}
88
</p>
99
<p class="action" v-if="data.actionText && data.actionLink">
1010
<NavLink class="action-button" :item="actionLink"/>

lib/default-theme/NavLinks.vue

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,28 @@ export default {
3232
userNav () {
3333
const { nav } = this.$site.themeConfig
3434
if (Array.isArray(nav)) return nav
35-
if (typeof nav === 'object') return nav[this.$basepath]
35+
if (typeof nav === 'object') return nav[this.$localePath]
3636
return []
3737
},
3838
nav () {
39-
if (this.$site.langs && this.$site.langs.length) {
39+
if (this.$site.langs) {
4040
let currentLink = this.$page.path
4141
const routes = this.$router.options.routes
4242
const languageDropdown = {
4343
text: this.$langConfig.selectText,
44-
items: this.$site.langs.map(lang => {
45-
const text = lang.label
44+
items: Object.keys(this.$site.langs).map(path => {
45+
const locale = this.$site.langs[path]
46+
const text = locale.label
4647
let link
4748
// Stay on the current page
48-
if (lang.lang === this.$lang) {
49+
if (locale.lang === this.$lang) {
4950
link = currentLink
5051
} else {
5152
// Try to stay on the same page
52-
link = currentLink.replace(this.$langConfig.path, lang.path)
53+
link = currentLink.replace(this.$langConfig.path, path)
5354
// fallback to homepage
5455
if (!routes.some(route => route.path === link)) {
55-
link = lang.path
56+
link = path
5657
}
5758
}
5859
return { text, link }

lib/default-theme/Navbar.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<template>
22
<header class="navbar">
33
<SidebarButton @toggle-sidebar="$emit('toggle-sidebar')"/>
4-
<router-link :to="$basepath" class="home-link">
4+
<router-link :to="$localePath" class="home-link">
55
<img class="logo"
66
v-if="$site.themeConfig.logo"
77
:src="$withBase($site.themeConfig.logo)">
88
<span class="site-name"
9-
v-if="$site.title"
9+
v-if="$title"
1010
:class="{ 'can-hide': $site.themeConfig.logo }">
1111
{{ $title }}
1212
</span>

lib/default-theme/SearchBox.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default {
5353
5454
const max = 5
5555
const { pages } = this.$site
56-
const localePath = this.$basepath
56+
const localePath = this.$localePath
5757
const matches = item => (
5858
item.title &&
5959
item.title.toLowerCase().indexOf(query) > -1

0 commit comments

Comments
 (0)