add detected source language display for 'auto' google translate, add test for auto translate.

This commit is contained in:
Daniel Folkinshteyn 2010-04-03 23:31:13 -04:00
parent b8f8cec6f5
commit 34cab7f4c9
2 changed files with 13 additions and 1 deletions

View File

@ -276,7 +276,18 @@ class Google(callbacks.PluginRegexp):
fd.close() fd.close()
if json['responseStatus'] != 200: if json['responseStatus'] != 200:
raise callbacks.Error, 'We broke The Google!' raise callbacks.Error, 'We broke The Google!'
if fromLang != '':
irc.reply(json['responseData']['translatedText'].encode('utf-8')) irc.reply(json['responseData']['translatedText'].encode('utf-8'))
else:
detected_language = json['responseData']['detectedSourceLanguage']
try:
long_lang_name = [k for k,v in lang.transLangs.iteritems() if v == detected_language][0]
except IndexError: #just in case google adds langs we don't know about
long_lang_name = detected_language
responsestring = "(Detected source language: %s) %s" % \
(long_lang_name,
json['responseData']['translatedText'].encode('utf-8'))
irc.reply(responsestring)
translate = wrap(translate, ['something', 'to', 'something', 'text']) translate = wrap(translate, ['something', 'to', 'something', 'text'])
def googleSnarfer(self, irc, msg, match): def googleSnarfer(self, irc, msg, match):

View File

@ -59,6 +59,7 @@ class GoogleTestCase(ChannelPluginTestCase):
def testTranslate(self): def testTranslate(self):
self.assertRegexp('translate en es hello world', 'mundo') self.assertRegexp('translate en es hello world', 'mundo')
self.assertRegexp('translate auto en ciao', 'Italian.*hello')
def testCalcDoesNotHaveExtraSpaces(self): def testCalcDoesNotHaveExtraSpaces(self):
self.assertNotRegexp('google calc 1000^2', r'\s+,\s+') self.assertNotRegexp('google calc 1000^2', r'\s+,\s+')