Skip to content

Instantly share code, notes, and snippets.

@armindocachada
Created July 21, 2020 15:25
Show Gist options
  • Save armindocachada/de7079c29d31f8f1af5c41f09acdfe16 to your computer and use it in GitHub Desktop.
Save armindocachada/de7079c29d31f8f1af5c41f09acdfe16 to your computer and use it in GitHub Desktop.
def translate(text,language):
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
if isinstance(text, bytes):
text = text.decode('utf-8')
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(
text, target_language=language)
print(u'Text: {}'.format(result['input']))
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
result['detectedSourceLanguage'])
)
return result['translatedText']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment