2 # -*- coding: utf-8 -*-
4 # split_merge_json.py - script for splitting the JSON files from
5 # Transifex into their public/private parts and to merging the
6 # private/public en.json files to form the source file for Transifex
8 # Copyright (C) 2024 sledgehammer999 <hammered999@gmail.com>
10 # License: Public domain.
16 from pathlib
import PurePath
20 def updateJson(json_file
: str, source
: dict[str, str]) -> None:
21 trimmed_path
: str = json_file
22 path_parts
= PurePath(json_file
).parts
23 if len(path_parts
) >= 3:
24 trimmed_path
= f
"{path_parts[-3]}/{path_parts[-2]}/{path_parts[-1]}"
26 if not os
.path
.exists(json_file
):
27 print(f
"\tWARNIG: {trimmed_path} doesn't exist")
30 print(f
"\tUpdating {trimmed_path}")
31 with
open(json_file
, mode
="r+", encoding
='utf-8') as file:
32 target
: dict[str, str] = json
.load(file)
33 if not isinstance(target
, dict):
34 print(f
"\tWARNIG: {trimmed_path} is malormed")
37 for key
in target
.keys():
38 value
= source
.get(key
)
42 json
.dump(target
, file, ensure_ascii
=False, indent
=2, sort_keys
=True)
47 def splitJson(transifex_dir
: str, json_public_dir
: str, json_private_dir
: str) -> None:
48 locales
: list[str] = glob
.glob("*.json", root_dir
=transifex_dir
)
49 locales
= [x
for x
in locales
if x
!= "en.json"]
50 for locale
in locales
:
51 print(f
"Processing lang {locale}")
52 transifex_file
: str = f
"{transifex_dir}/{locale}"
53 public_file
: str = f
"{json_public_dir}/{locale}"
54 private_file
: str = f
"{json_private_dir}/{locale}"
56 transifex_json
: dict[str, str] = {}
57 with
open(transifex_file
, mode
="r", encoding
='utf-8') as file:
58 transifex_json
= json
.load(file)
59 if not isinstance(transifex_json
, dict):
60 trimmed_path
: str = transifex_file
61 path_parts
= PurePath(transifex_file
).parts
62 if len(path_parts
) >= 2:
63 trimmed_path
= f
"{path_parts[-2]}/{path_parts[-1]}"
64 print(f
"\tERROR: {trimmed_path} is malformed.")
67 updateJson(public_file
, transifex_json
)
68 updateJson(private_file
, transifex_json
)
71 def mergeJson(transifex_dir
: str, json_public_dir
: str, json_private_dir
: str) -> None:
72 transifex_en_file
: str = f
"{transifex_dir}/en.json"
73 public_en_file
: str = f
"{json_public_dir}/en.json"
74 private_en_file
: str = f
"{json_private_dir}/en.json"
76 transifex_en_json
: dict[str, str] = {}
77 public_en_json
: dict[str, str] = {}
78 private_en_json
: dict[str, str] = {}
80 print("Merging source en.json file")
81 if os
.path
.exists(public_en_file
):
82 with
open(public_en_file
, mode
="r", encoding
='utf-8') as file:
83 public_en_json
= json
.load(file)
84 if not isinstance(public_en_json
, dict):
87 if os
.path
.exists(private_en_file
):
88 with
open(private_en_file
, mode
="r", encoding
='utf-8') as file:
89 private_en_json
= json
.load(file)
90 if not isinstance(private_en_json
, dict):
93 transifex_en_json
= public_en_json | private_en_json
94 with
open(transifex_en_file
, mode
="w", encoding
='utf-8') as file:
95 json
.dump(transifex_en_json
, file, ensure_ascii
=False, indent
=2, sort_keys
=True)
100 script_path
: str = os
.path
.dirname(os
.path
.realpath(__file__
))
101 transifex_dir
: str = f
"{script_path}/transifex"
102 json_public_dir
: str = f
"{script_path}/public/lang"
103 json_private_dir
: str = f
"{script_path}/private/lang"
105 parser
= argparse
.ArgumentParser()
106 parser
.add_argument("--mode", required
=True, choices
=["split", "merge"], help="SPLIT the translations files from Transifex into their public/private JSON counterpart. MERGE to merge the public and private en.json files into the 'transifex/en.json' file")
107 parser
.add_argument("--transifex-dir", default
=transifex_dir
, nargs
=1, help=f
"directory of WebUI transifex file (default: '{transifex_dir}')")
108 parser
.add_argument("--json-public-dir", default
=json_public_dir
, nargs
=1, help=f
"directory of WebUI public JSON translation files(default: '{json_public_dir}')")
109 parser
.add_argument("--json-private-dir", default
=json_private_dir
, nargs
=1, help=f
"directory of WebUI private JSON translation files(default: '{json_private_dir}')")
111 args
= parser
.parse_args()
113 if not os
.path
.isdir(args
.transifex_dir
):
114 raise RuntimeError(f
"'{args.transifex_dir}' isn't a directory")
116 if not os
.path
.isdir(args
.json_public_dir
):
117 raise RuntimeError(f
"'{args.json_public_dir}' isn't a directory")
119 if not os
.path
.isdir(args
.json_private_dir
):
120 raise RuntimeError(f
"'{args.json_private_dir}' isn't a directory")
122 if args
.mode
== "merge":
123 mergeJson(transifex_dir
, json_public_dir
, json_private_dir
)
125 splitJson(transifex_dir
, json_public_dir
, json_private_dir
)
132 if __name__
== '__main__':