Since ChatGPT 3.0, AI translation has swept the translation market with unprecedented accuracy. Since this blog once used English for writing, many articles have a mix of Chinese and English. Taking advantage of the upcoming price increase for Deepseek, I spent a few minutes translating all the articles on this blog into English. Additionally, by leveraging Github Actions, I achieved fully automated translation.
Pre-translation Preparation
- Confirm that your HUGO version and theme support i18n functionality
As long as your Hugo version isn’t particularly outdated (below 0.60.0), it should support this feature. Most mainstream Hugo themes also support i18n by default. You can check the theme’s yaml/toml file to see if there are multi-language configurations.
- Install Python
Download and install the Windows version from the Python official website.
- Install Necessary Dependencies
Use a command-line tool like Windows Terminal or Git Bash to install the requests
and openai
libraries. The former is used for sending HTTP requests, and the latter is for configuring the Deepseek API (Deepseek uses the standard OpenAI format).
|
|
- Apply for Deepseek API
Go to the Deepseek Open Platform to create an API KEY. Make sure to copy and save it when creating it. (Each new phone number registration comes with a 10 yuan balance. If not, you can top up 10 yuan yourself.)
Write and Run the Python Script
You can directly use my script. Save the following code as a translate.py
file, then run the command-line tool in the directory where the py file is saved, and enter python translate.py
to start the automatic translation. Usage instructions:
- Replace “sk-xxx” with your own Deepseek API KEY.
- Replace “D:/hugo/lawtee/content/” with your own Hugo article directory.
- Fixed translation mappings mainly solve potential semantic differences in article categories between Chinese and English. Use them according to your actual situation.
- MAX_TOKENS can be modified or left as is. If your articles are generally short, you can reduce it.
- The translation logic here is to traverse all folders in the Hugo content directory. For folders containing only
index.md
, translate it intoindex.en.md
. If bothindex.md
andindex.en.md
already exist, no translation is performed.
View Code
|
|
Tip
AI translation is generally not very fast, but Deepseek is currently the fastest. My 300+ articles with 800,000 Chinese characters took about five hours to translate, costing 1.7 yuan in API fees.
Hugo’s frontmatter section might fail to translate, especially at the junction between frontmatter and the main text. The translated result might lose the---
. It’s recommended to manually check after translation, as this issue might occur every few articles. You can also debug locally usinghugo server
to see if there are any errors.
Enable English Site
Refer to the Hugo theme configuration to enable it. For example, the settings for this site’s hugo.yaml are as follows:
languageCode
is set tozh-Hans
, indicating that the default language of the site is Chinese. For allindex.md
files, the default URL prefix ishttps://lawtee.com
. For allindex.en.md
files, the URL prefix ishttps://lawtee.com/en
. The same applies to other languages.
|
|
At this point, the local AI translation feature is set up. For future translations, simply run
python translate.py
locally to automatically translate newindex.md
files. If you find this cumbersome, you can refer to this article to set up a one-click automatic translation locally. Alternatively, you can use Github Actions’ powerful CI/CD tools to achieve automatic translation. The difference is that local translation results can be checked for bugs before submission, while automatic translation via Github Actions requires waiting for the build to complete to identify any bugs.
Set Up Github Actions for Automatic Translation
- Create a workflow file
translate.yml
in the Hugo repository, for example,D:/hugo/lawtee/.github/workflows/translate.yml
, to define the automation task. The content is as follows:
View Code
|
|
Add a repository secret in the GitHub repository settings:
Settings -> Secrets and variables -> Actions
. Name itDEEPSEEK_API_KEY
and enter your Deepseek API KEY as the content. (GitHub prohibits directly uploading API KEYS in files, so they must be added in the repository settings.) InSettings -> Actions -> general
, Set theWorkflow permissions
to read and write, allow GitHub Actions to create and approve pull requests.Push
translate.yml
andtranslate.py
to the GitHub repository.- Make sure to modify the
CONTENT_DIR = "D:/hugo/lawtee/content/posts"
intranslate.py
to the repository path. For example:CONTENT_DIR = "content/posts"
. - Also, change
API_KEY = "sk-xxx"
intranslate.py
toAPI_KEY = os.getenv("DEEPSEEK_API_KEY")
.
- Make sure to modify the
This way, GitHub Actions will automatically translate newly added index.md
documents into index.en.md
.
Tip
It is recommended to perform the initial AI batch translation locally for easier debugging. You can then use GitHub Actions for automatic translation later. During local testing, I found that batch translation occasionally causes errors in the frontmatter, but this issue has not been encountered when translating single articles.