|
| 1 | +from math import ceil |
| 2 | + |
| 3 | +from moz.l10n.formats import Format |
| 4 | +from moz.l10n.formats.fluent import fluent_parse_entry |
| 5 | +from moz.l10n.message import message_to_json, parse_message |
| 6 | +from moz.l10n.model import CatchallKey, PatternMessage, SelectMessage |
| 7 | + |
| 8 | +from django.db import migrations, models |
| 9 | + |
| 10 | + |
| 11 | +batch_size = 10000 |
| 12 | + |
| 13 | + |
| 14 | +def set_value_and_properties(apps, schema_editor): |
| 15 | + Resource = apps.get_model("base", "Resource") |
| 16 | + Translation = apps.get_model("base", "Translation") |
| 17 | + |
| 18 | + batch_total = ceil(Translation.objects.count() / batch_size) |
| 19 | + batch_count = 0 |
| 20 | + |
| 21 | + def print_progress(): |
| 22 | + nonlocal batch_count |
| 23 | + if batch_count % 10 == 0: |
| 24 | + print(f".({(batch_count / batch_total):.1%})", end="", flush=True) |
| 25 | + else: |
| 26 | + print(".", end="", flush=True) |
| 27 | + batch_count += 1 |
| 28 | + |
| 29 | + pv_trans = [] |
| 30 | + v_trans = [] |
| 31 | + format_q = models.Subquery( |
| 32 | + Resource.objects.filter(id=models.OuterRef("entity__resource_id")).values( |
| 33 | + "format" |
| 34 | + ) |
| 35 | + ) |
| 36 | + for trans in Translation.objects.annotate(format=format_q).iterator(): |
| 37 | + string = trans.string |
| 38 | + try: |
| 39 | + match trans.format: |
| 40 | + case "fluent": |
| 41 | + fe = fluent_parse_entry(string, with_linepos=False) |
| 42 | + msg = fe.value |
| 43 | + trans.properties = { |
| 44 | + name: message_to_json(msg) |
| 45 | + for name, msg in fe.properties.items() |
| 46 | + } or None |
| 47 | + case "lang" | "properties" | "": |
| 48 | + msg = PatternMessage([string]) |
| 49 | + case "android" | "gettext" | "webext" | "xcode" | "xliff": |
| 50 | + msg = parse_message(Format.mf2, string) |
| 51 | + case _: |
| 52 | + msg = parse_message(Format[trans.format], string) |
| 53 | + |
| 54 | + # MF2 syntax does not retain the catchall name/label |
| 55 | + if isinstance(msg, SelectMessage) and trans.format != "fluent": |
| 56 | + for keys in msg.variants: |
| 57 | + for key in keys: |
| 58 | + if isinstance(key, CatchallKey): |
| 59 | + key.value = "other" |
| 60 | + |
| 61 | + trans.value = message_to_json(msg) |
| 62 | + if trans.properties: |
| 63 | + pv_trans.append(trans) |
| 64 | + else: |
| 65 | + v_trans.append(trans) |
| 66 | + except Exception: |
| 67 | + if ( |
| 68 | + trans.approved |
| 69 | + and not trans.entity.obsolete |
| 70 | + and not trans.entity.resource.project.disabled |
| 71 | + ): |
| 72 | + print( |
| 73 | + f"\nUsing fallback value for approved and active {trans.format} translation {trans.pk} " |
| 74 | + f"for entity {trans.entity.pk}, locale {trans.locale.code}:\n{trans.string}", |
| 75 | + flush=True, |
| 76 | + ) |
| 77 | + trans.value = [trans.string] |
| 78 | + v_trans.append(trans) |
| 79 | + if len(pv_trans) == batch_size: |
| 80 | + Translation.objects.bulk_update(pv_trans, ["value", "properties"]) |
| 81 | + pv_trans.clear() |
| 82 | + print_progress() |
| 83 | + if len(v_trans) == batch_size: |
| 84 | + Translation.objects.bulk_update(v_trans, ["value"]) |
| 85 | + v_trans.clear() |
| 86 | + print_progress() |
| 87 | + if pv_trans: |
| 88 | + Translation.objects.bulk_update(pv_trans, ["value", "properties"]) |
| 89 | + print_progress() |
| 90 | + if v_trans: |
| 91 | + Translation.objects.bulk_update(v_trans, ["value"]) |
| 92 | + print_progress() |
| 93 | + |
| 94 | + |
| 95 | +class Migration(migrations.Migration): |
| 96 | + dependencies = [("base", "0110_add_translation_value_and_properties_schema")] |
| 97 | + |
| 98 | + operations = [ |
| 99 | + migrations.RunPython( |
| 100 | + set_value_and_properties, reverse_code=migrations.RunPython.noop |
| 101 | + ), |
| 102 | + ] |
0 commit comments