diff --git a/scripts/sync_translations.py b/scripts/sync_translations.py index a3a2f98b00b..3f7425a07ad 100644 --- a/scripts/sync_translations.py +++ b/scripts/sync_translations.py @@ -61,29 +61,52 @@ def get_resources_pair(): 'new_resource': new_resource, } - def sync_language(language, old_resource, new_resource): - try: - language_obj = tx_api.Language.get(code=language) - url = tx_api.ResourceTranslationsAsyncDownload. \ - download(resource=old_resource, language=language_obj) - translated_content = requests.get(url).text - print(translated_content) - - tx_api.ResourceTranslationsAsyncUpload.upload( - resource=new_resource, - language=f'l:{language}', - content=translated_content, - ) - except Exception as e: - if debug: - print(e) - import pdb - pdb.set_trace() - else: - raise e - - for lang in languages: - sync_language(language=lang, **get_resources_pair()) + def get_translations(language_code, resource): + language = transifex_api.Language.get(code=language_code) + translations = transifex_api.ResourceTranslation. \ + filter(resource=resource, language=language). \ + include('resource_string') + + return translations.all() + + def get_translation_id(translation): + return f'context:{translation.resource_string.context}:key:{translation.resource_string.key}' + # + # def sync_language(language_code, old_resource, new_resource): + # language = tx_api.Language.get(code=language_code) + # url = tx_api.ResourceTranslationsAsyncDownload. \ + # download(resource=old_resource, language=language) + # translated_content = requests.get(url).text + # + # tx_api.ResourceTranslationsAsyncUpload.upload( + # resource=new_resource, + # language=f'l:{language_code}', + # content=translated_content, + # ) + + def sync_translations(language_code, old_resource, new_resource): + old_translations = { + get_translation_id(translation): translation + for translation in get_translations(language_code=language_code, resource=old_resource) + } + + for new_translation in get_translations(language_code=language_code, resource=new_resource): + translation_id = get_translation_id(new_translation) + if old_translation := old_translations.get(translation_id): + updates = {} + for attr in ['reviewed', 'proofread', 'strings', 'tags']: + if old_attr_value := getattr(old_translation, attr, None): + if old_attr_value != getattr(new_translation, attr, None): + updates[attr] = old_attr_value + + if updates: + print(translation_id, updates) + new_translation.save(**updates) + + for lang_code in languages: + resource_pair = get_resources_pair() + # sync_language(language_code=lang_code, **resource_pair) + sync_translations(language_code=lang_code, **resource_pair) if __name__ == '__main__':