class TranslateMod(loader.Module): """Переводчик""" strings = { "name": "Translate", "translated": "<b>C: </b><code>{from_lang}</code>" "\n<b>На: </b><code>{to_lang}</code>\n\n{output}", "invalid_text": "Неверный текст для перевода", "doc_default_lang": "Язык для перевода по умолчанию", "doc_api_key": "Ключ API на https://translate.yandex.com/developers/keys" } def __init__(self): self.config = loader.ModuleConfig( "DEFAULT_LANG", "en", lambda m: self.strings("doc_default_lang", m), "API_KEY", "", lambda m: self.strings("doc_api_key", m)) def config_complete(self): self.tr = Translate(self.config["API_KEY"]) @loader.unrestricted @loader.ratelimit async def translatecmd(self, message): """.translate [с N1 языка->][->на N2 язык] <текст>""" args = utils.get_args(message) if len(args) == 0 or "->" not in args[0]: text = " ".join(args) args = ["", self.config["DEFAULT_LANG"]] else: text = " ".join(args[1:]) args = args[0].split("->") if len(text) == 0 and message.is_reply: text = (await message.get_reply_message()).message if len(text) == 0: await utils.answer(message, self.strings("invalid_text", message)) return if args[0] == "": args[0] = self.tr.detect(text) if len(args) == 3: del args[1] if len(args) == 1: logging.error( "python split() error, if there is -> in the text, it must split!" ) raise RuntimeError() if args[1] == "": args[1] = self.config["DEFAULT_LANG"] args[0] = args[0].lower() logger.debug(args) translated = self.tr.translate(text, args[1], args[0]) ret = self.strings("translated", message).format( from_lang=utils.escape_html(args[0]), to_lang=utils.escape_html(args[1]), output=utils.escape_html(translated)) await utils.answer(message, ret)
class TranslateMod(loader.Module): """Translator""" strings = { "name": "Translator", "translated": "<b>From: </b><code>{from_lang}</code>" "\n<b>To: </b><code>{to_lang}</code>\n\n{output}", "invalid_text": "Invalid text to translate", "doc_default_lang": "Language to translate to by default", "doc_api_key": "API key from https://translate.yandex.com/developers/keys" } def __init__(self): self.commands = {"translate": self.translatecmd} self.config = loader.ModuleConfig( "DEFAULT_LANG", "en", lambda: self.strings["doc_default_lang"], "API_KEY", "", lambda: self.strings["doc_api_key"]) def config_complete(self): self.tr = Translate(self.config["API_KEY"]) self.name = self.strings["name"] async def translatecmd(self, message): """.translate [from_lang->][->to_lang] <text>""" args = utils.get_args(message) if len(args) == 0 or "->" not in args[0]: text = " ".join(args) args = ["", self.config["DEFAULT_LANG"]] else: text = " ".join(args[1:]) args = args[0].split("->") if len(text) == 0 and message.is_reply: text = (await message.get_reply_message()).message if len(text) == 0: await message.edit(self.strings["invalid_text"]) return if args[0] == "": args[0] = self.tr.detect(text) if len(args) == 3: del args[1] if len(args) == 1: logging.error( "python split() error, if there is -> in the text, it must split!" ) raise RuntimeError() if args[1] == "": args[1] = self.config["DEFAULT_LANG"] args[0] = args[0].lower() logger.debug(args) translated = self.tr.translate(text, args[1], args[0]) ret = self.strings["translated"].format( from_lang=utils.escape_html(args[0]), to_lang=utils.escape_html(args[1]), output=utils.escape_html(translated)) await utils.answer(message, ret)
class TranslateMod(loader.Module): """Translator""" def __init__(self): self.commands = {"translate":self.translatecmd} self.config = {"DEFAULT_LANG":"en", "API_KEY":""} self.name = "Translator" def config_complete(self): self.tr = Translate(self.config["API_KEY"]) async def translatecmd(self, message): """.translate [from_lang->][->to_lang] <text>""" args = utils.get_args(message) if len(args) == 0 or not "->" in args[0]: text = " ".join(args) args = ["", self.config["DEFAULT_LANG"]] else: text = " ".join(args[1:]) args = args[0].split("->") if len(text) == 0 and message.is_reply: text = (await message.get_reply_message()).message if len(text) == 0: await message.edit("Invalid text to translate") return if args[0] == "": args[0] = self.tr.detect(text) if len(args) == 3: del args[1] if len(args) == 1: logging.error("python split() error, if there is -> in the text, it must split!") raise RuntimeException() if args[1] == "": args[1] = self.config["DEFAULT_LANG"] args[0] = args[0].lower() logger.debug(args) translated = self.tr.translate(text, args[1], args[0]) ret = "<b>Translated </b><code>" ret += utils.escape_html(text) ret += "</code><b> from </b><code>" ret += utils.escape_html(args[0]) ret += "</code><b> to </b><code>" ret += utils.escape_html(args[1]) ret += "</code><b> and it reads </b><code>" ret += utils.escape_html(translated) ret += "</code>" await message.edit(ret)
class TranslateMod(loader.Module): """Translator""" def __init__(self): self.commands = {"translate": self.translatecmd} self.config = loader.ModuleConfig("DEFAULT_LANG", "en", "Language to translate to by default", "API_KEY", "", "API key from https://translate.yandex.com/developers/keys") self.name = _("Translator") def config_complete(self): self.tr = Translate(self.config["API_KEY"]) async def translatecmd(self, message): """.translate [from_lang->][->to_lang] <text>""" args = utils.get_args(message) if len(args) == 0 or "->" not in args[0]: text = " ".join(args) args = ["", self.config["DEFAULT_LANG"]] else: text = " ".join(args[1:]) args = args[0].split("->") if len(text) == 0 and message.is_reply: text = (await message.get_reply_message()).message if len(text) == 0: await message.edit(_("Invalid text to translate")) return if args[0] == "": args[0] = self.tr.detect(text) if len(args) == 3: del args[1] if len(args) == 1: logging.error("python split() error, if there is -> in the text, it must split!") raise RuntimeError() if args[1] == "": args[1] = self.config["DEFAULT_LANG"] args[0] = args[0].lower() logger.debug(args) translated = self.tr.translate(text, args[1], args[0]) ret = _("<b>Translated </b><code>{text}</code>\n<b>from </b><code>{frlang}</code><b> to </b>" + "<code>{to}</code><b> and it reads</b>\n<code>{output}</code>") ret = ret.format(text=utils.escape_html(text), frlang=utils.escape_html(args[0]), to=utils.escape_html(args[1]), output=utils.escape_html(translated)) await utils.answer(message, ret)
def config_complete(self): self.tr = Translate(self.config["API_KEY"])
def config_complete(self): self.tr = Translate(self.config["API_KEY"]) self.name = self.strings["name"]