Created
March 29, 2011 12:08
-
-
Save meehow/892242 to your computer and use it in GitHub Desktop.
Translation from command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from urllib import urlencode, urlopen | |
from sys import argv | |
import json | |
def translate(lang1, lang2, text): | |
""" | |
docs: http://code.google.com/intl/pl/apis/ajaxlanguage/documentation/ | |
""" | |
url = 'https://ajax.googleapis.com/ajax/services/language/translate?' | |
params = urlencode(( | |
('v', 1.0), | |
('q', text), | |
('langpair', '%s|%s' % (lang1, lang2)), | |
)) | |
response = json.load(urlopen(url + params)) | |
if response.get('responseData'): | |
return response.get('responseData').get('translatedText') | |
else: | |
raise Exception(response.get('responseDetails')) | |
if __name__ == "__main__": | |
if len(argv) >= 4: | |
print translate(argv[1], argv[2], ' '.join(argv[3:])) | |
else: | |
print 'Usage: %s [from lang] [to lang] [text]' % argv[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment