Forum Discussion

Perez's avatar
Perez
Iron Contributor
Oct 29, 2025

How can I translate html files to another language?

Hi,

Our website only has the English version and we need to add a few languages, such as Japanese, French and German. It is a huge and time consuming task if we do this manually, either with a human translate or online service like Google Translate. The website has dozen of html pages and the online service usually translate the html tags and css style. This will make the html pages looking awful.

Is there any efficient and accurate way to transfer html files to another language from English?

Best regards.

Perez

6 Replies

  • Brookesnoe's avatar
    Brookesnoe
    Iron Contributor

    For transferring html files to another language, pls also try the Copilot tool provided by microsoft.

  • Using Googletrans for translating HTML files to another language can be an efficient and free way to handle translation, especially for small to medium-sized files. However, there are some important considerations to keep in mind:

    How Googletrans Works

    • Googletrans is a free, unofficial Python library that uses Google Translate's backend.
    • It allows you to programmatically translate text, making it suitable for automating the translation of content extracted from HTML files.

     

    Steps to Use Googletrans for Translating HTML Files to another Language

    1. Install Googletrans: pip install googletrans==4.0.0-rc1
    2. Parse the HTML and Extract Text: 
      Use BeautifulSoup to parse the HTML.
      Be careful to exclude non-translatable parts like <style>, <script>, <meta>, etc.
    3. Translate Extracted Text: 
       Use Googletrans to translate the text.
       Replace the original text with the translated version.
    4. Reconstruct the HTML:
       Save the modified HTML content back to a file.

     

    Example Code Snippet

    from bs4 import BeautifulSoup
    from googletrans import Translator

    # Initialize translator
    translator = Translator()

    # Load your HTML file
    with open('yourfile.html', 'r', encoding='utf-8') as f:
        soup = BeautifulSoup(f, 'html.parser')

    # Iterate over all text nodes
    for element in soup.find_all(string=True):
        parent = element.parent.name
        # Skip non-translatable tags
        if parent in ['style', 'script', 'meta', 'head', 'title']:
            continue
        try:
            # Translate the text
            translated = translator.translate(element, dest='es')  # Change 'es' to your target language code
            element.replace_with(translated.text)
        except Exception as e:
            print(f"Error translating: {element}")
            continue

    # Save the translated HTML
    with open('translated.html', 'w', encoding='utf-8') as f:
        f.write(str(soup))

  • OhioValley's avatar
    OhioValley
    Iron Contributor

    Using the DeepL API to translate HTML files to another language involves a few key steps: extracting the translatable content, sending it to DeepL for translation, and then reinserting the translated content into your HTML files. Here's a step-by-step guide:

    1. Get DeepL API Access
    2. Extract Text Content from HTML Files
        Use a script (e.g., Python with BeautifulSoup) to parse your HTML files and extract only the translatable          text, leaving HTML tags intact.
    3. Use DeepL API to Translate Text
       Send the extracted text to DeepL's translation API.
       Handle the response to get the translated text.
    4. Reintegrate Translated Text into HTML
       Replace the original text with the translated text in your HTML structure.
       Translate HTML files to another language and save the updated HTML files.

    DeepL excels with HTML because:

    • Preserves HTML tags and attributes during translation
    • Handles structural elements (divs, spans, paragraphs) properly
    • Maintains links, images, and formatting
    • Can ignore specific content (like code blocks) when configured
  • JoyceBeatty's avatar
    JoyceBeatty
    Iron Contributor

    Translating html files to another language efficiently and accurately can be challenging, but there are several strategies and tools to streamline the process while preserving your HTML structure and avoiding style issues. Here are some recommended approaches:

    1. Extract Text Content from HTML Files

    • Use tools or scripts to parse HTML and extract only the translatable text, excluding tags, CSS, and scripts.
    • This prevents translation tools from modifying HTML markup or styles.

    2. Use Professional Translation Management Systems (TMS)

    • Platforms like Crowdin, Transifex, or Phrase allow you to upload source files, manage translations, and then export translated content back into your website structure.
    • They support multiple formats, including HTML, and help maintain translation consistency.

    3. Employ Localization Files

    • Convert your website content into resource files (e.g., JSON, PO, or XML) that contain only key-value pairs of text.
    • Translate html files to another language using translation services or human translators.
    • Reintegrate the translated text into your HTML templates.
  • Matuo's avatar
    Matuo
    Copper Contributor

    Automated Translation with Google Translate API. 

    The Google Cloud Translation API is a powerful cloud-based service that uses Google's neural machine translation technology to dynamically translate text between thousands of language pairs. When it comes to HTML content, this API offers specialized capabilities that make it particularly valuable for web developers and content managers.

    Setup required:

    • Google Cloud account
    • Translation API enabled
    • API credentials

    Best for: Large-scale translation, dynamic content

    Transfer html files to another language with python

    from google.cloud import translate_v2 as translate
    import html
    
    def translate_html_file(input_file, output_file, target_language='es'):
        client = translate.Client()
        
        with open(input_file, 'r', encoding='utf-8') as f:
            html_content = f.read()
        
        # Translate the content (Google Translate handles HTML tags well)
        result = client.translate(html_content, target_language=target_language)
        
        translated_content = result['translatedText']
        
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write(translated_content)
    
    # Usage
    translate_html_file('index.html', 'index_es.html', 'es')

     

  • Nayaoh's avatar
    Nayaoh
    Iron Contributor

    Implement internationalization (i18n) using specialized libraries that separate content from structure.The term i18n is an abbreviation for "internationalization" - where "i" and "n" are the first and last letters, and "18" represents the 18 letters in between.

    i18n Libraries are software tools designed to help developers internationalize their applications - meaning to prepare them for translation into different languages and adaptation to various regions without engineering changes.

    Popular i18n Libraries for translating html files to another language:

    1. i18next (JavaScript)

    import i18next from 'i18next';
    
    i18next.init({
      lng: 'es',
      resources: {
        en: { translation: { welcome: "Welcome" } },
        es: { translation: { welcome: "Bienvenido" } }
      }
    });
    
    console.log(i18next.t('welcome')); // "Bienvenido"

    2. react-i18next (React)

    import { useTranslation } from 'react-i18next';
    
    function MyComponent() {
      const { t } = useTranslation();
      
      return (
        <div>
          <h1>{t('welcome.title')}</h1>
          <p>{t('welcome.subtitle')}</p>
        </div>
      );
    }

    i18n libraries are essential for building truly global applications that can seamlessly adapt to different languages, cultures, and regions while maintaining clean, maintainable code. This is one of the best ways to transfer html files to another language.

Resources