Coverage for app/venv/lib/python3.14/site-packages/weblate/checks/fluent/syntax.py: 57%

41 statements  

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

1# Copyright © Henry Wilkes <henry@torproject.org> 

2# 

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

4 

5from __future__ import annotations 

6 

7from typing import TYPE_CHECKING 

8 

9from django.utils.html import escape 

10from django.utils.translation import gettext, gettext_lazy 

11 

12from weblate.checks.base import SourceCheck, TargetCheck 

13from weblate.checks.fluent.utils import FluentUnitConverter, translation_from_check 

14 

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

16 from django_stubs_ext import StrOrPromise 

17 

18 from weblate.checks.fluent.utils import CheckModel, TransUnitModel 

19 

20 

21class _FluentSyntaxCheck: 

22 @staticmethod 

23 def check_fluent_syntax(source: str, unit: TransUnitModel) -> bool: 

24 syntax_error = FluentUnitConverter(unit, source).get_syntax_error() 

25 return bool(syntax_error) 

26 

27 @staticmethod 

28 def get_fluent_syntax_error_description( 

29 check_model: CheckModel, use_source: bool 

30 ) -> str | None: 

31 unit, source, target = translation_from_check(check_model) 

32 if not use_source: # A target check. 

33 source = target 

34 syntax_error = FluentUnitConverter(unit, source).get_syntax_error() 

35 if not syntax_error: 

36 return None 

37 return escape( 

38 gettext("Fluent syntax error: {error}.").format(error=syntax_error) 

39 ) 

40 

41 

42class FluentSourceSyntaxCheck(_FluentSyntaxCheck, SourceCheck): 

43 """Check that the source uses valid Fluent syntax.""" 

44 

45 check_id = "fluent-source-syntax" 

46 name = gettext_lazy("Fluent source syntax") 

47 description = gettext_lazy("Fluent syntax error in the source.") 

48 default_disabled = True 

49 

50 def check_source_unit(self, source: list[str], unit: TransUnitModel) -> bool: 

51 return self.check_fluent_syntax(source[0], unit) 

52 

53 def get_description(self, check_model: CheckModel) -> StrOrPromise: 

54 return self.get_fluent_syntax_error_description( 

55 check_model, True 

56 ) or super().get_description(check_model) 

57 

58 

59class FluentTargetSyntaxCheck(_FluentSyntaxCheck, TargetCheck): 

60 """Check that the target uses valid Fluent syntax.""" 

61 

62 check_id = "fluent-target-syntax" 

63 name = gettext_lazy("Fluent translation syntax") 

64 description = gettext_lazy("Fluent syntax error in the translation.") 

65 default_disabled = True 

66 

67 def check_single( 

68 self, 

69 source: str, 

70 target: str, 

71 unit: TransUnitModel, 

72 ) -> bool: 

73 return self.check_fluent_syntax(target, unit) 

74 

75 def get_description(self, check_model: CheckModel) -> StrOrPromise: 

76 return self.get_fluent_syntax_error_description( 

77 check_model, False 

78 ) or super().get_description(check_model)