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:
And update the individual translate lines in each translation template filter
# 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 ...
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.
# 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 ...
Uglek is now mostly translated, save for a few views. It is also much faster with this translation API.
Beautiful glacier views in Icy Bay, Alaska Photo credit - @Newbflat
Bear prints in Icy Bay Photo credit - @Newbflat
Bear sighting in Icy Bay Photo credit - @Newbflat
Icebergs in Icy Bay in Alaska This is a photo by @Newbflat of some icebergs we saw while kayaking. I will post some more photos from Icy Bay kayaking.