Skip to content

Commit

Permalink
fix(autofix): correctly apply autofix in various conditions
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
nijel committed Dec 17, 2024
1 parent 03d447b commit 997c468
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Not yet released.
**Bug fixes**

* Avoid query parser crash in multi-threaded environments.
* Avoid :ref:`autofix` crash on multi-value strings.

**Compatibility**

Expand Down
4 changes: 3 additions & 1 deletion weblate/trans/autofixes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def fix_target(self, target: str, unit: Unit) -> tuple[list[str], bool]:
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

Check failure on line 57 in weblate/trans/autofixes/base.py

View workflow job for this annotation

GitHub Actions / mypy

"list[Any]" has no attribute "get"
)
for i, text in enumerate(target)
]
return [r[0] for r in results], max(r[1] for r in results)
8 changes: 4 additions & 4 deletions weblate/trans/models/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand All @@ -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)
Expand Down

0 comments on commit 997c468

Please sign in to comment.