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:

  1. A running Hugo blog
  2. Node.js and npm environment
  3. OpenAI API key (for AI translation)
  4. Basic command line operation knowledge

Implementation Steps

1. Obtain the Tool

BASH
git clone https://github.com/lxb1226/hugo-translator.git
cd hugo-translator
Click to expand and view more

2. Install AI Translation Tool

We will use the ai-markdown-translator tool to translate Markdown files. First, install it globally:

BASH
npm install -g ai-markdown-translator
Click to expand and view more

3. Configure Environment Variables

Set the OpenAI API key:

BASH
export OPENAI_API_KEY='your-api-key'
Click to expand and view more

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:

BASH
export OPENAI_URL='your api url'
export API_KEY='your-api-key'
Click to expand and view more

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:

Key features include:

  1. Multi-language Support: Default support for multiple languages, including English, Japanese, and Korean.
  2. Intelligent Detection: Automatically identify source and target languages.
  3. Incremental Updates: Only translate new or changed content.
  4. Error Handling: Comprehensive error handling and logging.
  5. Progress Display: Real-time display of translation progress and status.

5. Usage

Basic usage:

BASH
./translate-posts.sh
Click to expand and view more

Custom target languages:

BASH
TARGET_LANGS="en ja ko" ./translate-posts.sh
Click to expand and view more

6. File Naming Rules

The translated files will be automatically named according to Hugo’s internationalization naming convention:

References

Copyright Notice

Author: heyjude

Link: https://heyjude.blog/posts/how-to-translate-hugo-blog-with-ai/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Comments

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut