Skip to content

Commit

Permalink
feat: sync from old transifex project
Browse files Browse the repository at this point in the history
This is a on-demand GitHub Actions workflow which will sync traslations
and their status from the `open-edx/edx-platform` Transifex project
(old) into `open-edx/openedx-translations` OEP-58 project (new).
  • Loading branch information
OmarIthawi committed Aug 20, 2023
1 parent 2c69144 commit 4543d65
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 0 deletions.
158 changes: 158 additions & 0 deletions .github/workflows/migrate-from-transifex-old-project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
name: Migrate translations from the old Transifex project

on:
# schedule: # TODO: Enable before merge
# # https://crontab.guru/#0_0_*_*_1
# # At midnight on Mondays
# - cron: 0 0 * * 1"
workflow_dispatch:
# push:
# branches: [sync_translations] # TODO: Remove before merge

jobs:
migrate-translations:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
batch:
- new_name: edx-ora2
new_slug: ae518fadaa8a0d0027a1b36c4e6e367a
old_slug: openassessment
old_project_slug: edx-platform

- new_name: edx-ora2-js
new_slug: 0b27792ea213cebf5cddad529a8cd442
old_slug: openassessment-js
old_project_slug: edx-platform

# - new_name: edx-proctoring
# new_slug: 66cfa04d24daa4089acf61c5b11ee883
# old_slug: edx-proctoring
# old_project_slug: edx-platform
#
# - new_name: studio-frontend
# new_slug: 862137d25aff8ed766d3dec238ce833a
# old_slug: studio-frontend
# old_project_slug: edx-platform
#
# - new_name: DoneXBlock
# new_slug: b8933764bdb3063ca09d6aa20341102f
# old_slug: xblock-done
# old_project_slug: xblocks
#
# - new_name: xblock-drag-and-drop-v2
# new_slug: bf47016dfec2eaf5710e1890d0ee0188
# old_slug: drag-and-drop-v2
# old_project_slug: xblocks
#
# - new_name: xblock-free-text-response
# new_slug: f3036886abbc04d70445374bc01e562e
# old_slug: xblock-free-text-response
# old_project_slug: xblocks
#
# - new_name: course-discovery
# new_slug: translations-course-discovery-course-discovery-conf-locale-en-lc-messages-django-po--main
# old_slug: course_discovery
# old_project_slug: edx-platform
#
# - new_name: course-discovery-js
# new_slug: translations-course-discovery-course-discovery-conf-locale-en-lc-messages-djangojs-po--main
# old_slug: course_discovery
# old_project_slug: edx-platform
#
# - new_name: credentials-credentials
# new_slug: translations-credentials-credentials-conf-locale-en-lc-messages-djangojs-po--main
# old_slug: credentials
# old_project_slug: edx-platform
#
# - new_name: frontend-app-account
# new_slug: translations-frontend-app-account-src-i18n-transifex-input-json--main
# old_slug: frontend-app-account
# old_project_slug: edx-platform
#
# - new_name: frontend-app-authn
# new_slug: translations-frontend-app-authn-src-i18n-transifex-input-json--main
# old_slug: frontend-app-authn
# old_project_slug: edx-platform
#
# - new_name: frontend-app-course-authoring
# new_slug: translations-frontend-app-course-authoring-src-i18n-transifex-input-json--main
# old_slug: frontend-app-course-authoring
# old_project_slug: edx-platform
#
# - new_name: frontend-app-discussions
# new_slug: translations-frontend-app-discussions-src-i18n-transifex-input-json--main
# old_slug: frontend-app-discussions
# old_project_slug: edx-platform
#
# - new_name: frontend-app-ecommerce
# new_slug: translations-frontend-app-ecommerce-src-i18n-transifex-input-json--main
# old_slug: frontend-app-ecommerce
# old_project_slug: edx-platform
#
# - new_name: frontend-app-gradebook
# new_slug: translations-frontend-app-gradebook-src-i18n-transifex-input-json--main
# old_slug: frontend-app-gradebook
# old_project_slug: edx-platform
#
# - new_name: frontend-app-learner-dashboard
# new_slug: translations-frontend-app-learner-dashboard-src-i18n-transifex-input-json--main
# old_slug: frontend-app-learner-dashboard
# old_project_slug: edx-platform
#
# - new_name: frontend-app-learner-record
# new_slug: translations-frontend-app-learner-record-src-i18n-transifex-input-json--main
# old_slug: frontend-app-learner-record
# old_project_slug: edx-platform
#
- new_name: frontend-app-learning
new_slug: translations-frontend-app-learning-src-i18n-transifex-input-json--main
old_slug: frontend-app-learning
old_project_slug: edx-platform
#
# - new_name: frontend-app-profile
# new_slug: translations-frontend-app-profile-src-i18n-transifex-input-json--main
# old_slug: frontend-app-profile
# old_project_slug: edx-platform
#
# - new_name: frontend-app-program-console
# new_slug: translations-frontend-app-program-console-src-i18n-transifex-input-json--main
# old_slug: frontend-app-program-manager
# old_project_slug: edx-platform
#
# - new_name: frontend-component-footer
# new_slug: translations-frontend-component-footer-src-i18n-transifex-input-json--main
# old_slug: frontend-component-footer-edx
# old_project_slug: edx-platform
#
# - new_name: frontend-component-header
# new_slug: translations-frontend-component-header-src-i18n-transifex-input-json--main
# old_slug: frontend-component-header
# old_project_slug: edx-platform
#
# - new_name: paragon
# new_slug: translations-paragon-src-i18n-transifex-input-json--main
# old_slug: paragon
# old_project_slug: edx-platform


# if: ${{ !github.event.inputs.resource || github.event.inputs.resource == matrix.new_resource_slug }}
steps:
- uses: actions/checkout@v3
- name: setup python
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Install Python dependencies
run: make sync_requirements

- name: Sync
env:
TX_NEW_NAME: ${{ matrix.batch.new_name }}
TX_NEW_SLUG: ${{ matrix.batch.new_slug }}
TX_OLD_SLUG: ${{ matrix.batch.old_slug }}
TX_OLD_PROJECT_SLUG: ${{ matrix.batch.old_project_slug }}
TX_LANGUAGES: 'ar,de,fr_CA'
TX_API_TOKEN: ${{ secrets.TRANSIFEX_API_TOKEN }}
run: python scripts/sync_translations.py
70 changes: 70 additions & 0 deletions scripts/sync_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import configparser
from os import getenv, environ
from os.path import expanduser

import requests
from transifex.api import transifex_api


ORGANIZATION_SLUG = 'open-edx'
LANGUAGES = environ['TX_LANGUAGES'].split(',')



def get_transifex_organization_projects():
"""
Get openedx-translations project from Transifex.
"""
transifex_api_token = getenv('TX_API_TOKEN')
if not transifex_api_token:
config = configparser.ConfigParser()
config.read(expanduser('~/.transifexrc'))
transifex_api_token = config['https://www.transifex.com']['password']

if not transifex_api_token:
raise Exception(
'Error: No auth token found. '
'Set transifex API token via TX_API_TOKEN environment variable or via the ~/.transifexrc file.'
)

transifex_api.setup(auth=transifex_api_token)

return transifex_api.Organization.get(slug=ORGANIZATION_SLUG).fetch('projects')


def sync_pair_into_new_resource():
projects = get_transifex_organization_projects()
new_project = projects.get(slug='openedx-translations')

for plan in resources_plan:
resource_name = plan['resource_name']
resource_slug = plan['resource_slug']
resource_id = f'o:{ORGANIZATION_SLUG}:p:{new_project.slug}:r:{resource_slug}'
new_resource = transifex_api.Resource.get(id=resource_id)
assert new_resource.name == resource_name
print(new_resource, new_resource.name, new_resource.id)

pair_id = f'o:{ORGANIZATION_SLUG}:p:{plan["pair_project_slug"]}:r:{plan["pair_slug"]}'
pair_resource = transifex_api.Resource.get(id=pair_id)
print(pair_resource, pair_resource.name)

print(f'Syncing {new_resource.name} from {pair_resource.name}...')

for language in LANGUAGES:
try:
language_obj = transifex_api.Language.get(code=language)
url = transifex_api.ResourceTranslationsAsyncDownload. \
download(resource=pair_resource, language=language_obj)
translated_content = requests.get(url).text
print(translated_content)

transifex_api.ResourceTranslationsAsyncUpload.upload(resource=new_resource, language=f'l:{language}',
content=translated_content)
except Exception as e:
print(e)
import pdb
pdb.set_trace()


if __name__ == '__main__':
sync_pair_into_new_resource()

0 comments on commit 4543d65

Please sign in to comment.