feat: add scripts/sync_i18n.py — automated i18n sync from a GitHub commit#3198
Merged
feat: add scripts/sync_i18n.py — automated i18n sync from a GitHub commit#3198
Conversation
Co-authored-by: arnaud4d <3355051+arnaud4d@users.noreply.github.com>
Co-authored-by: arnaud4d <3355051+arnaud4d@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Create Python script for syncing i18n documentation
feat: add scripts/sync_i18n.py — automated i18n sync from a GitHub commit
Mar 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Automates propagation of English
.mdchanges to all i18n target languages (fr, es, ja, pt) by analyzing a GitHub commit, translating only modified passages via OpenAI, and pushing a single grouped commit back to the branch.New files
scripts/sync_i18n.py— Main script (~420 lines, Python 3.9+):GitHubClient: fetches commit diffs, file contents, creates Git trees/commits via REST API with retry logicTranslationClient: callsgpt-4o, preserves code blocks, frontmatter, HTML, 4D command names; handlesRetry-Afteras integer or HTTP-dateparse_diff_chunks(): extracts old→new text blocks from unified diff patchesapply_translation(): exact-match replacement, falls back to normalized full-block comparisonmap_source_to_i18n(): maps source paths to i18n targetsdocs/foo/bar.mdi18n/{lang}/docusaurus-plugin-content-docs/current/foo/bar.mdversioned_docs/version-21/foo/bar.mdi18n/{lang}/docusaurus-plugin-content-docs/version-21/foo/bar.mdNew files get full translation; existing files get surgical per-chunk updates
Grouped commit uses branch HEAD as parent (not the source commit SHA)
--dry-run,--langs,--branch,--repoflags; exit codes 0/1/2scripts/requirements_sync_i18n.txt—requests,openai,python-dotenvscripts/.env.example—GITHUB_TOKEN,OPENAI_API_KEY,GITHUB_REPO,GITHUB_BRANCHscripts/README_sync_i18n.md— Usage docs in FrenchUsage
Original prompt
Contexte
Le dépôt
doc4d/docsest une documentation Docusaurus multilingue. Les pages sources sont en anglais dans :docs/(version courante → correspond àcurrentdans i18n)versioned_docs/version-XXX/(versions figées)Les traductions se trouvent dans :
i18n/{fr,es,ja,pt}/docusaurus-plugin-content-docs/{current,version-XXX}/Les langues cibles présentes sont : fr, es, ja, pt (dossier
enignoré car c'est la source).Objectif
Créer un script Python autonome
scripts/sync_i18n.pyqui, à partir d'un SHA de commit GitHub, automatise le workflow complet suivant :.mdmodifiés dans le commit donnéi18n/Règles de mapping des chemins
docs/foo/bar.mdi18n/{lang}/docusaurus-plugin-content-docs/current/foo/bar.mdversioned_docs/version-21/foo/bar.mdi18n/{lang}/docusaurus-plugin-content-docs/version-21/foo/bar.mdversioned_docs/version-21-R2/foo/bar.mdi18n/{lang}/docusaurus-plugin-content-docs/version-21-R2/foo/bar.mdLe préfixe
versioned_docs/version-XXX/est remplacé pari18n/{lang}/docusaurus-plugin-content-docs/version-XXX/.Le préfixe
docs/est remplacé pari18n/{lang}/docusaurus-plugin-content-docs/current/.Stratégie de traduction
---)```)<!-- -->**VERIFY DATA FILE**,`VERIFY DATA FILE`)[texte](url)→ seul letextepeut être traduit, l'URL est conservée##,###) peuvent être traduits si pertinent selon la langueMécanisme de mise à jour des fichiers traduits
Pour chaque fichier traduit à mettre à jour :
-vs ajoutées+dans le diff)Implémentation technique
Fichier principal :
scripts/sync_i18n.pyDépendances (à lister dans
scripts/requirements_sync_i18n.txt) :requests— API GitHubopenai— API OpenAI pour la traductionpython-dotenv— gestion des variables d'environnementtiktoken— comptage de tokens (optionnel)Variables d'environnement requises :
GITHUB_TOKEN— token GitHub avec droits de lecture/écriture sur le repoOPENAI_API_KEY— clé API OpenAIFichier
.env.exampleà créer dansscripts/:Détail du comportement attendu
1. Récupération du commit
Appel à
GET /repos/{owner}/{repo}/commits/{sha}pour obtenir la liste des fichiers et leurs diffs (patch).2. Filtrage
Ne traiter que les fichiers
.mdmodifiés dont le chemin commence pardocs/ouversioned_docs/.3. Pour chaque fichier modifié
Pour chaque fichier EN modifié et chaque langue cible :
a. Calculer le chemin i18n cible
b. Récupérer le contenu actuel du fichier traduit (GET
/repos/{owner}/{repo}/contents/{path})c. Parser le diff pour extraire les blocs
old_text→new_texten anglaisd. Pour chaque bloc de modification :
new_textdans la langue cible, en fournissant le contexte duold_texttraduit existant pour maintenir la cohérenceold_text_traduitparnew_text_traduitdans le fichier ciblee. Si le fichier cible n'existe pas, traduire l'intégralité du fichier sourc...
This pull request was created from Copilot chat.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.