Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions pontoon/sync/core/translations_from_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from collections.abc import Iterable, Sized
from datetime import datetime
from os.path import join, relpath, splitext
from typing import Any

from fluent.syntax import FluentParser
from moz.l10n.formats import l10n_extensions
from moz.l10n.message import message_to_json
from moz.l10n.model import Id as L10nId
from moz.l10n.message import message_from_json, message_to_json
from moz.l10n.model import Id as L10nId, Message
from moz.l10n.paths import L10nConfigPaths, L10nDiscoverPaths, parse_android_locale
from moz.l10n.resource import parse_resource

Expand Down Expand Up @@ -213,7 +213,8 @@ def find_db_updates(
"entity__resource__format",
"entity__key",
"locale_id",
"string",
"value",
"properties",
)
)
paginator = Paginator(trans_query, per_page=10000, allow_empty_first_page=True)
Expand All @@ -228,11 +229,10 @@ def find_db_updates(
if key in translations:
rt = translations[key]
if rt is not None and translations_equal(
project,
key[0],
trans_values["entity__resource__format"],
rt.string,
trans_values["string"],
rt.value,
rt.properties,
trans_values["value"],
trans_values["properties"],
):
del translations[key]
else:
Expand Down Expand Up @@ -278,21 +278,20 @@ def find_db_updates(


def translations_equal(
project: Project, db_path: str, format: str, a: object, b: object
value: Message,
properties: dict[str, Message] | None,
json_value: Any,
json_properties: dict[str, Any] | None,
) -> bool:
if a == b:
return True
if not isinstance(a, str) or not isinstance(b, str):
if value != message_from_json(json_value):
return False
if format == Resource.Format.FLUENT:
parser = FluentParser(with_spans=False)
try:
fa = parser.parse(a)
fb = parser.parse(b)
return fa.equals(fb)
except Exception as error:
log.debug(f"[{project.slug}:{db_path}] Parse error: {error}")
return False
if not properties:
return not json_properties
if not json_properties or set(properties.keys()) != set(json_properties.keys()):
return False
return properties == {
name: message_from_json(json) for name, json in json_properties.items()
}


def update_db_translations(
Expand Down
28 changes: 14 additions & 14 deletions pontoon/sync/tests/test_translations_from_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,15 @@ def test_android_translation_changes():
TranslationFactory.create(
entity=e1,
locale=locale,
string="target same",
value=["target same"],
active=True,
approved=True,
)
e2 = EntityFactory.create(resource=res, string="source", key=["k2"])
TranslationFactory.create(
entity=e2,
locale=locale,
string="target spaces changed from normalized",
value=["target spaces changed from normalized"],
active=True,
approved=True,
)
Expand All @@ -195,7 +195,7 @@ def test_android_translation_changes():
TranslationFactory.create(
entity=e3,
locale=locale,
string="target \t spaces \n changed from not normalized",
value=["target \t spaces changed from not normalized"],
active=True,
approved=True,
)
Expand All @@ -204,7 +204,7 @@ def test_android_translation_changes():
TranslationFactory.create(
entity=e4,
locale=locale,
string="target value previous",
value=["target value previous"],
active=True,
approved=True,
)
Expand Down Expand Up @@ -250,14 +250,14 @@ def test_android_translation_changes():

translations = Translation.objects.filter(entity__resource=res, locale=locale)
assert {
(*trans.entity.key, trans.approved, trans.string) for trans in translations
(*trans.entity.key, trans.approved): trans.value for trans in translations
} == {
("k1", True, "target same"),
("k2", True, "target spaces changed from normalized"),
("k3", False, "target \t spaces \n changed from not normalized"),
("k3", True, "target spaces changed from not normalized"),
("k4", False, "target value previous"),
("k4", True, "target value changed"),
("k1", True): ["target same"],
("k2", True): ["target spaces changed from normalized"],
("k3", False): ["target \t spaces changed from not normalized"],
("k3", True): ["target spaces changed from not normalized"],
("k4", False): ["target value previous"],
("k4", True): ["target value changed"],
}
tr_k4 = next(
trans
Expand All @@ -268,11 +268,11 @@ def test_android_translation_changes():

# Test actions
assert {
(action.translation.string, action.action_type)
(action.translation.value[0], action.action_type)
for action in ActionLog.objects.filter(translation__in=translations)
} == {
(
"target \t spaces \n changed from not normalized",
"target \t spaces changed from not normalized",
"translation:rejected",
),
("target spaces changed from not normalized", "translation:created"),
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_ini_translations():
TranslationFactory.create(
entity=ent,
locale=locale,
string="Target",
value=["Target"],
active=True,
approved=True,
)
Expand Down
Loading