1717import json
1818import re
1919
20- def convert_po_to_json (po_file , output_dir ):
21- """Convert a .po file to .json reading only msgid and msgstr lines."""
22-
23- json_data = {}
20+ def parse_po_file (po_file ):
21+ """Parse a .po file and return a dict of {msgid: msgstr}."""
22+ data = {}
2423 current_msgid = None
2524 current_msgstr = None
2625
2726 with open (po_file , "r" , encoding = "utf-8" ) as f :
2827 for line in f :
29- # Ignore all lines except those with msgid or msgstr
3028 line = line .strip ()
3129 if line .startswith ("msgid" ):
3230 current_msgid = re .findall (r'"(.*)"' , line )[0 ]
3331 elif line .startswith ("msgstr" ):
3432 current_msgstr = re .findall (r'"(.*)"' , line )[0 ]
35- # Save the pair if msgid is present
3633 if current_msgid is not None :
37- json_data [current_msgid ] = current_msgstr or current_msgid
38- current_msgid = None # Reset for the next pair
34+ data [current_msgid ] = current_msgstr or current_msgid
35+ current_msgid = None
36+ return data
37+
38+ def convert_po_to_json (po_file , output_dir ):
39+ """Convert a .po file to .json. Special case: ja.po + ja-kana.po -> merged ja.json."""
3940
40- # Extract language code (e.g., 'fr' from 'fr.po')
4141 lang_code = os .path .splitext (os .path .basename (po_file ))[0 ]
42- output_path = os .path .join (output_dir , f"{ lang_code } .json" )
4342
43+ # Special handling for Japanese
44+ if lang_code in ["ja" , "ja-kana" ]:
45+ ja_file = os .path .join (os .path .dirname (po_file ), "ja.po" )
46+ kana_file = os .path .join (os .path .dirname (po_file ), "ja-kana.po" )
47+
48+ if os .path .exists (ja_file ) and os .path .exists (kana_file ):
49+ ja_dict = parse_po_file (ja_file )
50+ kana_dict = parse_po_file (kana_file )
51+
52+ combined = {}
53+ all_keys = set (ja_dict .keys ()) | set (kana_dict .keys ())
54+ for key in all_keys :
55+ combined [key ] = {
56+ "kanji" : ja_dict .get (key , key ),
57+ "kana" : kana_dict .get (key , key ),
58+ }
59+
60+ output_path = os .path .join (output_dir , "ja.json" )
61+ os .makedirs (output_dir , exist_ok = True )
62+ with open (output_path , "w" , encoding = "utf-8" ) as f :
63+ json .dump (combined , f , indent = 2 , ensure_ascii = False )
64+ print (f"✅ Combined ja.po + ja-kana.po → { output_path } " )
65+ return # Don’t fall through to default case
66+
67+ # Default for all other langs
68+ json_data = parse_po_file (po_file )
69+ output_path = os .path .join (output_dir , f"{ lang_code } .json" )
4470 os .makedirs (output_dir , exist_ok = True )
4571 with open (output_path , "w" , encoding = "utf-8" ) as f :
4672 json .dump (json_data , f , indent = 2 , ensure_ascii = False )
47-
4873 print (f"✅ Converted { po_file } → { output_path } " )
4974
5075def convert_all_po_files (po_dir , output_dir ):
@@ -54,4 +79,4 @@ def convert_all_po_files(po_dir, output_dir):
5479 if file .endswith (".po" ):
5580 convert_po_to_json (os .path .join (root , file ), output_dir )
5681
57- convert_all_po_files ("./po" , "./locales" )
82+ convert_all_po_files ("./po" , "./locales" )
0 commit comments