The bilingualism config process for Hexo is recorded In this article. Different language version is in the same application with common template, any webpage can be switched to another language's corresponding webpage, and for the access url whose language is not specific, automatic redirection will be proceed according to browser language
Fulfillment Rule
Address Difference
Chinese Home:
English Home:
Switch between Languages
The following Chinese page as example
Click English
in the top right corner, the following webpage will be shown
Click 中文
in the top right corner in this webpage, the previous webpage will been shown
Automatic Redirection
The following address as example
If the browser prefer language is set to English, it will redirect to corresponding English version
If the browser prefer language is set to Chinese, it will redirect to corresponding Chinese version
Hexo Setting
Add English Setting
Add _config-en.yml
in the root directory
# Site
title: TITLE
subtitle: SUBTITLE
description: DESCRIPTION
keywords: KEYWORDS
language: en
# URL
url: https://chanvinxiao.com/en/blog
root: /en/blog/
# Directory
source_dir: source-en
public_dir: public-en
- in the
#Site
associated setting, I change the original Chinese content to English, and the key point is to changelanguage
toen
, thus the template will use english version -
URL
androot
need to be set as individual address and directory to distinguish between chinese counterpart - Divide
source
andpublic
directory from chinese, to ensure the Chinese or English version only show Chinese or English article respectively
Add Script
Add the following script in package.json
"scripts": {
...
"build:en": "hexo generate --config _config.yml,_config-en.yml",
"clean:en": "hexo clean --config _config.yml,_config-en.yml",
"server:en": "hexo server --config _config.yml,_config-en.yml"
},
- Add build, clear, and server script for English version, so the Chinese and English version is separated without interfering with each other
- Utilize Custom Config, combine config
_config.yml
and_config-en.yml
in the corresponding scripts - The combining config file
_multiconfig.yml
will be generated, which should be added to .gitignore
Nginx Config
Add the following config in corresponding server
session in Nginx
if ( $http_accept_language ~* ^en ) {
rewrite ^(/blog.*) /en$1 redirect;
}
rewrite ^(/blog.*) /cn$1 redirect;
location /cn/blog {
alias /PATH/TO/BLOG/public;
error_page 404 $scheme://$host/cn/blog;
}
location /en/blog {
alias /PATH/TO/BLOG/public-en;
error_page 404 $scheme://$host/en/blog;
}
-
$http_accept_language
is embedded variable set by Nginx'shttp
module for request headerAccept-Language
, If prefer language of browser is English, the value will start withen
, such asen-US,en;q=0.9
-
rewrite ^(/blog.*) /en$1 redirect;
will add en for the address start with /blog, the flag ofrewrite
is set toredirect
, which means 302 redirect, so does the following default cn - The above setting make redirect decision for address start with
/blog
(aka address without language), If the prefer language of browser is English, English site start with/en/blog
will be redirected, or else Chinese version start with/cn/blog
will be redirected - Because /cn/blog is matched with index.html in the public directory, not cn/blog/index.html, so here
alias
is used, notroot
-
error_page
is set for 404 handling,$scheme
ishttp
orhttps
, which means webpage redirect, and will redirect to Chinese or English homepage
Webpage Switch
Use template landscape as example, add the following content before })(jQuery);
in themes/landscape/source/js/script.js
let language = {};
language.now = location.pathname.match(/^\/en/) ? 'en' : 'cn';
if('en' === language.now){
language.label = '中文';
language.href = location.pathname.replace(/^\/en/, '/cn');
}else{
language.label = 'English';
language.href = location.pathname.replace(/^\/cn/, '/en');
}
$('#sub-nav').prepend(`<a class="main-nav-link" href="${language.href}">${language.label}</a>`)
- Determine the language of current webpage according to the existence of
/en
in the beginning of webpage path - Add link menu to Chinese webpage in English webpage, and add English link in Chinese version
- Directly change
cn
toen
oren
tocn
in the address to get the corresponding webpage, if there is not corresponding webpage, the associated homepage will be redirected according to the above Nginx config - Utilize
prepend
ofjQuery
to add link to the sub menu, with the common classmain-nav-link
style
Summarize
In the fulfillment of blog bilingualism, the following technology is primarily used:
- Custom config of hexo and
scripts
in package.json - Embedded variable for request header in
http
module of Nginx - Nginx's directive
rewrite
,alias
anderror_page
-
pathname
of location andprepend
of jQuery
Top comments (0)