diff --git a/src/chatgpt.py b/src/chatgpt.py index 85a4aaf..3ccd0ad 100644 --- a/src/chatgpt.py +++ b/src/chatgpt.py @@ -11,7 +11,7 @@ def translate_with_chatgpt(split_target, timeout): result = [] # 改行を削除(翻訳時扱いがめんどくさいため) - split_target = [line.replace('\\n', '').replace('\n', '') for line in split_target] + split_target = [line.replace('\\n', '').replace('\n', '') for line in split_target] if len(split_target) > 1 else split_target # APIキーとクライアントの初期化 client = OpenAI(api_key=provide_api_key()) diff --git a/src/init.py b/src/init.py index da77cc5..2eaa7d0 100644 --- a/src/init.py +++ b/src/init.py @@ -2,12 +2,13 @@ RESOURCE_DIR = Path('./resourcepacks/japanese') MODS_DIR = Path('./mods') -QUESTS_DIR1 = Path('./kubejs/assets/kubejs/lang') -QUESTS_DIR2 = Path('./kubejs/assets/ftbquests/lang') -QUESTS_DIR3 = Path('./config/ftbquests/quests/chapters') +BETTERQUESTING_DIR = Path('./resources/betterquesting/lang') +FTBQUESTS_DIR1 = Path('./kubejs/assets/kubejs/lang') +FTBQUESTS_DIR2 = Path('./kubejs/assets/ftbquests/lang') +FTBQUESTS_DIR3 = Path('./config/ftbquests/quests/chapters') MAX_ATTEMPTS = 5 USER = 'Y-RyuZU' REPO = 'MinecraftModsLocalizer' -VERSION = 'v2.0.4' +VERSION = 'v2.0.5' diff --git a/src/main.py b/src/main.py index 96fadf3..e88c1f4 100644 --- a/src/main.py +++ b/src/main.py @@ -6,18 +6,15 @@ from provider import set_api_key, set_chunk_size, provide_chunk_size, set_model, provide_model, set_prompt, provide_prompt, set_log_directory from jar import translate_from_jar from log import setup_logging -from quests import translate_quests +from quests import translate_ftbquests, translate_betterquesting from update import check_version if __name__ == '__main__': - # セレクトボックスのオプション - select_options = ['Mod', 'Quests', 'All'] - # レイアウトの定義 layout = [ [sg.Text("Translate Target")], - [sg.Combo(select_options, default_value=select_options[2], key='target', size=(20, 1), expand_x=True)], + [sg.Radio('Mod', key='target1', group_id=1, default=True), sg.Radio('FtbQuests', key='target2', group_id=1), sg.Radio('BetterQuesting', key='target3', group_id=1)], [sg.Text("OpenAI API KEY")], [sg.InputText(key='OPENAI_API_KEY', expand_x=True)], [sg.Text("Chunk Size")], @@ -58,7 +55,6 @@ # 送信ボタンが押された場合 if event == 'translate': # 入力された値を取得 - target = values['target'] set_api_key(values['OPENAI_API_KEY']) set_chunk_size(int(values['CHUNK_SIZE'])) set_model(values['MODEL']) @@ -70,13 +66,12 @@ break try: - if target == select_options[0]: - translate_from_jar() - elif target == select_options[1]: - translate_quests() - elif target == select_options[2]: + if values['target1']: translate_from_jar() - translate_quests() + elif values['target2']: + translate_ftbquests() + elif values['target3']: + translate_betterquesting() except Exception as e: logging.error(e) sg.popup('翻訳失敗') diff --git a/src/prepare.py b/src/prepare.py index 367986b..a397023 100644 --- a/src/prepare.py +++ b/src/prepare.py @@ -9,6 +9,17 @@ from provider import provide_chunk_size +def extract_map_from_lang(filepath): + collected_map = {} + with open(filepath, 'r', encoding='utf-8') as file: + for line in file: + line = line.strip() # 余分な空白や改行を削除 + if line and not line.startswith('#'): + key, value = line.split('=', 1) + collected_map[key.strip()] = value.strip() + return collected_map + + def extract_map_from_json(file_path): collected_map = {} diff --git a/src/quests.py b/src/quests.py index c3e0890..74d2567 100644 --- a/src/quests.py +++ b/src/quests.py @@ -4,12 +4,31 @@ import re import shutil -from init import QUESTS_DIR1, QUESTS_DIR2, QUESTS_DIR3 +from init import FTBQUESTS_DIR1, FTBQUESTS_DIR2, FTBQUESTS_DIR3, BETTERQUESTING_DIR from provider import provide_log_directory -from prepare import extract_map_from_json, prepare_translation +from prepare import extract_map_from_lang, extract_map_from_json, prepare_translation -def translate_quests_from_json(file_path): +def translate_betterquesting_from_json(file_path): + clean_json_file(file_path) + targets = extract_map_from_lang(file_path) + + translated_map = prepare_translation(list(targets.values())) + + translated_targets = {lang_key: translated_map[original] for lang_key, original in targets.items() if original in translated_map} + + untranslated_items = {lang_key: original for lang_key, original in targets.items() if original not in translated_map} + + with open(os.path.join(BETTERQUESTING_DIR / 'ja_jp.lang'), 'w', encoding="utf-8") as f: + # 翻訳された項目の書き込み + for lang_key, translated in translated_targets.items(): + f.write(f'{lang_key}={translated}\n') + # 翻訳されなかった項目の書き込み(原文のまま) + for lang_key, original in untranslated_items.items(): + f.write(f'{lang_key}={original}\n') + + +def translate_ftbquests_from_json(file_path): clean_json_file(file_path) targets = extract_map_from_json(file_path) @@ -19,9 +38,9 @@ def translate_quests_from_json(file_path): untranslated_items = {json_key: original for json_key, original in targets.items() if original not in translated_map} - with open(os.path.join(QUESTS_DIR1 / 'ja_jp.json'), 'w', encoding="utf-8") as f: + with open(os.path.join(FTBQUESTS_DIR1 / 'ja_jp.json'), 'w', encoding="utf-8") as f: json.dump(dict(sorted(translated_targets.items())), f, ensure_ascii=False, indent=4) - with open(os.path.join(QUESTS_DIR2 / 'ja_jp.json'), 'w', encoding="utf-8") as f: + with open(os.path.join(FTBQUESTS_DIR2 / 'ja_jp.json'), 'w', encoding="utf-8") as f: json.dump(dict(sorted(translated_targets.items())), f, ensure_ascii=False, indent=4) error_directory = os.path.join(provide_log_directory(), 'error') @@ -33,7 +52,7 @@ def translate_quests_from_json(file_path): json.dump(dict(sorted(untranslated_items.items())), f, ensure_ascii=False, indent=4) -def translate_quests_from_snbt(file_path): +def translate_ftbquests_from_snbt(file_path): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() logging.info(f"Translating {file_path}...") @@ -71,28 +90,46 @@ def translate_quests_from_snbt(file_path): f.write(content) -def translate_quests(): +def translate_ftbquests(): # バックアップ用のディレクトリを作成 backup_directory = provide_log_directory() / 'quests' backup_directory.mkdir(parents=True, exist_ok=True) logging.info("translating snbt files...") - json_path = os.path.join(QUESTS_DIR1, 'en_us.json') + json_path = os.path.join(FTBQUESTS_DIR1, 'en_us.json') if os.path.exists(json_path): - logging.info(f"en_us.json found in {QUESTS_DIR1}, translating from json...") + logging.info(f"en_us.json found in {FTBQUESTS_DIR1}, translating from json...") shutil.copy(json_path, backup_directory) - translate_quests_from_json(json_path) + translate_ftbquests_from_json(json_path) else: - logging.info(f"en_us.json not found in {QUESTS_DIR1}, translating snbt files in directory...") - nbt_files = list(QUESTS_DIR3.glob('*.snbt')) + logging.info(f"en_us.json not found in {FTBQUESTS_DIR1}, translating snbt files in directory...") + nbt_files = list(FTBQUESTS_DIR3.glob('*.snbt')) for file in nbt_files: backup_file = backup_directory / file.name shutil.copy(file, backup_file) - translate_quests_from_snbt(file) + translate_ftbquests_from_snbt(file) + + logging.info("Translate snbt files Done!") + + +def translate_betterquesting(): + # バックアップ用のディレクトリを作成 + backup_directory = provide_log_directory() / 'quests' + backup_directory.mkdir(parents=True, exist_ok=True) + + logging.info("translating snbt files...") + json_path = os.path.join(BETTERQUESTING_DIR, 'en_us.lang') + + if os.path.exists(json_path): + logging.info(f"en_us.json found in {BETTERQUESTING_DIR}, translating from json...") + shutil.copy(json_path, backup_directory) + translate_betterquesting_from_json(json_path) + else: + logging.error(f"en_us.json not found in {BETTERQUESTING_DIR}.") - logging.info("Traslate snbt files Done!") + logging.info("Translate snbt files Done!") def clean_json_file(json_path):