Skip to content

Commit

Permalink
feat: include tags in sync proccess
Browse files Browse the repository at this point in the history
Refs: FC-0012 OEP-58
  • Loading branch information
shadinaif committed Sep 18, 2023
1 parent 0d17e52 commit 2c7c3a8
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion scripts/sync_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 2c7c3a8

Please sign in to comment.