diff --git a/.github/workflows/sync_tx.yml b/.github/workflows/sync_tx.yml index 2fd378e8..5e7d3070 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 b7ebfd97..342b4697 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)))