Skip to content

Commit c0c2c5b

Browse files
committed
feature: Patchouliへの対応
1 parent 959f125 commit c0c2c5b

File tree

4 files changed

+59
-34
lines changed

4 files changed

+59
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
![image2.png](/docs/assets/image2.png)
44
![image3.png](/docs/assets/image3.png)
55
![image4.png](/docs/assets/image4.png)
6+
![image4.png](/docs/assets/image5.png)
67

78
# **New!! 翻訳に使用するAIをChatGPTに変更しました!API料金が大幅に安くなり(ATM9で0.5ドル以下)、精度も格段に良くなりました!ファイル構造の崩壊、特殊文字無視とはバイバイ!**
89
あとBetterQuestingに対応しました。RLCraftとかする人はどうぞ

src/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,15 @@
8080
sg.popup('翻訳失敗')
8181
break
8282

83+
84+
# if values['target1']:
85+
# translate_from_jar()
86+
# elif values['target2']:
87+
# translate_ftbquests()
88+
# elif values['target3']:
89+
# translate_betterquesting()
90+
# elif values['target4']:
91+
# translate_patchouli()
92+
8393
sg.popup('翻訳成功!')
8494
break

src/patchouli.py

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1824
def 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')

src/prepare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def extract_map_from_json(file_path):
3131

3232
# 値が英語でコメント以外のキーのみを保存します。
3333
for key, value in content.items():
34-
if not key.startswith("_comment") and not re.search('[\u3040-\u30FF\u3400-\u4DBF\u4E00-\u9FFF]', value):
34+
if not key.startswith("_comment") and isinstance(value, str) and not re.search('[\u3040-\u30FF\u3400-\u4DBF\u4E00-\u9FFF]', value):
3535
collected_map[key] = value
3636

3737
except json.JSONDecodeError:

0 commit comments

Comments
 (0)