Skip to content

Commit

Permalink
feat: update translations during sync; sync in chuncks of 145
Browse files Browse the repository at this point in the history
Transifex has a limit of 150 translations per bulk_update call
  • Loading branch information
OmarIthawi committed Jan 9, 2025
1 parent e7f3e97 commit 1471d7e
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions scripts/release_project_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
RELEASE_PROJECT_SLUG_TEMPLATE = 'openedx-translations-{release_name}'


def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]



@dataclasses.dataclass
class Command:

Expand Down Expand Up @@ -123,11 +130,11 @@ def sync_translations(self, lang_id, main_resource, release_resource):
})

if updates_to_apply and not self.is_dry_run():
self.tx_api.ResourceTranslation.bulk_update(updates_to_apply)
for updates_chunk in chunks(updates_to_apply, 145):
self.tx_api.ResourceTranslation.bulk_update(updates_chunk)

print(' finished', lang_id)


def determine_translation_updates(self, translation_from_main_project, release_translation):
"""
Compare translations between main and release projects and determine needed updates.
Expand All @@ -144,17 +151,37 @@ def determine_translation_updates(self, translation_from_main_project, release_t

updates = {}

# Only update review status if translations are the same across projects
if translation_from_main_project.strings == release_translation.strings:
def _update_review_proofread_attrs():
for attr in ['reviewed', 'proofread']:
# Reviews won't be deleted in the release project
if main_attr_value := getattr(translation_from_main_project, attr, None):
if main_attr_value != getattr(release_translation, attr, None):
updates[attr] = main_attr_value
else:

if (
translation_from_main_project.strings
and release_translation.strings
and translation_from_main_project.strings != release_translation.strings
):
# Do not override anything if translations are different
print(translation_id, 'has different translations will not update it')
return 'no-op', updates

if (
translation_from_main_project.strings
and translation_from_main_project.strings == release_translation.strings
):
# Only update review status if translations are the same across projects
_update_review_proofread_attrs()

if (
translation_from_main_project.strings
and not release_translation.strings
):
# Set translations from the old project and update review status
updates['strings'] = translation_from_main_project.strings
_update_review_proofread_attrs()

if updates:
print(translation_id, updates, '[Dry run]' if self.is_dry_run() else '')
if self.is_dry_run():
Expand All @@ -178,6 +205,10 @@ def sync_tags(self, main_resource, release_resource):
dict_item = item.to_dict()
main_quick_lookup[dict_item['attributes']['string_hash']] = dict_item['attributes']['tags']

dry_run_note = ''
if self.is_dry_run():
dry_run_note = ' (dry-run)'

for release_info in release_resource_str.all():
main_tags = main_quick_lookup.get(release_info.string_hash)
release_tags = release_info.tags
Expand All @@ -188,7 +219,7 @@ def sync_tags(self, main_resource, release_resource):
continue

if len(release_tags) != len(main_tags) or set(release_tags) != set(main_tags):
print(f' - found tag difference for {release_info.string_hash}. overwriting: {release_tags} with {main_tags}')
print(f' - found tag difference for {release_info.string_hash}. overwriting{dry_run_note}: {release_tags} with {main_tags}')

if not self.is_dry_run():
release_info.save(tags=main_tags)
Expand Down

0 comments on commit 1471d7e

Please sign in to comment.