parent
10106045ea
commit
7d0b1c5270
@ -0,0 +1,35 @@
|
||||
import unittest
|
||||
|
||||
from edmond.tests.test_plugin import get_plugin_patcher
|
||||
from ..translate import TranslatePlugin
|
||||
|
||||
|
||||
class TestTranslatePlugin(unittest.TestCase):
|
||||
|
||||
def test_parse_words(self):
|
||||
with get_plugin_patcher(TranslatePlugin):
|
||||
plugin = TranslatePlugin()
|
||||
plugin.config = {
|
||||
"default_dest": "en",
|
||||
"param_source": "from",
|
||||
"param_dest": "to",
|
||||
}
|
||||
|
||||
# No params.
|
||||
result = plugin.parse_words(["test"])
|
||||
self.assertEqual(result, ("autodetect", "en", "test"))
|
||||
# Source param.
|
||||
result = plugin.parse_words(["!from:fr", "test"])
|
||||
self.assertEqual(result, ("fr", "en", "test"))
|
||||
# Destination param.
|
||||
result = plugin.parse_words(["!to:fr", "test"])
|
||||
self.assertEqual(result, ("autodetect", "fr", "test"))
|
||||
# Both source and dest params.
|
||||
result = plugin.parse_words(["!from:it", "!to:fr", "test"])
|
||||
self.assertEqual(result, ("it", "fr", "test"))
|
||||
# Badly placed param.
|
||||
result = plugin.parse_words(["!from:it", "test", "!to:fr"])
|
||||
self.assertEqual(result, ("it", "en", "test !to:fr"))
|
||||
# Unknown param.
|
||||
result = plugin.parse_words(["!zzz", "test"])
|
||||
self.assertEqual(result, ("autodetect", "en", "test"))
|
@ -0,0 +1,54 @@
|
||||
try:
|
||||
from translate import Translator
|
||||
DEPENDENCIES_FOUND = True
|
||||
except ImportError:
|
||||
DEPENDENCIES_FOUND = False
|
||||
|
||||
from edmond.plugin import Plugin
|
||||
|
||||
|
||||
class TranslatePlugin(Plugin):
|
||||
|
||||
REQUIRED_CONFIGS = [
|
||||
"commands", "default_dest", "param_source", "param_dest",
|
||||
]
|
||||
|
||||
def __init__(self, bot):
|
||||
super().__init__(bot)
|
||||
|
||||
def on_pubmsg(self, event):
|
||||
if not self.should_handle_command(event.arguments[0]):
|
||||
return False
|
||||
|
||||
text = self.command.content
|
||||
if not text:
|
||||
return False
|
||||
words = text.split()
|
||||
if len(words) == 0:
|
||||
return False
|
||||
|
||||
from_lang, to_lang, text = self.parse_words(words)
|
||||
self.bot.log_d(f"Translating '{text}' from {from_lang} to {to_lang}.")
|
||||
translator = Translator(to_lang=to_lang, from_lang=from_lang)
|
||||
translation = translator.translate(text)
|
||||
self.bot.say(event.target, translation)
|
||||
return True
|
||||
|
||||
def parse_words(self, words):
|
||||
"""Parse given words for parameters and return (from, to, text)."""
|
||||
from_lang = "autodetect"
|
||||
to_lang = self.config["default_dest"]
|
||||
num_param_found = 0
|
||||
for word in words:
|
||||
if not word.startswith("!"):
|
||||
break
|
||||
num_param_found += 1
|
||||
param_word = word.lstrip("!")
|
||||
if not ":" in param_word:
|
||||
continue
|
||||
param_name, param_value = param_word.split(":")
|
||||
if param_name == self.config["param_source"]:
|
||||
from_lang = param_value
|
||||
elif param_name == self.config["param_dest"]:
|
||||
to_lang = param_value
|
||||
return from_lang, to_lang, " ".join(words[num_param_found:])
|
Loading…
Reference in new issue