Jeremyhuss.com

Here is a project that took a few days. It’s in English and Spanish.

Here’s a detailed step-by-step guide on how to set up and use the Python script to translate your Hugo website content to Spanish:

Step-by-Step Instructions

1. Set Up Your Google Cloud Project and Translation API

  1. Create a Google Cloud Project:
  • Go to the Google Cloud Console.
  • Create a new project or select an existing project.
  1. Enable the Google Cloud Translation API:
  • In the Google Cloud Console, navigate to APIs & Services > Library.
  • Search for “Cloud Translation API” and click on it.
  • Click Enable to enable the API for your project.
  1. Create a Service Account:
  • Go to APIs & Services > Credentials.
  • Click on Create Credentials and select Service account.
  • Fill in the details and click Create.
  • Assign the role Project > Editor and click Continue.
  • Click Done.
  1. Download the Service Account Key:
  • After creating the service account, click on it to open its details.
  • Go to the Keys tab and click Add Key > Create new key.
  • Select JSON and click Create. This will download the JSON file to your computer.

2. Set Up Your Python Environment

  1. Install Python: Ensure you have Python installed on your system. You can download it from python.org.
  2. Create a Virtual Environment: Open your terminal and navigate to your project directory. Run the following commands to create and activate a virtual environment:

bash

Copy code

python -m venv myenv
source myenv/bin/activate   # On Windows, use `myenv\Scripts\activate`
  1. Install Required Packages: Install the google-cloud-translate package using pip:

bash

Copy code

pip install google-cloud-translate==2.0.1

3. Set Up the Python Script

  1. Save the Script: Save the following Python script to a file named translate_hugo.py:

python

Copy code

import os
import re
from google.cloud import translate_v2 as translate
import html

# Set the correct path to the service account key file
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/your/service_account.json'

# Initialize the translator
translate_client = translate.Client()

# Path to your Hugo content directory
content_dir = 'path/to/your/hugo/content'
# Path to the translated content directory
translated_dir = 'path/to/your/hugo/content/es'

if not os.path.exists(translated_dir):
    os.makedirs(translated_dir)

# Function to translate text using Google Translate
def translate_text(text, target_lang='es'):
    result = translate_client.translate(text, target_language=target_lang)
    return html.unescape(result['translatedText'])

# Function to translate the front matter and content
def translate_content(file_path, target_lang='es'):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    
    # Split the front matter and content
    front_matter, main_content = re.split(r'\n---\n', content, maxsplit=1)

    # Translate the front matter (handle specific keys separately if needed)
    translated_front_matter = []
    for line in front_matter.splitlines():
        if line.startswith('title:'):
            key, value = line.split(':', 1)
            translated_title = translate_text(value.strip(), target_lang)
            translated_front_matter.append(f'{key}: {translated_title}')
        elif line.startswith('url:'):
            key, value = line.split(':', 1)
            translated_url = translate_text(value.strip(), target_lang).replace(' ', '-').lower()
            translated_front_matter.append(f'{key}: {translated_url}')
        elif line.startswith('description:'):
            key, value = line.split(':', 1)
            translated_description = translate_text(value.strip(), target_lang)
            translated_front_matter.append(f'{key}: {translated_description}')
        else:
            translated_front_matter.append(line)
    translated_front_matter = '\n'.join(translated_front_matter)
    
    # Translate the main content
    # Handle internal and external links properly
    def replace_link(match):
        text, url = match.groups()
        if url.startswith('http'):
            return f'[{text}]({url})'  # Do not translate external URLs
        else:
            translated_text = translate_text(text, target_lang)
            translated_url = translate_text(url, target_lang).replace(' ', '-').lower()
            return f'[{translated_text}]({translated_url})'
    
    # Handle links separately to preserve the original structure
    main_content = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', replace_link, main_content)
    
    # Translate the main content while preserving spacing
    translated_content_lines = []
    for line in main_content.splitlines():
        if line.strip() == "":
            translated_content_lines.append("")  # Preserve blank lines
        else:
            translated_line = translate_text(line, target_lang)
            translated_content_lines.append(translated_line)
    
    translated_content = "\n".join(translated_content_lines)
    
    # Combine translated front matter and content
    translated_full_content = f'{translated_front_matter}\n---\n{translated_content}'
    
    return translated_full_content

# Iterate over the files in the content directory
for root, dirs, files in os.walk(content_dir):
    for file in files:
        if file.endswith('.md'):  # Assuming Hugo content files are Markdown
            file_path = os.path.join(root, file)
            relative_path = os.path.relpath(file_path, content_dir)
            translated_file_path = os.path.join(translated_dir, relative_path)

            # Ensure the directory exists
            os.makedirs(os.path.dirname(translated_file_path), exist_ok=True)

            try:
                # Translate and save the content
                translated_content = translate_content(file_path)
                with open(translated_file_path, 'w', encoding='utf-8') as translated_file:
                    translated_file.write(translated_content)

                print(f'Translated {file_path} to {translated_file_path}')
            except Exception as e:
                print(f"Error translating {file_path}: {e}")
  1. Update the Script:
  • Replace 'path/to/your/service_account.json' with the actual path to your downloaded service account JSON file.
  • Replace 'path/to/your/hugo/content' with the path to your Hugo content directory.
  • Replace 'path/to/your/hugo/content/es' with the path where you want to save the translated content.

4. Run the Script

  1. Activate Your Virtual Environment:

bash

Copy code

source myenv/bin/activate   # On Windows, use `myenv\Scripts\activate`
  1. Run the Script:

bash

Copy code

python translate_hugo.py

Translate Navigation Menu

If you have a navigation menu file (e.g., config.toml), you can translate the menu items as well:

  1. Save the Menu Translation Script: Add the following function to your translate_hugo.py script to translate the menu items:

python

Copy code

# Function to translate the menu items
def translate_menu(file_path, target_lang='es'):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    
    translated_menu = []
    for line in content.splitlines():
        if line.startswith('name = '):
            key, value = line.split('=', 1)
            translated_name = translate_text(value.strip().strip('"'), target_lang)
            translated_menu.append(f'{key} = "{translated_name}"')
        elif line.startswith('url = '):
            key, value = line.split('=', 1)
            translated_url = translate_text(value.strip().strip('"'), target_lang).replace(' ', '-').lower()
            translated_menu.append(f'{key} = "{translated_url}"')
        else:
            translated_menu.append(line)
    
    return '\n'.join(translated_menu)

# Translate the menu file
menu_file_path = 'path/to/your/menu/file.toml'  # Update this path
translated_menu_path = 'path/to/your/menu/file_es.toml'  # Update this path
translated_menu_content = translate_menu(menu_file_path)

with open(translated_menu_path, 'w', encoding='utf-8') as translated_menu_file:
    translated_menu_file.write(translated_menu_content)

print(f'Translated menu file to {translated_menu_path}')
  1. Update the Paths:
  • Replace 'path/to/your/menu/file.toml' with the actual path to your menu file.
  • Replace 'path/to/your/menu/file_es.toml' with the path where you want to save the translated menu file.
  1. Run the Script Again:

bash

Copy code

python translate_hugo.py

By following these steps, you can automate the translation of your Hugo website’s Markdown content and navigation menu items into Spanish using the Google Cloud Translation API.

This script is by no means perfect. It still loops at the end but watch it build in visual studio and see when it repeats and then stop it. The translations are not perfect either. But it gives a great starting point. I wish i had this when i first started.

2 Likes