Skip to content

Commit

Permalink
Merge pull request #940 from maxpowa/patch-1
Browse files Browse the repository at this point in the history
[translate] Fix some edge cases
  • Loading branch information
embolalia committed Nov 22, 2015
2 parents 664d9f4 + f80f3d2 commit a8522ac
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions sopel/modules/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def translate(text, in_lang='auto', out_lang='en'):
url = "http://translate.googleapis.com/translate_a/single?{query}".format(query=query_string)
result = web.get(url, timeout=40, headers=headers)

if result == '[,,""]':
return None, in_lang

while ',,' in result:
result = result.replace(',,', ',null,')
result = result.replace('[,', '[null,')
Expand Down Expand Up @@ -72,6 +75,9 @@ def tr(bot, trigger):
if (len(phrase) > 350) and (not trigger.admin):
return bot.reply('Phrase must be under 350 characters.')

if phrase.strip() == '':
return bot.reply('You need to specify a string for me to translate!')

in_lang = in_lang or 'auto'
out_lang = out_lang or 'en'

Expand All @@ -83,7 +89,7 @@ def tr(bot, trigger):
msg = web.decode(msg) # msg.replace(''', "'")
msg = '"%s" (%s to %s, translate.google.com)' % (msg, in_lang, out_lang)
else:
msg = 'The %s to %s translation failed, sorry!' % (in_lang, out_lang)
msg = 'The %s to %s translation failed, are you sure you specified valid language abbreviations?' % (in_lang, out_lang)

bot.reply(msg)
else:
Expand Down Expand Up @@ -118,6 +124,9 @@ def langcode(p):
if (len(phrase) > 350) and (not trigger.admin):
return bot.reply('Phrase must be under 350 characters.')

if phrase.strip() == '':
return bot.reply('You need to specify a string for me to translate!')

src, dest = args
if src != dest:
msg, src = translate(phrase, src, dest)
Expand All @@ -127,7 +136,7 @@ def langcode(p):
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)
msg = 'The %s to %s translation failed, are you sure you specified valid language abbreviations?' % (src, dest)

bot.reply(msg)
else:
Expand Down

0 comments on commit a8522ac

Please sign in to comment.