From 88c7c7d16733abb8283e526ec48679d67730c79c Mon Sep 17 00:00:00 2001 From: miro Date: Fri, 17 May 2024 01:05:10 +0100 Subject: [PATCH] update translation automations handle more corner cases, missing subdirectories, deduplicate files --- .github/workflows/sync_tx.yml | 5 ++++ scripts/sync_translations.py | 44 ++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/.github/workflows/sync_tx.yml b/.github/workflows/sync_tx.yml index 2fd378e..5e7d307 100644 --- a/.github/workflows/sync_tx.yml +++ b/.github/workflows/sync_tx.yml @@ -20,6 +20,11 @@ jobs: with: python-version: 3.9 + - name: Run script if manual dispatch + if: github.event_name == 'workflow_dispatch' + run: | + python scripts/sync_translations.py + - name: Run script if merged by gitlocalize-app[bot] if: github.event_name == 'push' && github.event.head_commit.author.username == 'gitlocalize-app[bot]' run: | diff --git a/scripts/sync_translations.py b/scripts/sync_translations.py index b7ebfd9..342b469 100644 --- a/scripts/sync_translations.py +++ b/scripts/sync_translations.py @@ -20,11 +20,14 @@ with open(intents) as f: data = json.load(f) for fid, samples in data.items(): - if samples: - os.makedirs(f"{locale}/{lang.lower()}", exist_ok=True) - samples = [s.strip() for s in samples - if s and s.strip() != "[UNUSED]"] # s may be None + samples = list(set([s.strip() for s in samples + if s and s.strip() != "[UNUSED]"])) # s may be None + if fid.startswith("/"): + p = f"{locale}/{lang.lower()}{fid}" + else: + p = f"{locale}/{lang.lower()}/{fid}" + os.makedirs(os.path.dirname(p), exist_ok=True) with open(f"{locale}/{lang.lower()}/{fid}", "w") as f: f.write("\n".join(sorted(samples))) @@ -32,11 +35,14 @@ with open(dialogs) as f: data = json.load(f) for fid, samples in data.items(): - if samples: - os.makedirs(f"{locale}/{lang.lower()}", exist_ok=True) - samples = [s.strip() for s in samples - if s and s.strip() != "[UNUSED]"] # s may be None + samples = list(set([s.strip() for s in samples + if s and s.strip() != "[UNUSED]"])) # s may be None + if fid.startswith("/"): + p = f"{locale}/{lang.lower()}{fid}" + else: + p = f"{locale}/{lang.lower()}/{fid}" + os.makedirs(os.path.dirname(p), exist_ok=True) with open(f"{locale}/{lang.lower()}/{fid}", "w") as f: f.write("\n".join(sorted(samples))) @@ -44,11 +50,14 @@ with open(vocs) as f: data = json.load(f) for fid, samples in data.items(): - if samples: - os.makedirs(f"{locale}/{lang.lower()}", exist_ok=True) - samples = [s.strip() for s in samples - if s and s.strip() != "[UNUSED]"] # s may be None + samples = list(set([s.strip() for s in samples + if s and s.strip() != "[UNUSED]"])) # s may be None + if fid.startswith("/"): + p = f"{locale}/{lang.lower()}{fid}" + else: + p = f"{locale}/{lang.lower()}/{fid}" + os.makedirs(os.path.dirname(p), exist_ok=True) with open(f"{locale}/{lang.lower()}/{fid}", "w") as f: f.write("\n".join(sorted(samples))) @@ -56,11 +65,14 @@ with open(regexes) as f: data = json.load(f) for fid, samples in data.items(): - if samples: - os.makedirs(f"{locale}/{lang.lower()}", exist_ok=True) - samples = [s.strip() for s in samples - if s and s.strip() != "[UNUSED]"] # s may be None + samples = list(set([s.strip() for s in samples + if s and s.strip() != "[UNUSED]"])) # s may be None + if fid.startswith("/"): + p = f"{locale}/{lang.lower()}{fid}" + else: + p = f"{locale}/{lang.lower()}/{fid}" + os.makedirs(os.path.dirname(p), exist_ok=True) with open(f"{locale}/{lang.lower()}/{fid}", "w") as f: f.write("\n".join(sorted(samples)))