def migrate_ftl_translations_to_0_6_4(apps, schema): """ Converts all FTL translations to the latest (0.5) syntax and serializes them with the latest (0.6.4) serializer to prevent creating duplicate translations on save. Also convert corresponding TranslationMemoryEntries. See bugs 1441020 and 1442201 for more details. """ parser = FluentParser() serializer = FluentSerializer() # Translations Translation = apps.get_model("base", "Translation") translations = Translation.objects.filter(entity__resource__format="ftl") translations_to_update = [] for t in translations: current = t.string ast = parser.parse_entry(current) t.string = serializer.serialize_entry(ast) if t.string != current: translations_to_update.append(t) bulk_update(translations_to_update, update_fields=["string"]) # Translation Memory Entries TranslationMemoryEntry = apps.get_model("base", "TranslationMemoryEntry") updated_pks = [x.pk for x in translations_to_update] tms = TranslationMemoryEntry.objects.filter( translation__pk__in=updated_pks) tms_to_update = [] for tm in tms: current = tm.target ast = parser.parse_entry(current) tm.target = serializer.serialize_entry(ast) if tm.target != current: tms_to_update.append(tm) bulk_update(tms_to_update, update_fields=["target"])
def migrate_ftl_translations_to_0_6_4(apps, schema): """ Converts all FTL translations to the latest (0.5) syntax and serializes them with the latest (0.6.4) serializer to prevent creating duplicate translations on save. Also convert corresponding TranslationMemoryEntries. See bugs 1441020 and 1442201 for more details. """ parser = FluentParser() serializer = FluentSerializer() # Translations Translation = apps.get_model('base', 'Translation') translations = Translation.objects.filter(entity__resource__format='ftl') translations_to_update = [] for t in translations: current = t.string ast = parser.parse_entry(current) t.string = serializer.serialize_entry(ast) if t.string != current: translations_to_update.append(t) bulk_update(translations_to_update, update_fields=['string']) # Translation Memory Entries TranslationMemoryEntry = apps.get_model('base', 'TranslationMemoryEntry') updated_pks = [x.pk for x in translations_to_update] tms = TranslationMemoryEntry.objects.filter(translation__pk__in=updated_pks) tms_to_update = [] for tm in tms: current = tm.target ast = parser.parse_entry(current) tm.target = serializer.serialize_entry(ast) if tm.target != current: tms_to_update.append(tm) bulk_update(tms_to_update, update_fields=['target'])
def remove_comments_from_ftl_translations(apps, schema): """ Remove comments from the string column of FTL Translations and Entities, and source & target columns of FTL TranslationMemoryEntries. See bug 1501168 for more details. """ parser = FluentParser() serializer = FluentSerializer() # Translations Translation = apps.get_model("base", "Translation") translations = Translation.objects.filter(entity__resource__format="ftl") translations_to_update = [] for t in translations: current = t.string ast = parser.parse_entry(current) ast.comment = None t.string = serializer.serialize_entry(ast) if t.string != current: translations_to_update.append(t) bulk_update(translations_to_update, update_fields=["string"]) # Entities Entity = apps.get_model("base", "Entity") entities = Entity.objects.filter(resource__format="ftl") entities_to_update = [] for e in entities: current = e.string ast = parser.parse_entry(current) ast.comment = None e.string = serializer.serialize_entry(ast) if e.string != current: entities_to_update.append(e) bulk_update(entities_to_update, update_fields=["string"]) # Translation Memory Entries TranslationMemoryEntry = apps.get_model("base", "TranslationMemoryEntry") tmes = TranslationMemoryEntry.objects.filter(entity__resource__format="ftl") tmes_to_update = [] for tme in tmes: current_source = tme.source ast = parser.parse_entry(current_source) ast.comment = None tme.source = serializer.serialize_entry(ast) current_target = tme.target ast = parser.parse_entry(current_target) ast.comment = None tme.target = serializer.serialize_entry(ast) if tme.source != current_source or tme.target != current_target: tmes_to_update.append(tme) bulk_update(tmes_to_update, update_fields=["source", "target"])
def pretty_variant_key(text, index): parser = FluentParser() entry = parser.parse_entry(dedent_ftl(text)) variants = entry.value.elements[0].expression.variants return serialize_variant_key(variants[index].key)
def pretty_expr(text): parser = FluentParser() entry = parser.parse_entry(dedent_ftl(text)) expr = entry.value.elements[0].expression return serialize_expression(expr)