To create a blog that can be switched between English and Chinese, I researched various tutorials online. Most of them utilize the next theme or the hexo-generator-i18n plugin.
After some searching, I found a solution for bilingual switching by referring to this link.
The general idea involves creating two repositories on GitHub, one for each language, and setting up config.yml and _config.butterfly.yml for both the Chinese and English versions. Locally, two folders, source-en and source-zh, are manually translated into English and Chinese, respectively. Manual translation was chosen due to the unsatisfactory nature of most automatic translation APIs, and the fact that only GPT achieves relatively desirable results, but it’s a paid API.
# Add en url:https://your_url/en# Modify 'your_url' to replace it with your blog URL, e.g., https://aursus.github.io/en root:/en/
# Specify the startup path source_dir:source-en public_dir:public-en
# Exclude Chinese source include: exclude: ignore: -source/
# Modify repository name deploy: type:git repository:https://github.com/YourUserName/en.git# Replace this with your English repository name branch:main
STEP 3 - Setting up the language-switching JavaScript script
Modify btf.js in source/self, and add the following content. If there is no btf.js, create a new one in that location.
// Check if it's currently in English const nowIncludeEN = isIncludeEN(window.location.href)
// Modify your URL const selector = nowIncludeEN ? document.querySelectorAll('a[href^="https://<your-url>"]') // Only need to modify this line, replacing https://<your-url> with your blog URL. : document.querySelectorAll('a[href^="/en/"]')
eventFn(selector, nowIncludeEN) })()
STEP 4 - Copy the source folder
Duplicate the source folder and rename it as source-en. Ensure that both folders contain the btf.js file inside the source/self directory.
Modify the config-butterfly-zh.yml and config-butterfly-en.yml files according to the following code.
config-butterfly-zh.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Using Chinese menu menu: Home:/||fasfa-home 文章:/archives/||fasfa-archive 链接:/link/||fasfa-link 关于我:/about/||fasfa-heart 语言||fas fa-language: English:/en/||fasfa-e 中文:/||fasfa-c
# Using English menu menu: Home:/||fasfa-home Archives:/archives/||fasfa-archive Link:/link/||fasfa-link About:/about/||fasfa-heart Language||fas fa-language: English:/||fasfa-e 中文:https://aursus.github.io/||fasfa-c# change to the url of your blog
while [[ "$#" -gt 0 ]]; do case$1in -h|--help) echo"Usage: bash.sh [en|zh|all|show <en|zh>|-h]" echo"Options:" echo" [deploy|d] en Deploy English configuration to GitHub Pages" echo" [deploy|d] zh Deploy Chinese configuration to GitHub Pages" echo" [deploy|d] all Deploy both English and Chinese configurations to GitHub Pages" echo" [show|s] en Execute npm run show for English configuration" echo" [show|s] zh Execute npm run show for Chinese configuration" echo" -h, --help Display this help message" exit 0 ;; d | deploy) lang=$2 if [ "$lang" = "en" ] || [ "$lang" = "zh" ]; then cp"config-$lang.yml" _config.yml cp"config-butterfly-$lang.yml""_config.butterfly.yml" npm run kk echo"Deploy $1 success!" elif [ "$lang" = "all" ]; then for lang_choice in"zh""en"; do cp"config-$lang_choice.yml" _config.yml cp"config-butterfly-$lang_choice.yml""_config.butterfly.yml" npm run kk echo"Deploy $lang_choice success!" done else echo"Error! Please input 'en' or 'zh' or 'all'!" fi shift ;; s | show) lang=$2 if [ "$lang" = "en" ] || [ "$lang" = "zh" ]; then cp"config-$lang.yml" _config.yml cp"config-butterfly-$lang.yml""_config.butterfly.yml" npm run show echo"Running npm show!" else echo"Error! Please use './bash.sh show en' or './bash.sh show zh'!" fi shift# Move to the next argument after 'show' ;; *) echo"Error! Please input deploy <en|zh|all> or 'show <en|zh>' or '-h' for help!" exit 1 ;; esac shift done
Run the following commands in Git Bash:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Get help ./deploy.sh -h # This code is equivalent to the following ./deploy.sh --help
# Deploy English to the 'en' repository ./deploy.sh deploy en # This code is equivalent to the following ./deploy.sh d en
# Deploy Chinese to the 'zh' repository ./deploy.sh deploy zh # This code is equivalent to the following ./deploy.sh d zh
# Run locally in English ./deploy.sh show en # This code is equivalent to the following ./deploy.sh s en
# Run locally in Chinese ./deploy.sh show zh # This code is equivalent to the following ./deploy.sh s z
Page Language Translation
Modify config-butterfly.yml
Modify config-butterfly-zh.yml and config-butterfly-en.yml
config-butterfly-zh.yml
1 2 3 4 5 6 7 8 9 10 11 12
translate: enable:true # The text of a button default:中 # The language of the website (1 - Traditional Chinese / 2 - Simplified Chinese) defaultEncoding:1# Selecting anything doesn't affect the outcome # Time delay translateDelay:0# Selecting anything doesn't affect the outcome # The text of the button when the language is English msgToEnglish:'EN' # The text of the button when the language is Simplified Chinese msgToSimplifiedChinese:'中'
config-butterfly-en.yml
1 2 3 4 5 6 7 8 9 10 11 12
translate: enable:true # The text of a button default:EN # the language of website (1 - Traditional Chinese/ 2 - Simplified Chinese) defaultEncoding:1# Selecting anything doesn't affect the outcome # Time delay translateDelay:0# Selecting anything doesn't affect the outcome # The text of the button when the language is English msgToEnglish:'EN' # The text of the button when the language is Simplified Chinese msgToSimplifiedChinese:'中'
Create tw_en.js
Create files ch_en.js in both source/self/ and source-en/self/ directories.
Modify config-butterfly-zh.yml and config-butterfly-en.yml
config-butterfly-zh.yml
1 2 3 4 5 6
inject: head: ... bottom: -<scriptdata-pjaxsrc="/self/btf.js"></script> -<scriptdata-pjaxsrc="/self/ch_en.js"></script># <== here
config-butterfly-en.yml
1 2 3 4 5 6
inject: head: ... bottom: -<scriptdata-pjaxsrc="/en/self/btf.js"></script> -<scriptdata-pjaxsrc="/en/self/ch_en.js"></script># <== here
(Optional) Remove tw_cn.js
If the blog still displays with traditional and simplified Chinese conversion, delete the tw_cn.js file located at .\node_modules\hexo-theme-butterfly\source\js.
Reload
Enter the following command in Git Bash to refresh the webpage:
1
./deploy.sh deploy all
If there are any errors, please leave a comment for correction. Thank you.