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

feat: Add Alibaba machine translate service #10237

Merged
merged 5 commits into from
Dec 4, 2023
Merged
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
21 changes: 21 additions & 0 deletions docs/admin/machine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ The services translate from the source language as configured at

:ref:`machine-translation`

.. _mt-alibaba:

Alibaba
-------

:Service ID: ``alibaba``
:Configuration: +------------+-------------------+--+
| ``key`` | Access key ID | |
+------------+-------------------+--+
| ``secret`` | Access key secret | |
+------------+-------------------+--+
| ``region`` | Region ID | |
+------------+-------------------+--+

Alibaba Translate is a neural machine translation service for translating text
and it supports up to 214 language pairs.

.. seealso::

`Alibaba Translate Documentation <https://www.alibabacloud.com/help/en/machine-translation>`_

.. _mt-amagama:

Amagama
Expand Down
2 changes: 2 additions & 0 deletions requirements-optional.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
-r requirements.txt
# Alibaba
aliyun-python-sdk-alimt>=3.2.0,<4.0.0
# Amazon
boto3>=1.28.62,<1.34.0
# LDAP
Expand Down
287 changes: 287 additions & 0 deletions weblate/machinery/alibaba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
# Copyright © Michal Čihař <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later


import json

from aliyunsdkalimt.request.v20181012 import TranslateGeneralRequest
from aliyunsdkcore.client import AcsClient
from django.utils.functional import cached_property

from .base import MachineTranslation, MachineTranslationError
from .forms import AlibabaMachineryForm


class AlibabaTranslation(MachineTranslation):
"""Alibaba API machine translation support."""

name = "Alibaba"
max_score = 80

language_map = {
"zh_Hans": "zh",
"zh_Hant": "zh-tw",
}

settings_form = AlibabaMachineryForm

@cached_property
def client(self):
return AcsClient(
ak=self.settings["key"],
secret=self.settings["secret"],
region_id=self.settings["region"],
)

def download_languages(self):
"""List of supported languages."""
return [
"ab",
"sq",
"ak",
"ar",
"an",
"am",
"as",
"az",
"ast",
"nch",
"ee",
"ay",
"ga",
"et",
"oj",
"oc",
"or",
"om",
"os",
"tpi",
"ba",
"eu",
"be",
"ber",
"bm",
"pag",
"bg",
"se",
"bem",
"byn",
"bi",
"bal",
"is",
"pl",
"bs",
"fa",
"bho",
"br",
"ch",
"cbk",
"cv",
"ts",
"tt",
"da",
"shn",
"tet",
"de",
"nds",
"sco",
"dv",
"kdx",
"dtp",
"ru",
"fo",
"fr",
"sa",
"fil",
"fj",
"fi",
"fur",
"fvr",
"kg",
"km",
"ngu",
"kl",
"ka",
"gos",
"gu",
"gn",
"kk",
"ht",
"ko",
"ha",
"nl",
"cnr",
"hup",
"gil",
"rn",
"quc",
"ky",
"gl",
"ca",
"cs",
"kab",
"kn",
"kr",
"csb",
"kha",
"kw",
"xh",
"co",
"mus",
"crh",
"tlh",
"hbs",
"qu",
"ks",
"ku",
"la",
"ltg",
"lv",
"lo",
"lt",
"li",
"ln",
"lg",
"lb",
"rue",
"rw",
"ro",
"rm",
"rom",
"jbo",
"mg",
"gv",
"mt",
"mr",
"ml",
"ms",
"chm",
"mk",
"mh",
"kek",
"mai",
"mfe",
"mi",
"mn",
"bn",
"my",
"hmn",
"umb",
"nv",
"af",
"ne",
"niu",
"no",
"pmn",
"pap",
"pa",
"pt",
"ps",
"ny",
"tw",
"chr",
"ja",
"sv",
"sm",
"sg",
"si",
"hsb",
"eo",
"sl",
"sw",
"so",
"sk",
"tl",
"tg",
"ty",
"te",
"ta",
"th",
"to",
"toi",
"ti",
"tvl",
"tyv",
"tr",
"tk",
"wa",
"war",
"cy",
"ve",
"vo",
"wo",
"udm",
"ur",
"uz",
"es",
"ie",
"fy",
"szl",
"he",
"hil",
"haw",
"el",
"lfn",
"sd",
"hu",
"sn",
"ceb",
"syr",
"su",
"hy",
"ace",
"iba",
"ig",
"io",
"ilo",
"iu",
"it",
"yi",
"ia",
"hi",
"id",
"inh",
"en",
"yo",
"vi",
"zza",
"jv",
"zh",
"zh-tw",
"yue",
"zu",
]

def download_translations(
self,
source,
language,
text: str,
unit,
user,
threshold: int = 75,
):
"""Download list of possible translations from a service."""
# Create an API request and set the request parameters.
request = TranslateGeneralRequest.TranslateGeneralRequest()
request.set_SourceLanguage(source) # source language
request.set_SourceText(text) # original
request.set_TargetLanguage(language)
request.set_FormatType("text")
request.set_method("POST")

# Initiate the API request and obtain the response.
response = self.client.do_action_with_exception(request)
payload = json.loads(response)
if "Message" in payload:
raise MachineTranslationError(

Check warning on line 278 in weblate/machinery/alibaba.py

View check run for this annotation

Codecov / codecov/patch

weblate/machinery/alibaba.py#L278

Added line #L278 was not covered by tests
f"Error {payload['Code']}: {payload['Message']}"
)

yield {
"text": payload["Data"]["Translated"],
"quality": self.max_score,
"service": self.name,
"source": text,
}
12 changes: 12 additions & 0 deletions weblate/machinery/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ class AWSMachineryForm(KeySecretMachineryForm):
)


class AlibabaMachineryForm(KeySecretMachineryForm):
key = forms.CharField(
label=pgettext_lazy("Alibaba Translate configuration", "Access key ID")
)
secret = forms.CharField(
label=pgettext_lazy("Alibaba Translate configuration", "Access key secret")
)
region = forms.CharField(
label=pgettext_lazy("Alibaba Translate configuration", "Region ID")
)


class ModernMTMachineryForm(KeyURLMachineryForm):
url = forms.URLField(
label=pgettext_lazy("Automatic suggestion service configuration", "API URL"),
Expand Down
1 change: 1 addition & 0 deletions weblate/machinery/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class WeblateConf(AppConf):
WEBLATE_MACHINERY = (
"weblate.machinery.apertium.ApertiumAPYTranslation",
"weblate.machinery.aws.AWSTranslation",
"weblate.machinery.alibaba.AlibabaTranslation",
"weblate.machinery.baidu.BaiduTranslation",
"weblate.machinery.deepl.DeepLTranslation",
"weblate.machinery.glosbe.GlosbeTranslation",
Expand Down
Loading
Loading