Coverage for app/venv/lib/python3.14/site-packages/weblate/machinery/saptranslationhub.py: 25%

43 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 07:15 +0000

1# Copyright © Manuel Laggner <manuel.laggner@egger.com> 

2# Copyright © Michal Čihař <michal@weblate.org> 

3# 

4# SPDX-License-Identifier: GPL-3.0-or-later 

5 

6from __future__ import annotations 

7 

8from typing import TYPE_CHECKING 

9 

10from .base import MachineTranslation 

11from .forms import SAPMachineryForm 

12 

13if TYPE_CHECKING: 13 ↛ 14line 13 didn't jump to line 14 because the condition on line 13 was never true

14 from requests.auth import AuthBase 

15 

16 from .base import DownloadTranslations 

17 

18 

19class SAPTranslationHub(MachineTranslation): 

20 # https://api.sap.com/api/translationhub/overview 

21 name = "SAP Translation Hub" 

22 settings_form = SAPMachineryForm 

23 

24 @property 

25 def api_base_url(self): 

26 base = super().api_base_url 

27 if base.endswith("/v1"): 

28 return base 

29 return f"{base}/v1" 

30 

31 def get_headers(self): 

32 """Add authentication headers to request.""" 

33 # to access the sandbox 

34 result = {} 

35 if self.settings["key"]: 

36 result["APIKey"] = self.settings["key"] 

37 

38 return result 

39 

40 def get_auth(self) -> tuple[str, str] | AuthBase | None: 

41 # to access the productive API 

42 if self.settings["username"] and self.settings["password"]: 

43 return (self.settings["username"], self.settings["password"]) 

44 return None 

45 

46 def download_languages(self): 

47 """Get all available languages from SAP Translation Hub.""" 

48 # get all available languages 

49 response = self.request("get", self.get_api_url("languages")) 

50 payload = response.json() 

51 

52 return [d["id"] for d in payload["languages"]] 

53 

54 def download_translations( 

55 self, 

56 source_language, 

57 target_language, 

58 text: str, 

59 unit, 

60 user, 

61 threshold: int = 75, 

62 ) -> DownloadTranslations: 

63 """Download list of possible translations from a service.""" 

64 # should the machine translation service be used? 

65 # (rather than only the term database) 

66 enable_mt = self.settings["enable_mt"] 

67 

68 # build the json body 

69 data = { 

70 "targetLanguages": [target_language], 

71 "sourceLanguage": source_language, 

72 "enableMT": enable_mt, 

73 "enableTranslationQualityEstimation": enable_mt, 

74 "units": [{"value": text}], 

75 } 

76 

77 # Include domain if set 

78 domain = self.settings.get("domain") 

79 if domain: 

80 data["domain"] = domain 

81 

82 # perform the request 

83 response = self.request("post", self.get_api_url("translate"), json=data) 

84 payload = response.json() 

85 

86 # prepare the translations for weblate 

87 for item in payload["units"]: 

88 for translation in item["translations"]: 

89 quality = translation.get("qualityIndex", 100) 

90 if quality < threshold: 

91 continue 

92 yield { 

93 "text": translation["value"], 

94 "quality": quality, 

95 "show_quality": "qualityIndex" in translation, 

96 "service": self.name, 

97 "source": text, 

98 }