handle translation server errors properly
This commit is contained in:
parent
36ed83d664
commit
0ed8e159a9
|
@ -34,10 +34,9 @@ def unescape(text):
|
||||||
def goog_trans(text, slang, tlang):
|
def goog_trans(text, slang, tlang):
|
||||||
url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0'
|
url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0'
|
||||||
parsed = http.get_json(url, q=text, langpair=(slang + '|' + tlang))
|
parsed = http.get_json(url, q=text, langpair=(slang + '|' + tlang))
|
||||||
print slang, tlang, parsed
|
|
||||||
if not 200 <= parsed['responseStatus'] < 300:
|
if not 200 <= parsed['responseStatus'] < 300:
|
||||||
raise IOError('error with the translation server: %d: %s' % (
|
raise IOError('error with the translation server: %d: %s' % (
|
||||||
parsed['responseStatus'], ''))
|
parsed['responseStatus'], parsed['responseDetails']))
|
||||||
if not slang:
|
if not slang:
|
||||||
return unescape('(%(detectedSourceLanguage)s) %(translatedText)s' %
|
return unescape('(%(detectedSourceLanguage)s) %(translatedText)s' %
|
||||||
(parsed['responseData']))
|
(parsed['responseData']))
|
||||||
|
@ -65,18 +64,21 @@ def translate(inp):
|
||||||
|
|
||||||
args = inp.split(' ', 2)
|
args = inp.split(' ', 2)
|
||||||
|
|
||||||
if len(args) >= 2:
|
try:
|
||||||
sl = match_language(args[0])
|
if len(args) >= 2:
|
||||||
if not sl:
|
sl = match_language(args[0])
|
||||||
return goog_trans(inp, '', 'en')
|
if not sl:
|
||||||
if len(args) >= 3:
|
return goog_trans(inp, '', 'en')
|
||||||
tl = match_language(args[1])
|
if len(args) >= 3:
|
||||||
if not tl:
|
tl = match_language(args[1])
|
||||||
if sl == 'en':
|
if not tl:
|
||||||
return 'unable to determine desired target language'
|
if sl == 'en':
|
||||||
return goog_trans(args[1] + ' ' + args[2], sl, 'en')
|
return 'unable to determine desired target language'
|
||||||
return goog_trans(args[2], sl, tl)
|
return goog_trans(args[1] + ' ' + args[2], sl, 'en')
|
||||||
return goog_trans(inp, '', 'en')
|
return goog_trans(args[2], sl, tl)
|
||||||
|
return goog_trans(inp, '', 'en')
|
||||||
|
except IOError, e:
|
||||||
|
return e
|
||||||
|
|
||||||
|
|
||||||
languages = 'ja fr de ko ru zh'.split()
|
languages = 'ja fr de ko ru zh'.split()
|
||||||
|
|
Loading…
Reference in New Issue