-
Notifications
You must be signed in to change notification settings - Fork 2
Add the ability to translate text #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ae76a65
e465daa
42ea880
c3c4c32
ca213ca
9a1c1a0
c729f26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
| def main(self, bot, msg_data, func_type): | ||
| message = msg_data["message"] | ||
| error = '' | ||
| if len(message) > 2: | ||
| lang = message[1] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still causes a Additionally the lang should be standardized but running lower() on it otherwise the |
||
| 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 | ||
There was a problem hiding this comment.
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 definedexception.The syntax and langList variables need to be referenced with self.langList or self.syntax since they belong to the class.