Python 100 project #20: Google Cloud Translate

I’m working in Japanese company in UK, so naturally I use Japanese and English at work. It is often the case, that I just need to translate Japanese to English, or the opposite. And most of the times, it doesn’t require any technical background  (=just a plain translator). So I think it’d be great to have a function it automate those errands.

 

Output Example:

>>> import google_clouds
>>> s = '私は34歳の日本人です。仕事柄、日本語と英語を使いますが、ただの翻訳に時間を取られるのが嫌なので、自動化したいです。'
>>> 
>>> print(google_clouds.get_translate(s))
I am 34 years old Japanese. I use work patterns, Japanese and English, but I do not want to take time for just translation, so I would like to automate it.
>>>

There is just a minor mis-translation, but it is acceptable.

 

Here is the code:

from data_source.google_credentials import get_credential


def get_translate(sentence, lang='en'):
    # Imports the Google Cloud client library
    from google.cloud import translate

    # Instantiates a client
    translate_client = translate.Client(credentials=get_credential())

    # Translates some text into Russian
    translation = translate_client.translate(
        sentence,
        target_language=lang)

    return translation['translatedText']