From 3169d0553870b022c408155588232bff6cbd4f91 Mon Sep 17 00:00:00 2001 From: miro Date: Fri, 17 May 2024 01:10:25 +0100 Subject: [PATCH] update translation automations handle more corner cases, missing subdirectories, deduplicate files --- scripts/sync_translations.py | 40 +++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/scripts/sync_translations.py b/scripts/sync_translations.py index 30c3151..342b469 100644 --- a/scripts/sync_translations.py +++ b/scripts/sync_translations.py @@ -21,9 +21,13 @@ 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,9 +36,13 @@ 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))) @@ -43,9 +51,13 @@ 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))) @@ -54,9 +66,13 @@ 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)))