From ca045128d7ec498ca47f90b821e186a7399b9aae Mon Sep 17 00:00:00 2001 From: Daniel Folkinshteyn Date: Sat, 3 Apr 2010 23:31:13 -0400 Subject: [PATCH] add detected source language display for 'auto' google translate, add test for auto translate. --- plugins/Google/plugin.py | 13 ++++++++++++- plugins/Google/test.py | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/plugins/Google/plugin.py b/plugins/Google/plugin.py index 422d613b9..9d5fa42d9 100644 --- a/plugins/Google/plugin.py +++ b/plugins/Google/plugin.py @@ -292,7 +292,18 @@ class Google(callbacks.PluginRegexp): fd.close() if json['responseStatus'] != 200: raise callbacks.Error, 'We broke The Google!' - irc.reply(json['responseData']['translatedText'].encode('utf-8')) + if fromLang != '': + 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']) def googleSnarfer(self, irc, msg, match): diff --git a/plugins/Google/test.py b/plugins/Google/test.py index 663562a9e..3ef6eca81 100644 --- a/plugins/Google/test.py +++ b/plugins/Google/test.py @@ -61,6 +61,7 @@ class GoogleTestCase(ChannelPluginTestCase): def testTranslate(self): self.assertRegexp('translate en es hello world', 'mundo') + self.assertRegexp('translate auto en ciao', 'Italian.*hello') def testCalcDoesNotHaveExtraSpaces(self): self.assertNotRegexp('google calc 1000^2', r'\s+,\s+')