How to translate HTML file locally

321 views Asked by At

I have an HTML file that contains some text in English, and I want to translate it into another language. I'm using the google-cloud-translate library in Python, and I have already set up my authentication credentials using the os.environ variable.

Here's the code I'm using to read the HTML file, translate the text, and print the result:

from google.cloud import translate_v2
import os

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"trans.json"
translate_client = translate_v2.Client()

with open("index.html") as f:
    html_content = f.read()

html_english = translate_client.translate(html_content, target_language='hi', format_='html')
print(html_english)

This code doesn't works well for my needs because i got Request payload size exceeds the limit: 204800 bytes., I'm curious to know if there are other ways to translate HTML files using Python. Are there any other libraries or tools that can help me achieve this? I'd love to hear about any other solutions that people have found useful. Thanks in advance for your help!

1

There are 1 answers

6
Luciano Martins On

the Translate API bills per number of chars (as you can see here), so one alternative is to send multiple payloads instead of just a single and big one.

another recommendation is to extract only the data to be translate, instead of all HTML tags -- it will reduce the amount of chars being sent and also will reduce the cost.

Some time ago I wrote one prototype similar to your scenario (but for XML instead of HTML). I think you can base on it to have yours done.