mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-25 04:02:46 +01:00
Merge pull request #1132 from Ban3/google-translate
Restore Google translate
This commit is contained in:
commit
1567bc2533
@ -240,6 +240,44 @@ class Google(callbacks.PluginRegexp):
|
||||
s = ', '.join([format('%s: %i', bold(s), i) for (i, s) in results])
|
||||
irc.reply(s)
|
||||
|
||||
@internationalizeDocstring
|
||||
def translate(self, irc, msg, args, sourceLang, targetLang, text):
|
||||
"""<source language> [to] <target language> <text>
|
||||
|
||||
Returns <text> translated from <source language> into <target
|
||||
language>.
|
||||
"""
|
||||
|
||||
channel = msg.args[0]
|
||||
|
||||
headers = dict(utils.web.defaultHeaders)
|
||||
headers['User-Agent'] = ('Mozilla/5.0 (X11; U; Linux i686) '
|
||||
'Gecko/20071127 Firefox/2.0.0.11')
|
||||
|
||||
sourceLang = urllib.quote(sourceLang)
|
||||
targetLang = urllib.quote(targetLang)
|
||||
|
||||
text = urllib.quote(text)
|
||||
|
||||
result = utils.web.getUrlFd('http://translate.googleapis.com/translate_a/single'
|
||||
'?client=gtx&dt=t&sl=%s&tl=%s&q='
|
||||
'%s' % (sourceLang, targetLang, text),
|
||||
headers).read().decode('utf8')
|
||||
|
||||
while ',,' in result:
|
||||
result = result.replace(',,', ',null,')
|
||||
while '[,' in result:
|
||||
result = result.replace('[,', '[')
|
||||
data = json.loads(result)
|
||||
|
||||
try:
|
||||
language = data[2]
|
||||
except:
|
||||
language = 'unknown'
|
||||
|
||||
irc.reply(''.join(x[0] for x in data[0]), language)
|
||||
translate = wrap(translate, ['something', 'to', 'something', 'text'])
|
||||
|
||||
def googleSnarfer(self, irc, msg, match):
|
||||
r"^google\s+(.*)$"
|
||||
if not self.registryValue('searchSnarfer', msg.args[0]):
|
||||
|
@ -69,6 +69,9 @@ class GoogleTestCase(ChannelPluginTestCase):
|
||||
self.assertRegexp('fight supybot moobot', r'.*supybot.*: \d+')
|
||||
self.assertNotError('fight ... !')
|
||||
|
||||
def testTranslate(self):
|
||||
self.assertRegexp('translate en es hello world', 'Hola mundo')
|
||||
|
||||
def testCalcDoesNotHaveExtraSpaces(self):
|
||||
self.assertNotRegexp('google calc 1000^2', r'\s+,\s+')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user