From 3c3719bb4ea4c9782afee72a87e379798f77bf16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Tue, 17 Dec 2024 13:18:57 +0100 Subject: [PATCH] fix(autofix): correctly apply autofix in various conditions - Apply autofixes after adjusting plurals. This way the fixer gets a correct number of plurals. - Gracefully handle different number of plurals as these might not be plurals, but multi value strings. Fixes WEBLATE-12CM Fixes WEBLATE-1CZ9 --- docs/changes.rst | 1 + weblate/trans/autofixes/base.py | 12 ++++++++---- weblate/trans/models/unit.py | 8 ++++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index c257e7b30022..a050466b3247 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -13,6 +13,7 @@ Not yet released. **Bug fixes** * Avoid query parser crash in multi-threaded environments. +* Avoid :ref:`autofix` crash on multi-value strings. **Compatibility** diff --git a/weblate/trans/autofixes/base.py b/weblate/trans/autofixes/base.py index ca6fcb9be35e..9702e72d09dd 100644 --- a/weblate/trans/autofixes/base.py +++ b/weblate/trans/autofixes/base.py @@ -45,15 +45,19 @@ def fix_target(self, target: str, unit: Unit) -> tuple[list[str], bool]: for number, examples in source_plural.examples.items() } target_examples = target_plural.examples - plurals_map = [ - source_examples.get(tuple(target_examples.get(target_index, [])), -1) + plurals_map: dict[int, int] = { + target_index: source_examples.get( + tuple(target_examples.get(target_index, [])), -1 + ) for target_index in range(target_plural.number) - ] + } # Ensure we have a source strings for each map while len(source_strings) <= max(plurals_map): source_strings.append(source_strings[0]) results = [ - self.fix_single_target(text, source_strings[plurals_map[i]], unit) + self.fix_single_target( + text, source_strings[plurals_map.get(i, -1)], unit + ) for i, text in enumerate(target) ] return [r[0] for r in results], max(r[1] for r in results) diff --git a/weblate/trans/models/unit.py b/weblate/trans/models/unit.py index 865b30f52ec8..0b469db3c451 100644 --- a/weblate/trans/models/unit.py +++ b/weblate/trans/models/unit.py @@ -1564,10 +1564,6 @@ def translate( if isinstance(new_target, str): new_target = [new_target] - # Apply autofixes - if not self.translation.is_template: - new_target, self.fixups = fix_target(new_target, self) - # Handle managing alternative translations if add_alternative: new_target.append("") @@ -1579,6 +1575,10 @@ def translate( if not component.is_multivalue: new_target = self.adjust_plurals(new_target) + # Apply autofixes + if not self.translation.is_template: + new_target, self.fixups = fix_target(new_target, self) + # Update unit and save it self.target = join_plural(new_target) not_empty = any(new_target)