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

23 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# 

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

4from __future__ import annotations 

5 

6from typing import TYPE_CHECKING 

7 

8from .base import MachineTranslation 

9 

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

11 from .base import DownloadTranslations 

12 

13 

14class GlosbeTranslation(MachineTranslation): 

15 """Glosbe machine translation support.""" 

16 

17 name = "Glosbe" 

18 max_score = 90 

19 do_cleanup = False 

20 

21 def map_code_code(self, code): 

22 """Convert language to service specific code.""" 

23 return code.replace("_", "-").split("-")[0].lower() 

24 

25 def is_supported(self, source_language, target_language) -> bool: 

26 """Any language is supported.""" 

27 return True 

28 

29 def download_translations( 

30 self, 

31 source_language, 

32 target_language, 

33 text: str, 

34 unit, 

35 user, 

36 threshold: int = 75, 

37 ) -> DownloadTranslations: 

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

39 params = { 

40 "from": source_language, 

41 "dest": target_language, 

42 "format": "json", 

43 "phrase": text, 

44 } 

45 response = self.request( 

46 "get", "https://glosbe.com/gapi/translate", params=params 

47 ) 

48 payload = response.json() 

49 

50 if "tuc" not in payload: 

51 return 

52 

53 for match in payload["tuc"]: 

54 if "phrase" not in match or match["phrase"] is None: 

55 continue 

56 yield { 

57 "text": match["phrase"]["text"], 

58 "quality": self.max_score, 

59 "service": self.name, 

60 "source": text, 

61 }