Customize 404 page
Support custom 404 page (the page displayed when accessing the wrong URL), and support 404 page internationalization
Add 404 error page
Set the mapping between the url
of the website root directory and the path in the site_config
, such as
"route": {
"pages": {
"/": "pages/index/zh"
}
},
"translate": {
"pages": {
"/": [{
"url": "/en/",
"src": "pages/index/en"
}
]
}
}
Here the website root directory (/
) is mapped to the folder pages/index/zh
, you only need to create the file pages/index/zh/404.md
, and then add the content:
---
layout: 404.html
---
or
---
layout: 404
---
Users will return to this page when they visit the wrong path
404 Error Page Internationalization
The website root directory configuration mentioned above, if the locale
is configured as zh
in the config
file under pages/index/zh
, the content of the generated /404.html
will be Chinese
If the user's browser language is set in other languages, such as English en
, it will try to jump to /en/404.html
, so we only need to:
- Configure the path mapping of
translate
insite_config
, as shown in the example above - Then create a new
pages/index/en/404.md
, add content
---
layout: 404.html
---
If
locale
has a suffix, such aszh_CN
,en_US
, etc., the404
page will automatically try to jump to the page in the same language as the browser setting, such as/en_US/404.html
,/en -US/404.html
,/en-us/404.html
,/en_us/404.html
stop redirecting until the page language and browser language are the same. When all links are tried to redirect It also stops jumping after failure
If the language you need has not been translated, there are two ways:
- Can be achieved through [Custom 404 page] (#Customize-404-error-page-content)
- You can also contribute translation, go to here,
fork
repository, and thengit clone
To your warehouse locally, add a new language to thelocales.cfg
file, and then executepython trans_prepare.py
, which will generate a new translation file in thelocales
directory with the suffix.po
, translate This file (to translate thepo
file, you can directly modify the text, or use other tools to translate), and then executepython trans_finish.py
will generate amo
binary file, you can submit the changes without errors, and then ingithub
CreatePR(Pull Request)
to contribute translation toteedoc
repository
Customize 404 error page content
Modify the template directly based on the theme, or inherit the template of the theme, such as slightly changing the body
part of the 404.html
page, just create a new my_404.html
in the layout
directory. Note that the file name cannot be the same Built-in file name conflict, if conflict, it will prompt generate html fail: maximum recursion depth exceeded in comparison
:
{% extends "404.html" %}
{% block body_404 %}
Here is body
{{ body|safe }}
{% endblock%}
Then use this template in 404.md
---
layout: my_404.html
---