Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions _functions/translate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python
from function_template import *
import requests
import json
import re

import logging
log = logging.getLogger('bot')
syntax = 'Syntax: !translate <lang> <message...>'
langList = "www.i18nguy.com/unicode/language-identifiers.html"

class function(function_template):
def __init__(self):
function_template.__init__(self)
self.commands = ["translate", "trans"]
self.function_string = "Translate a string. Syntax: '!translate <lang> <message...>'\nExample Languages: 'es' = Spanish, 'es-US' = US Spanish, etc. List: " + langList
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes a NameError: global name 'langList' is not defined exception.

The syntax and langList variables need to be referenced with self.langList or self.syntax since they belong to the class.


def main(self, bot, msg_data, func_type):
message = msg_data["message"]
error = ''
if len(message) > 2:
lang = message[1]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still causes a IndexError: list index out of range error when running just !trans since the msg_data["message"] contains the whole message including !trans or !translate. !trans would be index 0 and there is no index 1.

Additionally the lang should be standardized but running lower() on it otherwise the Please use a language other than "en". is not called with !trans EN

nonTranslated = " ".join(msg_data["message"][2:])
unsafeCharString = "@#$%^&+=,/:;?\'\"<>{}|\\~[]`"
# Remove the unsafe chars
for unsafeChar in list(unsafeCharString):
nonTranslated.replace(unsafeChar, ' ')
nonTranslated = re.sub(" +", " ", nonTranslated)
# Eliminate multiple spaces
if not(error):
r = requests.get("http://api.mymemory.translated.net/get?langpair=" + lang + "|en&de=overcastian@mailinator.com&q=" + nonTranslated)
translated = ''
if r.status_code != requests.codes.ok:
error = 'Request Exception - Code: ' + str(r.status_code)
else:
jsonObj = json.loads(r.text)
translated = jsonObj["responseData"]["translatedText"]
statusCode = jsonObj["responseStatus"]
statusMsg = jsonObj["responseDetails"]
if statusCode != 200:
if "PLEASE SELECT TWO DISTINCT LANGUAGES" in statusMsg:
error = 'Please use a non english language'
if "NO QUERY SPECIFIED" in statusMsg:
error = syntax
if "INVALID SOURCE LANGUAGE" in statusMsg:
error = "\"" + lang + "\" is not a valid language. List: " + langList
if not(error):
error = "Unknown error when translating. See console"
log.info('"' + translated + '" for "' + nonTranslated + '" with lang: ' + lang)
else:
translated = translated.encode('utf-8')
bot._irc.sendMSG("\"" + str(translated) + "\"", msg_data["target"])
else:
error = syntax
if error: bot._irc.sendMSG(error, msg_data["target"])
return True