Using Google Translation API v2 with Python

A simple Python wrapper to call the Google Translation API v2. The only dependecy is demjson.

The API is only abailable as a paid service. You can get an API Key on the Google APIs Console.

Just two things:

  • I noticed an issue (maybe demjson ?) with simple quote, so I added replace in the quick_translate function.
  • The unicode_urlencode function is borrowed from http://github.com/sciyoshi/pyfacebook/.

Here is the code.

# encoding: utf-8
import urllib
import demjson

API_KEY = "YOUR_API_KEY"
TRANSLATE_URL = "https://www.googleapis.com/language/translate/v2?key=" + API_KEY  # &q=hello%20world&source=en&target=de
DETECT_URL = "https://www.googleapis.com/language/translate/v2/detect?key=" + API_KEY  # &q=google+translate+is+fast


def unicode_urlencode(params):
    if isinstance(params, dict):
        params = params.items()
    return urllib.urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v) for k, v in params])


def make_request(url):
    return urllib.urlopen(url).read()


def quick_translate(text, target, source):
    try:
        return translate(text, target, source)["data"]["translations"][0]["translatedText"].replace(''', "'")
    except:
        return ""


def translate(text, target, source):
    query_params = {"q": text, "source": source, "target": target}
    url = TRANSLATE_URL + "&" + unicode_urlencode(query_params)
    try:
        return demjson.decode(make_request(url))
    except:
        return {}


def quick_detect(text):
    try:
        return detect(text)["data"]["detections"][0][0]["language"]
    except:
        return ""


def detect(text):
    query_params = {"q": text}
    url = DETECT_URL + "&" + unicode_urlencode(query_params)
    try:
        return demjson.decode(make_request(url))
    except:
        return {}

if __name__ == '__main__':
    mstr = u"同じ部屋が1人用、2人用と出ていたが、2人用を1人で使用した。値段は60ユーロも違った。部屋のオファーの仕方がよくないのはないでしょうか。設定の仕方が問題がある。"
    lang = quick_detect(mstr)
    print lang
    print quick_translate(mstr, "en", lang)

You should follow me on Twitter

Share this article

Tip with Bitcoin

Tip me with Bitcoin and vote for this post!

1FKdaZ75Ck8Bfc3LgQ8cKA8W7B86fzZBe2

Leave a comment

© Thomas Sileo. Powered by Pelican and hosted by DigitalOcean.