Skip to content
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

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

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: [
sybers marked this conversation as resolved.
Show resolved Hide resolved
`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: '返回首页',
notFoundMessages: [
sybers marked this conversation as resolved.
Show resolved Hide resolved
'这里什么都没有。',
'你怎么到这来了?',
'这是一个 404 页面。',
'看起来这是一个错误的链接。'
]
}
}
}
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": "返回首页",
"notFoundMessages": [
"这里什么都没有。",
"你怎么到这来了?",
"这是一个 404 页面。",
"看起来这是一个错误的链接。"
]
}
}
},
Expand Down
47 changes: 34 additions & 13 deletions packages/@vuepress/theme-default/layouts/404.vue
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
<template>
<div class="theme-container">
<div class="theme-default-content">
<h1>404</h1>
<ClientOnly>
<h1>404</h1>

<blockquote>{{ getMsg() }}</blockquote>
<blockquote>{{ getMsg() }}</blockquote>

<RouterLink to="/">
Take me home.
</RouterLink>
<RouterLink :to="localePath">
{{ notFoundLinkText }}
</RouterLink>
</ClientOnly>
</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 = [
sybers marked this conversation as resolved.
Show resolved Hide resolved
`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: [
sybers marked this conversation as resolved.
Show resolved Hide resolved
`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: '返回首页',
notFoundMessages: [
sybers marked this conversation as resolved.
Show resolved Hide resolved
'这里什么都没有。',
'你怎么到这来了?',
'这是一个 404 页面。',
'看起来这是一个错误的链接。'
],
lastUpdated: '上次更新',
nav: require('./nav/zh'),
sidebar: {
Expand Down
16 changes: 16 additions & 0 deletions packages/docs/docs/guide/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ 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: [
`There's nothing here.`,
`How did we get here?`,
`That's a Four-Oh-Four.`,
`Looks like we've got some broken links.`
],
// config for Service Worker
serviceWorker: {
updatePopup: {
Expand All @@ -79,6 +88,13 @@ module.exports = {
selectText: '选择语言',
label: '简体中文',
editLinkText: '在 GitHub 上编辑此页',
notFoundLinkText: '返回首页',
notFoundMessages: [
'这里什么都没有。',
'你怎么到这来了?',
'这是一个 404 页面。',
'看起来这是一个错误的链接。'
],
serviceWorker: {
updatePopup: {
message: "发现新内容可用.",
Expand Down
16 changes: 16 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,13 @@ module.exports = {
label: 'English',
ariaLabel: 'Languages'
editLinkText: 'Edit this page on 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.`
],
serviceWorker: {
updatePopup: {
message: "New content is available.",
Expand All @@ -76,6 +83,15 @@ module.exports = {
label: '简体中文',
// 编辑链接文字
editLinkText: '在 GitHub 上编辑此页',
// 404 页面返回首页链接文字
notFoundLinkText: '返回首页',
// 404 页面提示信息
notFoundMessages: [
'这里什么都没有。',
'你怎么到这来了?',
'这是一个 404 页面。',
'看起来这是一个错误的链接。'
],
// Service Worker 的配置
serviceWorker: {
updatePopup: {
Expand Down