Skip to content
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

Add Languages command #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# translate
A [maubot](https://github.com/maubot/maubot) to translate words using Google Translate (DeepL is planned too)
A [maubot](https://github.com/maubot/maubot) to translate words using [Google
Translate](https://translate.google.com/about/) or
[LibreTranslate](https://github.com/LibreTranslate/LibreTranslate)
(Deepl is planned too)

## Usage

Expand All @@ -19,15 +22,14 @@ You can also use the alias `tr`:
!tr en ru Hello world.

The first parameter (source language) can be set to `auto` or omitted entirely
to let Google Translate detect the source language. Additionally, you can reply
to let the bot detect the source language. Additionally, you can reply
to a message with `!tr <from> <to>` (no text) to translate the message you
replied to.

## supported languages:

- de: (german)
- en: (english)
- zh: (chinese)
- ...
Since this depends on which translation provider you choose and can even vary from
one LibreTranslate instance to another, you can use the following command to get
all the supported languages and their codes:

Full list of supported languages: https://cloud.google.com/translate/docs/languages
!languages
5 changes: 4 additions & 1 deletion base-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Translation provider settings
provider:
id: google
args: {}
args: {
url: 'translate.example.com', # required if you use libretranslate, else irrelevant
api_key: 'loremipsumdolorsitamen1234' # optional if you use libretranslate, else irrelevant
}
auto_translate:
- room_id: '!roomid:example.com'
main_language: en
Expand Down
17 changes: 12 additions & 5 deletions translate/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class TranslatorBot(Plugin):

async def start(self) -> None:
await super().start()
self.on_external_config_update()
await self.on_external_config_update()

def on_external_config_update(self) -> None:
async def on_external_config_update(self) -> None:
self.translator = None
self.config.load_and_update()
self.auto_translate = self.config.load_auto_translate()
try:
self.translator = self.config.load_translator()
self.translator = await self.config.load_translator()
except TranslationProviderError:
self.log.exception("Error loading translator")

Expand Down Expand Up @@ -84,7 +84,8 @@ def is_acceptable(lang: str) -> bool:
async def command_handler(self, evt: MessageEvent, language: Optional[Tuple[str, str]],
text: str) -> None:
if not language:
await evt.reply("Usage: !translate [from] <to> [text or reply to message]")
await evt.reply("No supported target language detected. "
"Usage: !translate [from] <to> [text or reply to message]")
return
if not self.config["response_reply"]:
evt.disable_reply = True
Expand All @@ -95,7 +96,13 @@ async def command_handler(self, evt: MessageEvent, language: Optional[Tuple[str,
reply_evt = await self.client.get_event(evt.room_id, evt.content.get_reply_to())
text = reply_evt.content.body
if not text:
await evt.reply("Usage: !translate [from] <to> [text or reply to message]")
await evt.reply("Nothing to translate detected. Usage: !translate [from] <to> [text or reply to message]")
return
result = await self.translator.translate(text, to_lang=language[1], from_lang=language[0])
await evt.reply(result.text)

@command.new("languages")
async def languages_command_handler(self, evt: MessageEvent) -> None:
await evt.reply(
", ".join([f"{name} `{code}`" for code, name in self.translator.get_supported_languages().items()])
)
5 changes: 5 additions & 0 deletions translate/provider/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class AbstractTranslationProvider(ABC):
@abstractmethod
def __init__(self, args: Dict) -> None:
pass
async def post_init(self) -> None:
pass

@abstractmethod
async def translate(self, text: str, to_lang: str, from_lang: str = "auto") -> Result:
Expand All @@ -35,3 +37,6 @@ def is_supported_language(self, code: str) -> bool:
@abstractmethod
def get_language_name(self, code: str) -> str:
pass
@abstractmethod
def get_supported_languages(self) -> dict:
pass
3 changes: 3 additions & 0 deletions translate/provider/deepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,8 @@ def is_supported_language(self, code: str) -> bool:
def get_language_name(self, code: str) -> str:
return self.supported_languages[code]

def get_supported_languages(self) -> dict:
return self.supported_languages


make_translation_provider = DeepLTranslate
3 changes: 3 additions & 0 deletions translate/provider/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,8 @@ def is_supported_language(self, code: str) -> bool:
def get_language_name(self, code: str) -> str:
return self.supported_languages[code]

def get_supported_languages(self) -> dict:
return self.supported_languages


make_translation_provider = GoogleTranslate
74 changes: 74 additions & 0 deletions translate/provider/libretranslate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# translate - A maubot plugin to translate words.
# Copyright (C) 2022 Tammes Burghard
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import json
import re
from typing import Dict

from aiohttp import ClientSession, client_exceptions
from yarl import URL

from . import AbstractTranslationProvider, Result


class LibreTranslate(AbstractTranslationProvider):
headers: Dict[str, str] = {"Content-Type": "application/json"}

def __init__(self, args: Dict) -> None:
super().__init__(args)
if args.get("url") is None:
raise ValueError("Please specify the url of your preferred libretranslate instance in provider.args.url")
else:
self._base_url = args.get("url")
if not re.match(r"^https?://", self._base_url):
self._base_url = "https://" + self._base_url
self.url: URL = URL(self._base_url + "/translate")
self.api_key = args.get("api_key")
self.supported_languages = {"auto": "Detect language"}

async def post_init(self):
try:
async with ClientSession() as sess:
resp = await sess.get(self._base_url + "/languages")
for lang in await resp.json():
self.supported_languages[lang["code"]] = lang["name"]
except client_exceptions.ClientError:
raise ValueError(f"This url ({self._base_url}) does not point to a compatible libretranslate instance. "
f"Please change it")

async def translate(self, text: str, to_lang: str, from_lang: str = "auto") -> Result:
if not from_lang:
from_lang = "auto"
async with ClientSession() as sess:
data=json.dumps({"q": text, "source": from_lang, "target": to_lang,
"format": "text", "api_key": self.api_key})
resp = await sess.post(self.url, data=data, headers=self.headers)
if resp.status == 403:
raise ValueError("Request forbidden. You did probably configure an incorrect api key.")
data = await resp.json()
return Result(text=data["translatedText"],
source_language=data["detectedLanguage"]["language"] if from_lang == "auto" else from_lang)

def is_supported_language(self, code: str) -> bool:
return code.lower() in self.supported_languages.keys()

def get_language_name(self, code: str) -> str:
return self.supported_languages[code]

def get_supported_languages(self) -> dict:
return self.supported_languages


make_translation_provider = LibreTranslate
6 changes: 4 additions & 2 deletions translate/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("auto_translate")
helper.copy("response_reply")

def load_translator(self) -> AbstractTranslationProvider:
async def load_translator(self) -> AbstractTranslationProvider:
try:
provider = self["provider.id"]
mod = import_module(f".{provider}", "translate.provider")
make = mod.make_translation_provider
except (KeyError, AttributeError, ImportError) as e:
raise TranslationProviderError("Failed to load translation provider") from e
try:
return make(self["provider.args"])
translation_provider = make(self["provider.args"])
await translation_provider.post_init()
return translation_provider
except Exception as e:
raise TranslationProviderError("Failed to initialize translation provider") from e

Expand Down