From 2820bc61d68b3ad4996565b8887268790fc776e4 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Fri, 17 Feb 2012 22:16:49 -0500 Subject: [PATCH] fix translate module, convert to 4 space tabs --- modules/translate.py | 53 +++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/modules/translate.py b/modules/translate.py index 17f25a3..6fdc3a4 100644 --- a/modules/translate.py +++ b/modules/translate.py @@ -73,37 +73,40 @@ tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?' tr.priority = 'low' def tr2(phenny, input): - """Translates a phrase, with an optional language hint.""" - command = input.group(2).encode('utf-8') + """Translates a phrase, with an optional language hint.""" + command = input.group(2) + if not command: + phenny.reply("Please provide a phrase to translate") + return - def langcode(p): - return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha() + def langcode(p): + return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha() - args = ['auto', 'en'] + args = ['auto', 'en'] - for i in xrange(2): - if not ' ' in command: break - prefix, cmd = command.split(' ', 1) - if langcode(prefix): - args[i] = prefix[1:] - command = cmd - phrase = command + for i in range(2): + if not ' ' in command: break + prefix, cmd = command.split(' ', 1) + if langcode(prefix): + args[i] = prefix[1:] + command = cmd + phrase = command.encode('utf-8') - if (len(phrase) > 350) and (not input.admin): - return phenny.reply('Phrase must be under 350 characters.') + if (len(phrase) > 350) and (not input.admin): + return phenny.reply('Phrase must be under 350 characters.') - src, dest = args - if src != dest: - msg, src = translate(phrase, src, dest) - if isinstance(msg, str): - msg = msg.decode('utf-8') - if msg: - msg = web.decode(msg) # msg.replace(''', "'") - msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest) - else: msg = 'The %s to %s translation failed, sorry!' % (src, dest) + src, dest = args + if src != dest: + msg, src = translate(phrase, src, dest) + #if isinstance(msg, str): + # msg = msg.decode('utf-8') + if msg: + msg = web.decode(msg) # msg.replace(''', "'") + msg = '"%s" (%s to %s, translate.google.com)' % (msg, src, dest) + else: msg = 'The %s to %s translation failed, sorry!' % (src, dest) - phenny.reply(msg) - else: phenny.reply('Language guessing failed, so try suggesting one!') + phenny.reply(msg) + else: phenny.reply('Language guessing failed, so try suggesting one!') tr2.commands = ['tr'] tr2.priority = 'low'