@Jasper_Holton's profile photo

How to Upgrade Python Googletrans to Google Translate Enterprise API Upgrading to Google Translate Enterprise is fairly simple. It only involves modifying the initialization of the translator and changing the translate command. This looks like the following:


# app/templatetags/app_filters.py
from google.cloud import translate_v2 as translate
...
# translator = translate.Translator() -- Remove this
translator = translate.Client.from_service_account_json('/home/path/to/service-account-file.json') # Change to this
...
And update the individual translate lines in each translation template filter

# app/templatetags/app_filters.py
...
# translation = translator.translate(...) -- Remove this
translation = translator.translate(value, target_language=lang, source_language='en', format_='html')['translatedText'] # Change to this
...
This is all it takes to get the Django template tags from my previous translation example working with Google Cloud Enterprise translation. Visit the previous example: uglek.com/post/300/ or see the code in action by using the "Language" button at the top of the site or sidebar (bottom on mobile) and selecting a language to translate the website.


one comment.

Pinterest logo
Tumblr logo VK logo

https://uglek.com/post/330/ -

Jasper_Holton's profile photo

Uglek is now mostly translated, save for a few views. It is also much faster with this translation API.

© Uglek, 2022

Terms of Use and Privacy Policy