Skip to content

Commit

Permalink
feat: sync translations, review status and others
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Aug 23, 2023
1 parent a38cccf commit 8c6be3a
Showing 1 changed file with 46 additions and 23 deletions.
69 changes: 46 additions & 23 deletions scripts/sync_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit 8c6be3a

Please sign in to comment.