Skip to content

Commit 184db3b

Browse files
authored
Add composed Machinery suggestions (#4236)
Multi-value entries, i.e. Fluent messages with attributes or selectors, had no Machinery entry that filled every field at once: each leaf got its own TM or MT match, and translators copied them one by one. Add /machinery-composed/, which walks an entity leaf by leaf through the pretranslation pipeline (a 100% TM match, falling back to the requested MT service) and returns the composed (value, properties) data model, i.e. the same JSON shape entities use. The frontend shows it above the per-leaf matches as labeled fields, and copies it into all editor fields at once. TM lookups exclude the entity's own entries, so a translated string is not suggested back to itself. Along the way: collapse the MT service arguments into an MTEngine enum, move source-string parsing into pontoon.translations, and share the editor's field-building logic with the Machinery panel. Also fix #4330: accesskey attributes are now matched case-insensitively, in pretranslation and in the editor's accesskey picker alike. That bug predates this work; composing whole entries only made it visible.
1 parent 596f760 commit 184db3b

43 files changed

Lines changed: 1748 additions & 273 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎pontoon/api/tests/test_views.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ def test_pretranslation_tm(member):
16931693
format="fluent",
16941694
)
16951695
entity_b = EntityFactory.create(
1696-
string="Entity B",
1696+
string="entity-b = Entity B\n",
16971697
resource=resource_b,
16981698
)
16991699
project_c = ProjectFactory(

‎pontoon/api/views.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
Term,
3939
TermTranslation,
4040
)
41+
from pontoon.translations.utils import parse_source_string_to_json
4142

4243
from .serializers import (
4344
TRANSLATION_STATS_FIELDS,
@@ -579,11 +580,28 @@ def post(self, request):
579580

580581
locale = generics.get_object_or_404(Locale, code=locale)
581582

583+
fmt = resource_format or None
582584
project = SimpleNamespace(slug="temp-project")
583-
resource = SimpleNamespace(project=project, format=resource_format or None)
584-
entity = SimpleNamespace(resource=resource, string=text)
585-
585+
resource = SimpleNamespace(project=project, format=fmt)
586+
587+
# TODO: refactor `get_pretranslation` (and `Pretranslation`) to accept the
588+
# parsed `(value, properties)` directly, so callers not backed by a synced
589+
# row don't need to fake `Project`, `Resource`, and `Entity` objects just
590+
# to satisfy the attribute access in the pipeline. `key` is only read by
591+
# `Pretranslation.serialize()`, to id an Entry that `set_accesskey()`
592+
# ignores — once that serialization goes away, `parse_source_string_to_json`
593+
# can stop returning an identifier nothing uses.
586594
try:
595+
# Parsing happens here (not before) so invalid syntax for the chosen
596+
# format is reported as a 400 rather than escaping as a 500.
597+
key, value, properties = parse_source_string_to_json(fmt, text)
598+
entity = SimpleNamespace(
599+
resource=resource,
600+
string=text,
601+
key=key,
602+
value=value,
603+
properties=properties,
604+
)
587605
pretranslation = get_pretranslation(entity=entity, locale=locale)
588606
except Exception as e:
589607
return Response(

‎pontoon/base/tests/models/test_entity.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def entity_test_models(translation_a, locale_b):
3636
)
3737
entity_a.string = "Entity zero"
3838
entity_a.key = [entity_a.string]
39+
entity_a.value = [entity_a.string]
3940
entity_a.order = 0
4041
entity_a.save()
4142
entity_b = EntityFactory(
@@ -294,7 +295,7 @@ def test_entity_project_locale_no_paths(
294295
},
295296
"pk": entity_a.pk,
296297
"original": entity_a.string,
297-
"value": [],
298+
"value": ["Entity zero"],
298299
"date_created": entity_a.date_created,
299300
}
300301
assert e1["path"] == trX.entity.resource.path

‎pontoon/batch/actions.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pontoon.base.models.translation import TranslationQuerySet
1313
from pontoon.batch import utils
1414
from pontoon.messaging.notifications import send_badge_notification
15-
from pontoon.translations.utils import parse_db_string_to_json
15+
from pontoon.translations.utils import parse_source_string_to_json
1616

1717

1818
def batch_action_template(form, user, translations, locale):
@@ -315,7 +315,7 @@ def copy_translation_from_locale(
315315
if not other_locale:
316316
# Copy from other locale (entity.string directly)
317317
for entity in entities:
318-
value, properties = parse_db_string_to_json(
318+
_, value, properties = parse_source_string_to_json(
319319
entity.resource.format, entity.string
320320
)
321321
translations_to_create.append(
@@ -334,7 +334,7 @@ def copy_translation_from_locale(
334334
)
335335
else:
336336
for t in other_locale_translations:
337-
value, properties = parse_db_string_to_json(
337+
_, value, properties = parse_source_string_to_json(
338338
t.entity.resource.format, t.string
339339
)
340340
translations_to_create.append(

‎pontoon/batch/utils.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pontoon.base.models.translation import TranslationQuerySet
1313
from pontoon.checks import DB_FORMATS
1414
from pontoon.checks.libraries import run_checks
15-
from pontoon.translations.utils import parse_db_string_to_json
15+
from pontoon.translations.utils import parse_source_string_to_json
1616

1717

1818
parser = FluentParser()
@@ -126,8 +126,8 @@ def find_and_replace(
126126

127127
errors = False
128128
try:
129-
new_translation.value, new_translation.properties = parse_db_string_to_json(
130-
res_format, new_translation.string
129+
_, new_translation.value, new_translation.properties = (
130+
parse_source_string_to_json(res_format, new_translation.string)
131131
)
132132
except ValueError:
133133
errors = True

0 commit comments

Comments
 (0)