-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathconvert_po_to_json.py
More file actions
108 lines (94 loc) · 4.29 KB
/
Copy pathconvert_po_to_json.py
File metadata and controls
108 lines (94 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Copyright (c) 2025 Walter Bender
# Copyright (c) 2025 Aman Chadha, DMP'25
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the The GNU Affero General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# You should have received a copy of the GNU Affero General Public
# License along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
#
# Note: This script converts .po translation files into JSON format,
# following the i18n infrastructure used in Music Blocks. -- Aman Chadha, July 2025
import os
import json
import re
import sys
SOLFEGE_KEY = "ti la sol fa mi re do"
SOLFEGE_TOKEN_COUNT = 7
def is_valid_solfege_translation(value):
"""Return True when a solfege translation has seven note labels."""
return len(value.split()) == SOLFEGE_TOKEN_COUNT
def parse_po_file(po_file):
"""Parse a .po file and return a dict of {msgid: msgstr}."""
data = {}
current_msgid = None
current_msgstr = None
with open(po_file, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line.startswith("msgid"):
current_msgid = re.findall(r'"(.*)"', line)[0]
elif line.startswith("msgstr"):
current_msgstr = re.findall(r'"(.*)"', line)[0]
if current_msgid is not None:
if (
current_msgid == SOLFEGE_KEY
and not is_valid_solfege_translation(current_msgstr)
):
print(
f"Warning: invalid solfege translation in "
f"{po_file}: expected {SOLFEGE_TOKEN_COUNT} "
f"tokens, got {len(current_msgstr.split())}. "
f"Falling back to English.",
file=sys.stderr,
)
data[current_msgid] = current_msgid
else:
data[current_msgid] = current_msgstr or current_msgid
current_msgid = None
return data
def convert_po_to_json(po_file, output_dir):
"""Convert a .po file to .json. Special case: ja.po + ja-kana.po -> merged ja.json."""
lang_code = os.path.splitext(os.path.basename(po_file))[0]
# Special handling for Japanese
if lang_code in ["ja", "ja-kana"]:
ja_file = os.path.join(os.path.dirname(po_file), "ja.po")
kana_file = os.path.join(os.path.dirname(po_file), "ja-kana.po")
if os.path.exists(ja_file) and os.path.exists(kana_file):
ja_dict = parse_po_file(ja_file)
kana_dict = parse_po_file(kana_file)
combined = {}
all_keys = list(ja_dict.keys())
all_keys.extend(key for key in kana_dict.keys() if key not in ja_dict)
for key in all_keys:
combined[key] = {
"kanji": ja_dict.get(key, key),
"kana": kana_dict.get(key, key),
}
output_path = os.path.join(output_dir, "ja.json")
os.makedirs(output_dir, exist_ok=True)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(combined, f, indent=2, ensure_ascii=False)
print(f"Combined ja.po + ja-kana.po -> {output_path}")
return # Don’t fall through to default case
# Default for all other langs
json_data = parse_po_file(po_file)
output_path = os.path.join(output_dir, f"{lang_code}.json")
os.makedirs(output_dir, exist_ok=True)
with open(output_path, "w", encoding="utf-8") as f:
json.dump(json_data, f, indent=2, ensure_ascii=False)
print(f"Converted {po_file} -> {output_path}")
def convert_all_po_files(po_dir, output_dir):
"""Convert all .po files in the given directory to .json format."""
for root, _, files in os.walk(po_dir):
for file in files:
if file.endswith(".po"):
convert_po_to_json(os.path.join(root, file), output_dir)
if __name__ == "__main__":
if len(sys.argv) == 3:
convert_po_to_json(sys.argv[1], sys.argv[2])
else:
convert_all_po_files("./po", "./locales")