Introduction
Once we have created our own blog, we would like it to support multiple languages. This not only expands our audience but also enhances the SEO performance of the blog. However, manually translating blog posts is a time-consuming and labor-intensive task, requiring professional translators. But with the development of AI, things are different now; using AI, you can easily translate your blog into any desired language. To facilitate this, I created a tool that utilizes AI to automate the translation of your blog posts, allowing you to effortlessly support multiple languages.
You can find the tool at hugo-translator.
Preparation
Before getting started, you need to prepare the following:
- A running Hugo blog
- Node.js and npm environment
- OpenAI API key (for AI translation)
- Basic command line operation knowledge
Implementation Steps
1. Obtain the Tool
git clone https://github.com/lxb1226/hugo-translator.git
cd hugo-translator
2. Install AI Translation Tool
We will use the ai-markdown-translator
tool to translate Markdown files. First, install it globally:
npm install -g ai-markdown-translator
3. Configure Environment Variables
Set the OpenAI API key:
export OPENAI_API_KEY='your-api-key'
If you do not have an OpenAI API key, you can also use a third-party API. You can purchase a third-party API key through this link. Afterwards, you can set:
export OPENAI_URL='your api url'
export API_KEY='your-api-key'
You can also add this configuration to your .bashrc
or .zshrc
file for it to take effect permanently.
4. Create the Translation Script
Create a script file named translate-posts.sh
to automate the translation process. This script will:
- Automatically detect blog posts
- Support multi-language translation
- Skip already translated posts
- Provide detailed translation progress and statistics
Key features include:
- Multi-language Support: Default support for multiple languages, including English, Japanese, and Korean.
- Intelligent Detection: Automatically identify source and target languages.
- Incremental Updates: Only translate new or changed content.
- Error Handling: Comprehensive error handling and logging.
- Progress Display: Real-time display of translation progress and status.
5. Usage
Basic usage:
./translate-posts.sh
Custom target languages:
TARGET_LANGS="en ja ko" ./translate-posts.sh
6. File Naming Rules
The translated files will be automatically named according to Hugo’s internationalization naming convention:
- English version:
post-name.en.md
- Japanese version:
post-name.ja.md
- Korean version:
post-name.ko.md
Comments