Skip to content

Commit

Permalink
chore: refactor translations sync tool
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Aug 23, 2023
1 parent 4d1b574 commit a38cccf
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions scripts/sync_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import requests
from transifex.api import transifex_api

NEW_PROJECT_SLUG = 'openedx-translations'
ORGANIZATION_SLUG = 'open-edx'

def get_transifex_organization_projects(environ, tx_api, organization_slug):

def get_transifex_organization_projects(environ, tx_api):
"""
Get openedx-translations project from Transifex.
"""
Expand All @@ -25,42 +28,44 @@ def get_transifex_organization_projects(environ, tx_api, organization_slug):
)

tx_api.setup(auth=tx_api_token)
return tx_api.Organization.get(slug=organization_slug).fetch('projects')
return tx_api.Organization.get(slug=ORGANIZATION_SLUG).fetch('projects')


def sync_pair_into_new_resource(environ, tx_api, debug=False):
new_project_slug = 'openedx-translations'
organization_slug = 'open-edx'

languages = environ['TX_LANGUAGES'].split(',')
projects = get_transifex_organization_projects(
environ=environ,
tx_api=tx_api,
organization_slug=organization_slug,
)
new_project = projects.get(slug=new_project_slug)

resource_slug = environ['TX_NEW_SLUG']
resource_id = f'o:{organization_slug}:p:{new_project.slug}:r:{resource_slug}'
new_resource = tx_api.Resource.get(id=resource_id)
def get_resources_pair():
projects = get_transifex_organization_projects(
environ=environ,
tx_api=tx_api,
)
new_project = projects.get(slug=NEW_PROJECT_SLUG)

resource_slug = environ['TX_NEW_SLUG']
resource_id = f'o:{ORGANIZATION_SLUG}:p:{new_project.slug}:r:{resource_slug}'
new_resource = tx_api.Resource.get(id=resource_id)

if resource_name := environ.get('TX_NEW_NAME'):
# Validate that the resource name is correct, useful for avoiding typos in
# the list of resources in GitHub Actions.
assert new_resource.name == resource_name
print(new_resource, new_resource.name, new_resource.id)
if resource_name := environ.get('TX_NEW_NAME'):
# Validate that the resource name is correct, useful for avoiding typos in
# the list of resources in GitHub Actions.
assert new_resource.name == resource_name
print(new_resource, new_resource.name, new_resource.id)

pair_id = f'o:{organization_slug}:p:{environ["TX_OLD_PROJECT_SLUG"]}:r:{environ["TX_OLD_SLUG"]}'
pair_resource = tx_api.Resource.get(id=pair_id)
print(pair_resource, pair_resource.name)
pair_id = f'o:{ORGANIZATION_SLUG}:p:{environ["TX_OLD_PROJECT_SLUG"]}:r:{environ["TX_OLD_SLUG"]}'
old_resource = tx_api.Resource.get(id=pair_id)
print(old_resource, old_resource.name)

print(f'Syncing {new_resource.name} from {pair_resource.name}...')
print(f'Syncing {new_resource.name} from {old_resource.name}...')
return {
'old_resource': old_resource,
'new_resource': new_resource,
}

for language in languages:
def sync_language(language, old_resource, new_resource):
try:
language_obj = tx_api.Language.get(code=language)
url = tx_api.ResourceTranslationsAsyncDownload. \
download(resource=pair_resource, language=language_obj)
download(resource=old_resource, language=language_obj)
translated_content = requests.get(url).text
print(translated_content)

Expand All @@ -77,6 +82,9 @@ def sync_pair_into_new_resource(environ, tx_api, debug=False):
else:
raise e

for lang in languages:
sync_language(language=lang, **get_resources_pair())


if __name__ == '__main__':
sync_pair_into_new_resource(
Expand Down

0 comments on commit a38cccf

Please sign in to comment.