2024-02-11 12:56:30 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
"""Mozhi (alternative frontend for popular translation engines)"""
|
|
|
|
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
from urllib.parse import urlencode
|
2024-06-14 12:05:41 +00:00
|
|
|
from flask_babel import gettext
|
2024-02-11 12:56:30 +00:00
|
|
|
|
|
|
|
about = {
|
|
|
|
"website": 'https://codeberg.org/aryak/mozhi',
|
|
|
|
"wikidata_id": None,
|
|
|
|
"official_api_documentation": 'https://mozhi.aryak.me/api/swagger/index.html',
|
|
|
|
"use_official_api": True,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'JSON',
|
|
|
|
}
|
|
|
|
|
|
|
|
engine_type = 'online_dictionary'
|
2024-04-25 17:48:37 +00:00
|
|
|
categories = ['general', 'translate']
|
2024-02-11 12:56:30 +00:00
|
|
|
|
|
|
|
base_url = "https://mozhi.aryak.me"
|
|
|
|
mozhi_engine = "google"
|
|
|
|
|
|
|
|
re_transliteration_unsupported = "Direction '.*' is not supported"
|
|
|
|
|
|
|
|
|
|
|
|
def request(_query, params):
|
|
|
|
request_url = random.choice(base_url) if isinstance(base_url, list) else base_url
|
|
|
|
|
|
|
|
args = {'from': params['from_lang'][1], 'to': params['to_lang'][1], 'text': params['query'], 'engine': mozhi_engine}
|
|
|
|
params['url'] = f"{request_url}/api/translate?{urlencode(args)}"
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def response(resp):
|
|
|
|
translation = resp.json()
|
|
|
|
|
|
|
|
infobox = ""
|
|
|
|
|
|
|
|
if translation['target_transliteration'] and not re.match(
|
|
|
|
re_transliteration_unsupported, translation['target_transliteration']
|
|
|
|
):
|
|
|
|
infobox = f"<b>{translation['target_transliteration']}</b>"
|
|
|
|
|
|
|
|
if translation['word_choices']:
|
|
|
|
for word in translation['word_choices']:
|
2024-06-14 12:05:41 +00:00
|
|
|
infobox += f"<dl><dt>{word['word']}: {word['definition']}</dt>"
|
2024-02-11 12:56:30 +00:00
|
|
|
|
2024-06-14 12:05:41 +00:00
|
|
|
if word['examples_target']:
|
|
|
|
for example in word['examples_target']:
|
|
|
|
infobox += f"<dd>{re.sub(r'<|>', '', example)}</dd>"
|
|
|
|
infobox += f"<dd>{re.sub(r'<|>', '', example)}</dd>"
|
2024-02-11 12:56:30 +00:00
|
|
|
|
|
|
|
infobox += "</dl>"
|
|
|
|
|
2024-06-14 12:05:41 +00:00
|
|
|
if translation['source_synonyms']:
|
|
|
|
infobox += f"<dl><dt>{gettext('Synonyms')}: {', '.join(translation['source_synonyms'])}</dt></dl>"
|
|
|
|
|
2024-02-11 12:56:30 +00:00
|
|
|
result = {
|
|
|
|
'infobox': translation['translated-text'],
|
|
|
|
'content': infobox,
|
|
|
|
}
|
|
|
|
|
|
|
|
return [result]
|