Skip to content
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ A translation app for GNOME.
- Open Source:
- LibreTranslate - Use any public instance, defaults to [our own](https://lt.dialectapp.org/).
- Lingva Translate - Use any public instance, defaults to [our own](https://lingva.dialectapp.org/).
- [MinT](https://www.mediawiki.org/wiki/MinT) - Use any public instance, defaults to [Wikimedia Cloud Services](https://translate.wmcloud.org/)

## Features

Expand Down
87 changes: 87 additions & 0 deletions dialect/providers/modules/mint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright 2021 Mufeed Ali
# Copyright 2021 Rafael Mardojai CM
# Copyright 2025 Alejandro Armas
# SPDX-License-Identifier: GPL-3.0-or-later

from dialect.providers.base import (
ProviderCapability,
ProviderFeature,
Translation,
)
from dialect.providers.errors import (
UnexpectedError,
)
from dialect.providers.soup import SoupProvider


class Provider(SoupProvider):
name = "mint"
prettyname = "MinT"

capabilities = ProviderCapability.TRANSLATION
features = ProviderFeature.INSTANCES

defaults = {
"instance_url": "translate.wmcloud.org",
"api_key": "",
"src_langs": ["en", "fr", "es", "de"],
"dest_langs": ["fr", "es", "de", "en"],
}

def __init__(self, **kwargs):
super().__init__(**kwargs)

self.src_dest_langs = dict()

@property
def lang_url(self):
return self.format_url(self.instance_url, "/api/languages")

@property
def translate_url(self):
return self.format_url(self.instance_url, "/api/translate")

def cmp_langs(self, a: str, b: str) -> bool:
"""
Compare two language codes.

Args:
a: First lang to compare.
b: Second lang to compare.

Returns:
True if it's not possible to translate from a to b.
"""
valid_dests = self.src_dest_langs.get(a, [])
return not b in valid_dests

async def init_trans(self):
response = await self.get(self.lang_url)

try:
for src_lang, dest_langs in response.items():
src_lang = self.normalize_lang_code(src_lang)
self.add_lang(src_lang)
self.src_dest_langs[src_lang] = [self.normalize_lang_code(l) for l in dest_langs.keys()]

except Exception as exc:
raise UnexpectedError from exc

async def translate(self, request):
src, dest = self.denormalize_lang(request.src, request.dest)

# Request body
data = {
"content": request.text,
"format": "text",
"source_language": src,
"target_language": dest,
}

# Do request
response = await self.post(self.translate_url, data)

try:
return Translation(response["translation"], request)
except Exception as exc:
raise UnexpectedError from exc