Skip to content

chore(feat): localize 404 page (fix #1987) #2004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
2 changes: 1 addition & 1 deletion packages/@vuepress/core/lib/node/ClientComputedMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = siteData => {
for (const path in locales) {
if (path === '/') {
defaultLang = locales[path]
} else if (this.$page.path.indexOf(path) === 0) {
} else if (this.$page.path.indexOf(path) === 0 || (!this.$page.path && this.$route.path.indexOf(path) === 0)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you trying to solve with this condition? When would this.$page.path not be defined?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dzetah ?

targetLang = locales[path]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,27 @@ module.exports = {
label: 'English',
selectText: 'Languages',
editLinkText: 'Edit this page on GitHub',
lastUpdated: 'Last Updated'
lastUpdated: 'Last Updated',
notFoundLinkText: 'Take me home.',
notFoundMessages: [
`There's nothing here.`,
`How did we get here?`,
`That's a Four-Oh-Four.`,
`Looks like we've got some broken links.`
]
},
'/zh/': {
label: '简体中文',
selectText: '选择语言',
editLinkText: '在 GitHub 上编辑此页',
lastUpdated: '上次更新',
notFoundLinkText: 'Take me home.',
notFoundMessages: [
`There's nothing here.`,
`How did we get here?`,
`That's a Four-Oh-Four.`,
`Looks like we've got some broken links.`
]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs translation.

}
}
}
Expand Down
18 changes: 16 additions & 2 deletions packages/@vuepress/test-utils/client/siteData.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,27 @@ export default {
"label": "English",
"selectText": "Languages",
"editLinkText": "Edit this page on GitHub",
"lastUpdated": "Last Updated"
"lastUpdated": "Last Updated",
"notFoundLinkText": "Take me home.",
"notFoundMessages": [
"There's nothing here.",
"How did we get here?",
"That's a Four-Oh-Four.",
"Looks like we've got some broken links."
]
},
"/zh/": {
"label": "简体中文",
"selectText": "选择语言",
"editLinkText": "在 GitHub 上编辑此页",
"lastUpdated": "上次更新"
"lastUpdated": "上次更新",
"notFoundLinkText": "Take me home.",
"notFoundMessages": [
"There's nothing here.",
"How did we get here?",
"That's a Four-Oh-Four.",
"Looks like we've got some broken links."
]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs translation.

}
}
},
Expand Down
37 changes: 28 additions & 9 deletions packages/@vuepress/theme-default/layouts/404.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,42 @@
<div class="theme-default-content">
<h1>404</h1>
<blockquote>{{ getMsg() }}</blockquote>
<router-link to="/">Take me home.</router-link>
<router-link :to="localePath">{{ notFoundLinkText }}</router-link>
</div>
</div>
</template>

<script>
const msgs = [
`There's nothing here.`,
`How did we get here?`,
`That's a Four-Oh-Four.`,
`Looks like we've got some broken links.`
]

export default {
computed: {
localePath () {
return this.$localePath || '/'
},
notFoundLinkText () {
return (
this.$themeLocaleConfig.notFoundLinkText
|| this.$site.themeConfig.notFoundLinkText
|| `Take me home.`
)
},
notFoundMessages () {
const messages = [
`There's nothing here.`,
`How did we get here?`,
`That's a Four-Oh-Four.`,
`Looks like we've got some broken links.`
]

return (
this.$themeLocaleConfig.notFoundMessages
|| this.$site.themeConfig.notFoundMessages
|| messages
)
}
},
methods: {
getMsg () {
return msgs[Math.floor(Math.random() * msgs.length)]
return this.notFoundMessages[Math.floor(Math.random() * this.notFoundMessages.length)]
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/docs/docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ module.exports = ctx => ({
selectText: 'Languages',
ariaLabel: 'Select language',
editLinkText: 'Edit this page on GitHub',
notFoundLinkText: 'Take me home.',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to set this for the / locale as these are the default values right?

notFoundMessages: [
`There's nothing here.`,
`How did we get here?`,
`That's a Four-Oh-Four.`,
`Looks like we've got some broken links.`
],
lastUpdated: 'Last Updated',
nav: require('./nav/en'),
sidebar: {
Expand All @@ -56,6 +63,13 @@ module.exports = ctx => ({
selectText: '选择语言',
ariaLabel: "选择语言",
editLinkText: '在 GitHub 上编辑此页',
notFoundLinkText: 'Take me home.',
notFoundMessages: [
`There's nothing here.`,
`How did we get here?`,
`That's a Four-Oh-Four.`,
`Looks like we've got some broken links.`
],
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs translation.

lastUpdated: '上次更新',
nav: require('./nav/zh'),
sidebar: {
Expand Down
5 changes: 5 additions & 0 deletions packages/docs/docs/guide/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ module.exports = {
ariaLabel: 'Languages'
// text for the edit-on-github link
editLinkText: 'Edit this page on GitHub',
// text for the 404 page link
notFoundLinkText: 'Take me home.',
// the list of random messages to show on the 404 page
notFoundMessages: [/* ... */],
// config for Service Worker
serviceWorker: {
updatePopup: {
Expand All @@ -79,6 +83,7 @@ module.exports = {
selectText: '选择语言',
label: '简体中文',
editLinkText: '在 GitHub 上编辑此页',
notFoundLinkText: 'Take me home.',
serviceWorker: {
updatePopup: {
message: "发现新内容可用.",
Expand Down
8 changes: 7 additions & 1 deletion packages/docs/docs/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,13 @@ module.exports = {
// default value is true. Allows to hide prev page links on all pages
prevLinks: false,
// custom text for edit link. Defaults to "Edit this page"
editLinkText: 'Help us improve this page!'
editLinkText: 'Help us improve this page!',

// Optional options fot the 404 page

// custom text for 404 page link. Defaults to "Take me home."
notFoundLinkText: 'Take me home.',
notFoundMessages: [/* ... */]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is important to write an example with the default values here, to let the user know what the default values are.

}
}
```
Expand Down
6 changes: 6 additions & 0 deletions packages/docs/docs/zh/guide/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ module.exports = {
label: 'English',
ariaLabel: 'Languages'
editLinkText: 'Edit this page on GitHub',
notFoundLinkText: 'Take me home.',
notFoundMessages: [/* ... */],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (to translate)

serviceWorker: {
updatePopup: {
message: "New content is available.",
Expand All @@ -76,6 +78,10 @@ module.exports = {
label: '简体中文',
// 编辑链接文字
editLinkText: '在 GitHub 上编辑此页',
// TODO: comment me
notFoundLinkText: 'Take me home.',
// TODO: comment me
notFoundMessages: [/* ... */],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (to translate)

// Service Worker 的配置
serviceWorker: {
updatePopup: {
Expand Down
6 changes: 5 additions & 1 deletion packages/docs/docs/zh/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,11 @@ module.exports = {
// 默认是 false, 设置为 true 来启用
editLinks: true,
// 默认为 "Edit this page"
editLinkText: '帮助我们改善此页面!'
editLinkText: '帮助我们改善此页面!',
// TODO: comment me
notFoundLinkText: 'Take me home.',
// TODO: comment me
notFoundMessages: [/* ... */]
}
}
```
Expand Down