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

33 statements  

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

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

2# Copyright © Sun Zhigang <hzsunzhigang@corp.netease.com> 

3# 

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

5from __future__ import annotations 

6 

7from typing import TYPE_CHECKING, ClassVar 

8 

9from .base import ( 

10 MachineryRateLimitError, 

11 MachineTranslation, 

12 MachineTranslationError, 

13) 

14from .forms import KeySecretMachineryForm 

15 

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

17 from .base import ( 

18 DownloadTranslations, 

19 ) 

20 

21BAIDU_API = "http://api.fanyi.baidu.com/api/trans/vip/translate" 

22 

23 

24class BaiduTranslation(MachineTranslation): 

25 """Baidu API machine translation support.""" 

26 

27 name = "Baidu" 

28 max_score = 90 

29 

30 # Map codes used by Baidu to codes used by Weblate 

31 language_map: ClassVar[dict[str, str]] = { 

32 "zh_Hans": "zh", 

33 "ja": "jp", 

34 "ko": "kor", 

35 "fr": "fra", 

36 "es": "spa", 

37 "ar": "ara", 

38 "bg": "bul", 

39 "et": "est", 

40 "da": "dan", 

41 "fi": "fin", 

42 "ro": "rom", 

43 # The slo should map to Slovak, but Baidu uses this code for Slovenian 

44 "sl": "slo", 

45 "sw": "swe", 

46 "zh_Hant": "cht", 

47 "vi": "vie", # codespell:ignore vie 

48 } 

49 settings_form = KeySecretMachineryForm 

50 

51 def download_languages(self): 

52 """List of supported languages.""" 

53 return [ 

54 "zh", 

55 "en", 

56 "yue", 

57 "wyw", 

58 "jp", 

59 "kor", 

60 "fra", 

61 "spa", 

62 "th", 

63 "ara", 

64 "ru", 

65 "pt", 

66 "de", 

67 "it", 

68 "el", 

69 "nl", 

70 "pl", 

71 "bul", 

72 "est", 

73 "dan", 

74 "fin", 

75 "cs", 

76 "rom", 

77 "slo", 

78 "swe", 

79 "hu", 

80 "cht", 

81 "vie", # codespell:ignore vie 

82 ] 

83 

84 def check_failure(self, response) -> None: 

85 payload = response.json() 

86 

87 if "error_code" in payload: 

88 try: 

89 error_code = int(payload["error_code"]) 

90 except ValueError: 

91 pass 

92 else: 

93 if error_code == 54003: 

94 raise MachineryRateLimitError(payload["error_msg"]) 

95 msg = "Error {error_code}: {error_msg}".format(**payload) 

96 raise MachineTranslationError(msg) 

97 super().check_failure(response) 

98 

99 def download_translations( 

100 self, 

101 source_language, 

102 target_language, 

103 text: str, 

104 unit, 

105 user, 

106 threshold: int = 75, 

107 ) -> DownloadTranslations: 

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

109 salt, sign = self.signed_salt( 

110 self.settings["key"], self.settings["secret"], text 

111 ) 

112 args = { 

113 "q": text, 

114 "from": source_language, 

115 "to": target_language, 

116 "appid": self.settings["key"], 

117 "salt": salt, 

118 "sign": sign, 

119 } 

120 

121 response = self.request("get", BAIDU_API, params=args) 

122 payload = response.json() 

123 

124 for item in payload["trans_result"]: 

125 yield { 

126 "text": item["dst"], 

127 "quality": self.max_score, 

128 "service": self.name, 

129 "source": item["src"], 

130 }