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 97f1b24
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 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
12 changes: 8 additions & 4 deletions weblate/trans/autofixes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 97f1b24

Please sign in to comment.