From 1471d7ec1b351dc25de2447d55c76fa6fd209357 Mon Sep 17 00:00:00 2001 From: Omar Al-Ithawi Date: Thu, 9 Jan 2025 20:06:33 +0300 Subject: [PATCH] feat: update translations during sync; sync in chuncks of 145 Transifex has a limit of 150 translations per bulk_update call --- scripts/release_project_sync.py | 43 ++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/scripts/release_project_sync.py b/scripts/release_project_sync.py index af5a062036c..411f9503482 100644 --- a/scripts/release_project_sync.py +++ b/scripts/release_project_sync.py @@ -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: @@ -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. @@ -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(): @@ -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 @@ -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)