Coverage for baibu/localization/management/commands/update_translation_sources.py: 100%

19 statements  

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

1from django.core.management.base import BaseCommand 

2from django.core.management.base import CommandError 

3 

4from baibu.localization import services 

5from baibu.localization.extraction import ExtractionError 

6 

7 

8class Command(BaseCommand): 

9 help = ( 

10 "Extract the interface strings from the code (and the deployment's templates) and merge them " 

11 "into every language's translation draft, keeping existing translations." 

12 ) 

13 

14 def add_arguments(self, parser): 

15 parser.add_argument("--force", action="store_true", help="Store the template even if it has not changed.") 

16 

17 def handle(self, *args, force=False, **options): 

18 if not services.translatable_languages(): 

19 self.stdout.write("Only the source language is enabled; nothing to translate.") 

20 return 

21 try: 

22 result = services.update_sources(force=force) 

23 except ExtractionError as exc: 

24 raise CommandError(str(exc)) from exc 

25 state = "updated" if result.created else "unchanged" 

26 merged = ", ".join(result.merged) or "none" 

27 self.stdout.write(f"Source strings {state} ({result.source.entry_count} messages). Drafts updated: {merged}.")