Hexo Next 7.7 Bilingual Switching
One year ago, through frantic searching and experimentation on Google, I managed to implement bilingual switching (between Chinese and English) on the 7.1 version of the Next theme. However, it came with numerous bugs (such as switching to the English interface and then clicking on tags like "About" or "Archives" would redirect back to the Chinese interface). Given my procrastination tendencies and advanced laziness, I never got around to fixing it. Recently, I took the opportunity during an upgrade of the Next theme to address this issue. After two days of tinkering, I finally succeeded in achieving the desired bilingual switching effect, even to the extent that it distracted me from my undergraduate thesis project.
This post was translated from my Chinese blog post with the aid of ChatGpt.
Time Reminder
This article was written in March 2020! Please be aware of its age, as some of the content may have become outdated! For the latest solutions, please check out Hexo Next 8 Bilingual Switching.
Principle
My initial idea was to create a new repository on GitHub and then enable GitHub Pages for that repository to use it as an English website. This way, the Chinese and English websites could exist independently without interfering with each other, and I would only need to add a toggle button to both websites. However, upon further investigation, I discovered that each GitHub account can only have one personal homepage. This presented a dilemma. Did I need to register another account just for the English website? That seemed overly complicated. Just when I was feeling stuck, I came across an article by chitanda titled Issues Related to Adding Multiple GitHub Pages Under a Single GitHub Account.
According to the official documentation, GitHub Pages can be classified into two types: User/Organization Pages and Project Pages. There are a few key differences between them (for the purposes of this article, we’ll refer to user and organization pages as user pages):
- User pages must have the same name as the user’s GitHub account. So, each user can have only one repository as their user page, and it must be in the format
/ .github.io. In contrast, project pages don’t have this restriction, and you can have an arbitrary number of them. - Without considering custom domains, the GitHub subdomain for user pages is
.github.io, whereas project pages use .github.io/ . There is no . .github.io format. - User pages display content from the master branch, while project pages use content from the gh-pages branch.
With this information, if I create a new repository on GitHub and deploy the English website using the project pages method, the Chinese and English websites should still remain independent of each other. The English website’s domain format would be ChineseDomain/RepositoryName/
, and I wouldn’t need to separately configure a new domain. It sounds promising, so let’s give it a try.
Steps
Here, I’ll use my own domain name siriusq.top
and the English website repository name en
as an example.
Create a Project Page
- Create a new repository named
en
on GitHub. - Go to the newly created repository and click on
Settings
. - Scroll down to the
Pages
section. - Click on
Choose a theme
, and then select any theme. - After successfully creating the project page, the
Pages
section will displayYour site is published at https://siriusq.top/en/
. - With this, the project page for the English website is successfully created.
Set Up the Local English Website Directory
Copy the current Chinese blog folder and rename it as desired. Here, I’ll use my own Chinese folder name Hexo
and the duplicated English folder name Hexoen
as an example.
- Open the
./Hexoen/_config.yml
file and update the configurations as follows:1
2
3
4language: en
url: https://siriusq.top/en/ # Replace siriusq.top/en/ with your own domain/repository name/
root: /en/ # Replace en with your own repository name
repo: git@github.com:Siriusq/en.git # Replace Siriusq/en with your own username/repository name - Delete the
CNAME
file (if it exists) under./Hexoen/source
.
Add the Chinese Switch Button
- Open the
./Hexoen/source/_data/next.yml
file.- Under
Menu Settings
, addswitch_lang
tomenu:
as follows:1
2
3
4
5
6
7
8
9
10menu:
home: / || home
about: /about/ || user
tags: /tags/ || tags
categories: /categories/ || th
archives: /archives/ || archive
switch_lang: https://siriusq.top || language # Replace siriusq.top with your Chinese website domain
#schedule: /schedule/ || calendar
#sitemap: /sitemap.xml || sitemap
#commonweal: /404/ || heartbeat
- Under
- Open the
./Hexoen/themes/next/languages/en.yml
file.- Under
Menu Settings
, addswitch_lang: 简体中文
tomenu:
as follows:1
2
3
4
5
6
7
8
9
10
11menu:
home: Home
archives: Archives
categories: Categories
tags: Tags
about: About
switch_lang: 简体中文
search: Search
schedule: Schedule
sitemap: Sitemap
commonweal: Commonweal 404
- Under
Add the English Switch Button
- Open the
./Hexo/source/_data/next.yml
file.- Under
Menu Settings
, addswitch_lang
tomenu:
as follows:1
2
3
4
5
6
7
8
9
10menu:
home: / || home
about: /about/ || user
tags: /tags/ || tags
categories: /categories/ || th
archives: /archives/ || archive
switch_lang: https://siriusq.top/en/ || language # Replace siriusq.top/en/ with your English website domain
#schedule: /schedule/ || calendar
#sitemap: /sitemap.xml || sitemap
#commonweal: /404/ || heartbeat
- Under
- Open the
./Hexoen/themes/next/languages/en.yml
file.- Under
Menu Settings
, addswitch_lang: 简体中文
tomenu:
as follows:1
2
3
4
5
6
7
8
9
10
11menu:
home: 首页
archives: 归档
categories: 分类
tags: 标签
about: 关于
switch_lang: English
search: 搜索
schedule: 日程表
sitemap: 站点地图
commonweal: 公益 404
- Under
Add Switching Conditions
Chinese Website
Open ./Hexo/themes/next/layout/_partials/head/head-unique.swig
and paste the following code:
1 | <script type="text/javascript"> |
English Website
Open ./Hexoen/themes/next/layout/_partials/head/head-unique.swig
and paste the following code:
1 | <script type="text/javascript"> |
Deployment
Execute the Hexo triple command in both folders to deploy the website to Github. After a while, you will be able to access it.
1 | hexo cl |
Additional Notes
- Since the two websites are independent of each other, theoretically, using different themes for both websites is also possible. I only modified the 404 page for the English website.
- One minor issue: When PJAX is enabled, clicking the switch button before the page is fully loaded may cause a slight delay before the redirection.
- Another minor issue: Since the websites are completely independent, the pages are fully reloaded when switching, and PJAX does not work.
- Note: When writing blog posts, keep the Markdown file names consistent between the Chinese and English directories; otherwise, switching may lead to a 404 page.
It indeed takes a lot of time to tinker with these things. My undergraduate thesis hasn’t progressed at all, and I’m feeling quite anxious now. If you have any questions, feel free to leave a comment in the comment section, although I might not be able to respond promptly.
Without further ado, I’m rolling up my sleeves to work on my thesis!
ε=ε=ε=ε=ε=ε=┌(; ̄◇ ̄)┘