General Introduction
hugo-translator is an automated translation tool designed for Hugo's static site builder, hosted on GitHub and created by developer Rico00121. The tool is designed to help Hugo users quickly translate their blog content into multiple languages, utilizing the Google Translate API for efficient multilingual support. Whether you're an individual blogger or a webmaster needing to internationalize your content, this tool simplifies the translation process. It is open source, allowing users to freely download, modify and contribute code, which is ideal for the developer community in pursuit of efficiency and personalization. Currently, the project is still in the development stage, with relatively basic functions, but with a certain degree of practicality and potential.
Function List
- Automated translation of blog content: Translate Hugo's Markdown files (e.g. index.en.md) into the specified language.
- Support for Google Translate API: Fast, accurate content translation through Google's translation services.
- multilingual output: Currently hardcoded to support French, German, and Spanish, with more languages planned for the future.
- Document scanning and processing: Scans a specified directory and identifies and translates eligible Markdown files.
- Open Source and Extensible: The code is publicly available on GitHub, and users can develop it secondarily according to their needs.
Using Help
hugo-translator is a Go-based command line tool mainly used for translating Hugo blog content. Here is a detailed installation and usage guide to help you get started.
Installation process
- Preparing the environment
- Make sure you have the Go programming language installed on your computer (recommended version 1.16 or higher). This can be done with the command
go version
Check if the installation was successful. - You'll need a Google Cloud account with the Google Translate API enabled.Log in to the Google Cloud Console, create a project, enable the Translate API, and then generate the API key file (usually in JSON format, for example)
google-secret.json
).
- Make sure you have the Go programming language installed on your computer (recommended version 1.16 or higher). This can be done with the command
- Download Project Code
- Open a terminal and enter the following command to clone the project locally:
git clone https://github.com/Rico00121/hugo-translator.git
- Go to the project catalog:
cd hugo-translator
- Open a terminal and enter the following command to clone the project locally:
- Configuring the Google Translate API
- file the downloaded
google-secret.json
file in the hugo-translator project root directory. This is the credentials file for calling the Google Translate API, so make sure the path is correct and the filename has not changed.
- file the downloaded
- compilation program
- Run the following command in the project directory to get the dependencies and compile the executable:
go get go build translate.go
- When compilation is complete, a file called
translate
executable (Windowstranslate.exe
).
- Run the following command in the project directory to get the dependencies and compile the executable:
- Verify Installation
- importation
. /translate -h
(Windowstranslate.exe -h
), if the help message is displayed, the installation was successful.
- importation
Usage
The core function of hugo-translator is to translate English Markdown files from Hugo blog to other languages. Here are the steps to do it:
Prepare Hugo blog files
- Make sure that your Hugo blog content starts with
index.en.md
Format names to indicate English content. Example:
content/posts/my-blog/index.en.md
- The content of the file should be in standard Markdown format, containing Hugo's front matter (e.g., title, date, etc.) and body text.
Run the translation command
- Basic Translation Operations
- In the terminal, run the following command to specify the path of the file to be translated:
. /translate
Example:
. /translate content/posts/my-blog/index.en.md
- The current version automatically translates the file into French (fr), German (de) and Spanish (es) and generates the corresponding file, e.g.
index.fr.md
,index.de.md
,index.es.md
The
- Checking the output file
- When the translation is complete, the new language file appears in the same directory as the original file. Example:
content/posts/my-blog/ index.en.md index.fr.md index.de.md ├── index.es.md
- Open these files and check the translation results. the front matter section usually remains unchanged, and the body text is translated into the target language.
caveat
- File naming conventions: The tool currently only recognizes
index.en.md
format, other naming conventions (e.g.index.md
) is not supported at this time. - Translation restrictions: Currently the language support is hard-coded (French, German, Spanish) and it is not possible to specify other languages via the command line. Language selection may be added in future versions.
- network requirement: Internet access is required to run, as translation relies on the Google Translate API.
- error detection: If the translation fails, check the
google-secret.json
Are they placed correctly, or verify that the network connection is working properly.
Featured Function Operation
Automated Multi-Language Support
- The highlight of hugo-translator is that it generates multiple language versions of a file at once. For example, if you enter a blog file in English, it will automatically generate three language translations, saving you the trouble of manually translating them one by one.
- Example of operation:
. /translate content/posts/my-blog/index.en.md
- Output results:
index.fr.md
: French versionindex.de.md
: German-language versionindex.es.md
: Spanish version
Open Source Extensions
- If you need to translate another language, you can modify the source code. For example, open the
translate.go
file, find the translation logic section (usually involvingtranslateTextWithModel
function), add the target language code (e.g."it"
(Indicates Italian). - Modify and recompile:
go build translate.go
- This flexibility is ideal for users with programming skills.
Example of usage scenarios
Let's say you have a blog post index.en.md
The content is as follows:
---
title: "My First Blog"
date: 2023-01-01
---
Hello, this is my first blog post!
Run command:
. /translate content/posts/my-blog/index.en.md
After translation.index.fr.md
It may be as follows:
---
title: "My First Blog"
date: 2023-01-01
--- --- --- --- --- --- --- --- --- --- --- ---
Salut, ceci est mon premier article de blog !
The other language files are similar and can be used directly in the Hugo project to implement a multilingual blog.
Directions for future improvement
- The current version is simple, developer Rico00121 mentioned on GitHub about planned enhancements such as support for specified languages, batch processing of all files in a directory, and more. Users can follow the project updates or participate in contributing.
With the above steps, you can easily use hugo-translator to translate your blog content into multilingual versions, improving the accessibility and internationalization of your website.