Skip to content

Commit

Permalink
update translation automations
Browse files Browse the repository at this point in the history
handle more corner cases, missing subdirectories, deduplicate files
  • Loading branch information
JarbasAl committed May 17, 2024
1 parent a86fe4b commit f48e785
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/sync_tx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
44 changes: 28 additions & 16 deletions scripts/sync_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,59 @@
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)))

if os.path.isfile(dialogs):
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)))

if os.path.isfile(vocs):
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)))

if os.path.isfile(regexes):
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)))

0 comments on commit f48e785

Please sign in to comment.