|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import re |
| 5 | +import zipfile |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from src.init import RESOURCE_DIR, MODS_DIR |
| 9 | +from src.provider import provide_log_directory |
| 10 | +from src.prepare import extract_map_from_json, prepare_translation |
| 11 | + |
| 12 | + |
| 13 | +def process_jar_file(jar_path): |
| 14 | + mod_name = get_mod_name_from_jar(jar_path) |
| 15 | + if mod_name is None: |
| 16 | + logging.info(f"Could not determine mod name for {jar_path}") |
| 17 | + return {} |
| 18 | + |
| 19 | + lang_path_in_jar = Path(f'assets/{mod_name}/lang/') |
| 20 | + ja_jp_path_in_jar = os.path.join(lang_path_in_jar, 'ja_jp.json') |
| 21 | + en_us_path_in_jar = os.path.join(lang_path_in_jar, 'en_us.json') |
| 22 | + ja_jp_path_in_jar_str = str(ja_jp_path_in_jar).replace('\\', '/') |
| 23 | + en_us_path_in_jar_str = str(en_us_path_in_jar).replace('\\', '/') |
| 24 | + |
| 25 | + logging.info(f"Extract en_us.json or ja_jp.json in {jar_path / lang_path_in_jar}") |
| 26 | + with zipfile.ZipFile(jar_path, 'r') as zip_ref: |
| 27 | + if en_us_path_in_jar_str in zip_ref.namelist(): |
| 28 | + extract_specific_file(jar_path, en_us_path_in_jar_str, provide_log_directory()) |
| 29 | + if ja_jp_path_in_jar_str in zip_ref.namelist(): |
| 30 | + extract_specific_file(jar_path, ja_jp_path_in_jar_str, provide_log_directory()) |
| 31 | + |
| 32 | + en_us_path = os.path.join(provide_log_directory(), en_us_path_in_jar) |
| 33 | + ja_jp_path = os.path.join(provide_log_directory(), ja_jp_path_in_jar) |
| 34 | + |
| 35 | + return extract_map_from_json(ja_jp_path) if os.path.exists(ja_jp_path) else extract_map_from_json(en_us_path) |
| 36 | + |
| 37 | + |
| 38 | +def translate_from_jar(): |
| 39 | + if not os.path.exists(RESOURCE_DIR): |
| 40 | + os.makedirs(os.path.join(RESOURCE_DIR, 'assets', 'japanese', 'lang')) |
| 41 | + |
| 42 | + targets = {} |
| 43 | + |
| 44 | + extracted_pack_mcmeta = False |
| 45 | + for filename in os.listdir(MODS_DIR): |
| 46 | + if filename.endswith('.jar'): |
| 47 | + # Extract pack.mcmeta if it exists in the jar |
| 48 | + if not extracted_pack_mcmeta: |
| 49 | + extracted_pack_mcmeta = extract_specific_file(os.path.join(MODS_DIR, filename), 'pack.mcmeta', |
| 50 | + RESOURCE_DIR) |
| 51 | + update_resourcepack_description(os.path.join(RESOURCE_DIR, 'pack.mcmeta'), '日本語化パック') |
| 52 | + |
| 53 | + targets.update(process_jar_file(os.path.join(MODS_DIR, filename))) |
| 54 | + |
| 55 | + translated_map = prepare_translation(list(targets.values())) |
| 56 | + |
| 57 | + translated_targets = {json_key: translated_map[original] for json_key, original in targets.items() if |
| 58 | + original in translated_map} |
| 59 | + |
| 60 | + untranslated_items = {json_key: original for json_key, original in targets.items() if |
| 61 | + original not in translated_map} |
| 62 | + |
| 63 | + with open(os.path.join(RESOURCE_DIR, 'assets', 'japanese', 'lang', 'ja_jp.json'), 'w', encoding="utf-8") as f: |
| 64 | + json.dump(dict(sorted(translated_targets.items())), f, ensure_ascii=False, indent=4) |
| 65 | + |
| 66 | + error_directory = os.path.join(provide_log_directory(), 'error') |
| 67 | + |
| 68 | + if not os.path.exists(error_directory): |
| 69 | + os.makedirs(error_directory) |
| 70 | + |
| 71 | + with open(os.path.join(error_directory, 'mod_ja_jp.json'), 'w', encoding="utf-8") as f: |
| 72 | + json.dump(dict(sorted(untranslated_items.items())), f, ensure_ascii=False, indent=4) |
| 73 | + |
| 74 | + |
| 75 | +def update_resourcepack_description(file_path, new_description): |
| 76 | + # ファイルが存在するか確認 |
| 77 | + if not os.path.exists(file_path): |
| 78 | + return |
| 79 | + |
| 80 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 81 | + try: |
| 82 | + data = json.load(file) |
| 83 | + except json.JSONDecodeError as e: |
| 84 | + return |
| 85 | + |
| 86 | + # 'description'の'text'を新しい値に更新 |
| 87 | + try: |
| 88 | + if 'pack' in data and 'description' in data['pack'] and 'text' in data['pack']['description']: |
| 89 | + data['pack']['description']['text'] = new_description |
| 90 | + else: |
| 91 | + return |
| 92 | + except Exception as e: |
| 93 | + return |
| 94 | + |
| 95 | + # 変更を加えたデータを同じファイルに書き戻す |
| 96 | + with open(file_path, 'w', encoding='utf-8') as file: |
| 97 | + try: |
| 98 | + json.dump(data, file, ensure_ascii=False, indent=2) # JSONを整形して書き込み |
| 99 | + except Exception as e: |
| 100 | + return |
| 101 | + |
| 102 | + |
| 103 | +def get_mod_name_from_jar(jar_path): |
| 104 | + with zipfile.ZipFile(jar_path, 'r') as zip_ref: |
| 105 | + asset_dirs_with_lang = set() |
| 106 | + for name in zip_ref.namelist(): |
| 107 | + parts = name.split('/') |
| 108 | + if len(parts) > 3 and parts[0] == 'assets' and parts[2] == 'lang' and parts[1] != 'minecraft': |
| 109 | + asset_dirs_with_lang.add(parts[1]) |
| 110 | + if asset_dirs_with_lang: |
| 111 | + return list(asset_dirs_with_lang)[0] |
| 112 | + return None |
| 113 | + |
| 114 | + |
| 115 | +def extract_specific_file(zip_filepath, file_name, dest_dir): |
| 116 | + with zipfile.ZipFile(zip_filepath, 'r') as zip_ref: |
| 117 | + if file_name in zip_ref.namelist(): |
| 118 | + zip_ref.extract(file_name, dest_dir) |
| 119 | + return True |
| 120 | + else: |
| 121 | + logging.info(f"The file {file_name} in {zip_filepath} was not found in the ZIP archive.") |
| 122 | + return False |
0 commit comments