@@ -15,51 +15,65 @@ def translate_patchouli():
1515 if filename .endswith ('.jar' ):
1616 process_jar_file (os .path .join (MODS_DIR , filename ))
1717
18+ import zipfile
19+ import os
20+ import re
21+ import logging
22+
23+
1824def process_jar_file (jar_path ):
1925 mod_name = get_mod_name_from_jar (jar_path )
2026 if mod_name is None :
2127 return
2228
23- # パスの定義
24- lang_path_in_jar = f'assets/{ mod_name } /patchouli_books/guidebook'
25- en_us_path = f'{ lang_path_in_jar } /en_us/'
26- ja_jp_path = f'{ lang_path_in_jar } /ja_jp/'
27-
28- print (en_us_path )
29- print (ja_jp_path )
29+ # Base path in the jar
30+ base_path_in_jar = f'assets/{ mod_name } /patchouli_books/'
3031
3132 try :
3233 with zipfile .ZipFile (jar_path , 'r' ) as jar :
34+ # Get list of directories under patchouli_books
35+ dirs_in_patchouli = {item .filename .split ('/' )[3 ] for item in jar .infolist ()
36+ if item .filename .startswith (base_path_in_jar ) and item .is_dir ()}
3337
34- en_us_exists = any (en_us_path in item .filename for item in jar .infolist ())
35- ja_jp_exists = any (ja_jp_path in item .filename for item in jar .infolist ())
36-
37- if not en_us_exists :
38- return {}
39- if ja_jp_exists :
40- return {}
41-
42- logging .info (f"Translate Patchouli in { jar_path } " )
38+ modification_needed = False
4339
4440 with zipfile .ZipFile (jar_path + '.new' , 'w' ) as new_jar :
4541 for item in jar .infolist ():
4642 data = jar .read (item .filename )
47- new_jar .writestr (item , data ) # すべての元のファイルをコピー
48- if item .filename .startswith (en_us_path ) and not item .is_dir ():
49- new_filename = item .filename .replace (en_us_path , ja_jp_path )
50-
51- # 翻訳処理
52- if item .filename .endswith ('.json' ):
53- content = data .decode ('utf-8' )
54- matches = re .findall (r'"(name|description|title)":\s*"(.*?)(?<!\\)"' , content )
55- extracted_strings = [match [1 ] for match in matches ]
56- translated_map = prepare_translation (extracted_strings )
57- for original , translated in translated_map .items ():
58- content = content .replace (f'"{ original } "' , f'"{ translated } "' )
59- new_jar .writestr (new_filename , content .encode ('utf-8' ))
60-
61- # 新しい.jarファイルで古いファイルを置き換え
62- os .replace (jar_path + '.new' , jar_path )
43+ new_jar .writestr (item , data ) # Copy all original files
44+
45+ if item .filename .startswith (base_path_in_jar ) and not item .is_dir ():
46+ # Check for en_us and translate to ja_jp for each directory under patchouli_books
47+ for subdir in dirs_in_patchouli :
48+ en_us_path = f'{ base_path_in_jar } { subdir } /en_us/'
49+ ja_jp_path = f'{ base_path_in_jar } { subdir } /ja_jp/'
50+
51+ en_us_exists = any (en_us_path in item .filename for item in jar .infolist ())
52+ ja_jp_exists = any (ja_jp_path in item .filename for item in jar .infolist ())
53+
54+ if en_us_exists and not ja_jp_exists :
55+ modification_needed = True
56+
57+ if item .filename .startswith (en_us_path ):
58+ new_filename = item .filename .replace (en_us_path , ja_jp_path )
59+
60+ # Translation processing
61+ if item .filename .endswith ('.json' ):
62+ logging .info (f"Translating Patchouli for { item .filename } in { jar_path } " )
63+
64+ content = data .decode ('utf-8' )
65+ matches = re .findall (r'"(name|description|title|text)":\s*"(.*?)(?<!\\)"' , content )
66+ extracted_strings = [match [1 ] for match in matches ]
67+ translated_map = prepare_translation (extracted_strings )
68+ for original , translated in translated_map .items ():
69+ content = content .replace (f'"{ original } "' , f'"{ translated } "' )
70+ new_jar .writestr (new_filename , content .encode ('utf-8' ))
6371
6472 except zipfile .BadZipFile :
65- logging .error ("Failed to read or write to the jar file." )
73+ logging .error ("Failed to read or write to the jar file." )
74+
75+ finally :
76+ if modification_needed :
77+ os .replace (jar_path + '.new' , jar_path )
78+ else :
79+ os .remove (jar_path + '.new' )
0 commit comments