From 0ed8e159a923489e87ca7d374a0bd00165e70fcb Mon Sep 17 00:00:00 2001 From: Ryan Hitchman Date: Fri, 10 Sep 2010 22:08:35 -0500 Subject: [PATCH] handle translation server errors properly --- plugins/translate.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/plugins/translate.py b/plugins/translate.py index 9ed19dc..80a20b2 100644 --- a/plugins/translate.py +++ b/plugins/translate.py @@ -34,10 +34,9 @@ def unescape(text): def goog_trans(text, slang, tlang): url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0' parsed = http.get_json(url, q=text, langpair=(slang + '|' + tlang)) - print slang, tlang, parsed if not 200 <= parsed['responseStatus'] < 300: raise IOError('error with the translation server: %d: %s' % ( - parsed['responseStatus'], '')) + parsed['responseStatus'], parsed['responseDetails'])) if not slang: return unescape('(%(detectedSourceLanguage)s) %(translatedText)s' % (parsed['responseData'])) @@ -65,18 +64,21 @@ def translate(inp): args = inp.split(' ', 2) - if len(args) >= 2: - sl = match_language(args[0]) - if not sl: - return goog_trans(inp, '', 'en') - if len(args) >= 3: - tl = match_language(args[1]) - if not tl: - if sl == 'en': - return 'unable to determine desired target language' - return goog_trans(args[1] + ' ' + args[2], sl, 'en') - return goog_trans(args[2], sl, tl) - return goog_trans(inp, '', 'en') + try: + if len(args) >= 2: + sl = match_language(args[0]) + if not sl: + return goog_trans(inp, '', 'en') + if len(args) >= 3: + tl = match_language(args[1]) + if not tl: + if sl == 'en': + return 'unable to determine desired target language' + return goog_trans(args[1] + ' ' + args[2], sl, '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()