Adeel Raza
Adeel Raza

Reputation: 634

How to break Larger HTML to a shorter one for google translate API

I am using google translate API and my requirement for translation is greater than the google max limit. I want to translate a HTML section which is far greater than google max limit which it allows for a single request. How can I break my HTML into pieces so that I send multiple request with my overall html structure being valid.

Also,I am using nodeJs as a server side language.

any other idea how to achieve this?

Upvotes: 2

Views: 183

Answers (1)

Lukas Boersma
Lukas Boersma

Reputation: 1082

Use a parser like jsdom to transform your HTML content into a DOM structure.

Then, use the translate API to translate the contents of the text nodes in the DOM structure and replace the translated text to get the full translated page.

If you need it, you could also try to find and translate any relevant text outside of text nodes, like alt- or title-attributes.

If you care about performance, you could try to translate bigger subtrees of the DOM structure at once, but then you would have to be careful to not upload too much content again.

Upvotes: 1

Related Questions