Skip to content

Commit

Permalink
fix(machinery): avoid mixing translations with different replacements
Browse files Browse the repository at this point in the history
The same text and different replacements would use first replacements
only because the text is replaced in the translations dictionary.

Fixes #11179
  • Loading branch information
nijel committed Jun 18, 2024
1 parent ba9f236 commit d53bce1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Released on June 19th 2024.
* Fixed behavior of some site-wide :ref:`addons`.
* Saving strings needing editing to :doc:`/formats/winrc`.
* :ref:`check-xml-tags` better handle XML entities.
* Automatic suggestions could mix up replacements between translated strings.

**Compatibility**

Expand Down
11 changes: 7 additions & 4 deletions weblate/machinery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,16 @@ def _translate(
# Postprocess translations
for text, result in translations.items():
for _unit, original_source, replacements in pending[text]:
for item in result:
# Always operate on copy of the dictionaries
partial = [x.copy() for x in result]

for item in partial:
item["original_source"] = original_source
if cache_key:
cache.set(cache_key, result, 30 * 86400)
cache.set(cache_key, partial, 30 * 86400)
if replacements or self.force_uncleanup:
self.uncleanup_results(replacements, result)
output[original_source] = result
self.uncleanup_results(replacements, partial)
output[original_source] = partial
return output

def get_error_message(self, exc: Exception) -> str:
Expand Down
10 changes: 10 additions & 0 deletions weblate/machinery/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ def test_placeholders(self) -> None:
],
)

def test_batch(self):
machine_translation = self.get_machine()
units = [
MockUnit(code="cs", source="Hello, %s!", flags="c-format"),
MockUnit(code="cs", source="Hello, %d!", flags="c-format"),
]
machine_translation.batch_translate(units)
self.assertEqual(units[0].machinery["translation"], ["Nazdar %s!"])
self.assertEqual(units[1].machinery["translation"], ["Nazdar %d!"])

def test_key(self) -> None:
machine_translation = self.get_machine()
self.assertEqual(
Expand Down

0 comments on commit d53bce1

Please # to comment.