diff --git a/scripts/sync_translations.py b/scripts/sync_translations.py index c845abe081b..6f9870e9e43 100644 --- a/scripts/sync_translations.py +++ b/scripts/sync_translations.py @@ -107,7 +107,7 @@ def sync_translations(self, language_code, old_resource, new_resource): translation_id = self.get_translation_id(new_translation) if old_translation := old_translations.get(translation_id): updates = {} - for attr in ['reviewed', 'proofread', 'strings', 'tags']: + for attr in ['reviewed', 'proofread', 'strings']: if old_attr_value := getattr(old_translation, attr, None): if old_attr_value != getattr(new_translation, attr, None): updates[attr] = old_attr_value @@ -118,6 +118,38 @@ def sync_translations(self, language_code, old_resource, new_resource): if not self.is_dry_run(): new_translation.save(**updates) + def sync_tags(self, old_resource, new_resource): + """ + Sync tags from the old Transifex resource into the new Transifex resource. This process is language independent. + """ + old_resource_str = self.tx_api.ResourceString.filter(resource=old_resource) + new_resource_str = self.tx_api.ResourceString.filter(resource=new_resource) + + old_quick_lookup = { + item['attributes']['string_hash']: item['attributes']['tags'] for item in old_resource_str.to_dict()['data'] + } + + for new_info in new_resource_str.all(): + old_tags = old_quick_lookup.get(new_info.string_hash) + if old_tags is None: + continue + + save_needed = False + new_tags = new_info.tags + if len(new_tags) != len(old_tags): + save_needed = True + elif len(new_tags) == 0 and len(old_tags) == 0: + continue + + if not save_needed and set(new_tags) != set(old_tags): + save_needed = True + + if save_needed: + print(f' - found tag difference for {new_info.string_hash}. overwriting: {new_tags} with {old_tags}') + + if not self.is_dry_run(): + new_info.save(tags=old_tags) + def get_translation_id(self, translation): """ Build a unique identifier for a translation entry. @@ -145,6 +177,9 @@ def sync_pair_into_new_resource(self, new_slug, old_slug, old_project_slug): for lang_code in languages: self.sync_translations(language_code=lang_code, **resource_pair) + print('Syncing tags...') + self.sync_tags(**resource_pair) + print('-' * 80, '\n') def run_from_workflow_yaml_file(self, workflow_configs):