diff --git a/translate/import.py b/translate/import.py index 742c6dc..1fde05a 100644 --- a/translate/import.py +++ b/translate/import.py @@ -6,133 +6,125 @@ import shutil import os from tools import convert_zh_to_tw, import_translation_json + # ========== 配置区域 ========== -IMPORTLANG = "zh_CN" # 語言選項 -VERSION = "V1" # 版本號 -WEBLATE_PATH = Path("F:/Deskto/psoutertale-weblate") # WEBLATE文件夾路徑(須修改!) -SOURCE_DIR = Path("F:/Deskto/Petrichor-PSTVL").resolve() # PETRICHOR專案根目錄(須修改!) +IMPORTLANG = "zh_TW" # 語言選項 +VERSION = "V1.1" # 版本號 +WEBLATE_PATH = "F:/Deskto/psoutertale-weblate" # WEBLATE文件夾路徑(須修改!) +SOURCE_DIR = Path("F:/Deskto/Petrichor-PSTVL").resolve() # Petrichor倉庫的根目錄(須修改!) # ============================== -# Wasneet聲明: -# 這個版本還是「能跑就行」,不准糾我錯! +# WTGIIIR協定 +# Wasneet: That's Great If It Is Run-able協定 -def main(): - PROJECT_ROOT = SOURCE_DIR - LANG_IMAGES = SOURCE_DIR / "images" / IMPORTLANG - BUILD_OUTPUT = SOURCE_DIR / "translate" / "gamebuild" / f"Petrichor-{VERSION}.love" - src_text_path = PROJECT_ROOT / "translate" / "src" - translate_dir = PROJECT_ROOT / "translate" +os.chdir("translate") +print(f"🟡「切換目錄」 進入翻譯專案目錄:{os.getcwd()}") +os.system(f"cd {WEBLATE_PATH} && git pull") +print("\n🔵「文本同步」 從Weblate拉取最新譯文中...") +print("\n🔵「文件準備」 處理LUA文本模板開始:") +for lua_file in src_text_path.glob("*.lua"): + print(f"🟢 處理模板:{lua_file.stem}") + print(" ▸ 克隆簡體中文譯文中...") + for lang in ["zh_CN"]: + shutil.copy( + f"{WEBLATE_PATH}/petrichor/{lua_file.stem}/{lang}.json", + f"strings/{lua_file.stem}/{lang}.json", + ) + print(" ▸ 生成繁體中文譯文中...") + convert_zh_to_tw( + f"strings/{lua_file.stem}/zh_CN.json", f"strings/{lua_file.stem}/zh_TW.json" + ) + print(" ▸ 編譯最後的語言文件...") + import_translation_json( + lua_file, + f"strings/{lua_file.stem}/en_US.json", + f"strings/{lua_file.stem}/{IMPORTLANG}.json", + f"text/{IMPORTLANG}/{lua_file.stem}.lua", + ) +PROJECT_ROOT = SOURCE_DIR +LANG_IMAGES = SOURCE_DIR / "images" / IMPORTLANG +BUILD_OUTPUT = SOURCE_DIR / "translate" / "gamebuild" / f"Petrichor-{VERSION}.love" +src_text_path = PROJECT_ROOT / "translate" / "src" - print(f"🟢「初始化」 Hello!\n 我現在開始裝箱 {IMPORTLANG} 版 Petrichor!") - print(f" ▸ 版本號是:{VERSION}") - print(f" ▸ Weblate路徑是:{WEBLATE_PATH}") - print(f" ▸ 專案根目錄是:{PROJECT_ROOT}\n") +print(f"🟢「初始化」 Hello!\n 我現在開始裝箱 {IMPORTLANG} 版 Petrichor!") +print(f" ▸ 版本號是:{VERSION}") +print(f" ▸ Weblate路徑是:{WEBLATE_PATH}") +print(f" ▸ 專案根目錄是:{PROJECT_ROOT}\n") - try: - os.chdir(translate_dir) - print(f"🟡「切換目錄」 進入翻譯專案目錄:{os.getcwd()}") +# 7z校驗 +print(f"\n🟢「開始裝箱」 準備為 {VERSION} 打包囉!") +print("\n🔵「環境校驗」 來讓我找找你的7-zip...") +if shutil.which("7z") is None: + print("🔴「報錯」 快點去安裝7-zip!沒有這個我怎麼幫你裝箱嘛!") + print("🔴「提示」 官方網址:https://www.7-zip.org/") + exit(1) +else: + print("🟢「好耶」 你電腦裡有7-zip耶,太棒了!") +# 繁瑣的校驗 +if not LANG_IMAGES.exists(): + print(f"🔴「報錯」 找不到這個語言圖片資料夾:{LANG_IMAGES}") + print("🔴「提示」 你看看你路徑有沒有填錯?是不是打錯字了?") + exit(1) +else: + image_count = len(list(LANG_IMAGES.glob("**/*"))) // 2 + print(f"🟢 找到了 {image_count} 張圖片,看起來都在!") - print("\n🔵「文本同步」 從Weblate拉取最新譯文中...") - subprocess.run(f"cd {WEBLATE_PATH} && git pull", shell=True, check=True) +print("🟡「提示」 遊戲文件裝箱中...") +original_cwd = os.getcwd() - print("\n🔵「文件準備」 處理LUA文本模板開始:") - for lua_file in src_text_path.glob("*.lua"): - print(f"🟢 處理模板:{lua_file.stem}") - - target_dir = translate_dir / "strings" / lua_file.stem - target_dir.mkdir(parents=True, exist_ok=True) +with TemporaryDirectory() as tmpdir: + tmp_path = Path(tmpdir) + print(f"\n🔵「準備臨時空間」 建個臨時檔案夾{tmp_path}沒意見吧") - print(" ▸ 克隆簡體中文譯文中...") - shutil.copy( - WEBLATE_PATH / "petrichor" / lua_file.stem / "zh_CN.json", - target_dir / "zh_CN.json" - ) + print("🔵「準備文件」 開始複製專案主體檔案...") + shutil.copytree( + SOURCE_DIR, + tmp_path, + ignore=shutil.ignore_patterns( + 'translate', # 把translate操作間目錄忽略 + 'images', + '*.py', + '*.pyc', + '__pycache__', + '.git' + ), + dirs_exist_ok=True + ) + file_count = len(list(tmp_path.glob("**/*"))) // 2 + print(f"🟢 專案文件複製完成,一共大約 {file_count} 個文件~") - print(" ▸ 生成繁體中文譯文中...") - convert_zh_to_tw( - target_dir / "zh_CN.json", - target_dir / "zh_TW.json" - ) + print("\n🔵「資源替換」 複製語言圖片中...") + # 圖片多言語替換 + shutil.copytree( + LANG_IMAGES, + tmp_path / "images", + dirs_exist_ok=True + ) + print("🟢 圖片替換完畢,準備壓縮!") - print(" ▸ 編譯最後的語言文件...") - import_translation_json( - lua_file, - target_dir / "en_US.json", - target_dir / f"{IMPORTLANG}.json", - translate_dir / "text" / IMPORTLANG / f"{lua_file.stem}.lua" - ) - print(f" ✔️ {lua_file.stem} ビンゴ !\n") + print("\n🔵「壓縮階段」 開始使用7-zip打包成.love檔案...") + os.chdir(tmp_path) + + # 讓7z閉嘴 + command = f'7z a -tzip "{BUILD_OUTPUT}" .' + result = subprocess.run( + command, + shell=True, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL + ) + exit_code = result.returncode + os.chdir(original_cwd) - print("\n🔵「資源校驗」") - if not LANG_IMAGES.exists(): - print(f"🔴「報錯」 我怎麼找不到{LANG_IMAGES}啊,是我的問題嗎(汗") - print("🔴「提示」 你看看你路徑配置的對不對(汗") - return - - image_count = len(list(LANG_IMAGES.glob("**/*"))) // 2 - print(f"🟢 找到了 {image_count} 個圖片!應該全了!") - - print("\n🔵「環境校驗」 來讓我找找你的7-zip...") - if not shutil.which("7z"): - print("🔴「報錯」 快點去安7-zip!") - print("🔴 我給你個網址:https://www.7-zip.org/") - return - print("🟢 好耶,你有7-zip耶!") - - print(f"\n🟢 「開始裝箱」 開始裝箱{VERSION}咯...") - with TemporaryDirectory() as tmpdir: - tmp_path = Path(tmpdir) - print(f"🟢 我在你電腦裡面創建個臨時檔案夾{tmp_path}沒意見吧?") - - print("\n🔵「準備文件」 克隆專案文件中...") - shutil.copytree( - PROJECT_ROOT, - tmp_path, - ignore=shutil.ignore_patterns( - 'translate', 'images', '*.py', '*.pyc', '__pycache__', '.git' - ), - dirs_exist_ok=True - ) - file_count = len(list(tmp_path.glob("**/*"))) // 2 - print(f"✔️ {file_count} 個專案文件已克隆完畢!") - - print("\n🔵「資源替換」 寫入多語言文件中...") - shutil.copytree( - LANG_IMAGES, - tmp_path / "images", - dirs_exist_ok=True - ) - print(f"✔️ {len(list((tmp_path / 'images').glob('**/*')))//2}個圖片資源替換完畢!") - - print("\n🔵「壓縮」 LOVE文件生成中...") - os.chdir(tmp_path) - try: - subprocess.run( - f'7z a -tzip "{BUILD_OUTPUT}" .', - shell=True, - check=True, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL - ) - except subprocess.CalledProcessError as e: - print(f"🔴「壓縮錯誤」 裝箱失敗啦T T(錯誤碼:{e.returncode})") - print("🔴 你看看你關沒關遊戲文件?") - return - finally: - os.chdir(PROJECT_ROOT) - - if BUILD_OUTPUT.exists(): - print(f"\n🎉「裝箱完畢」 完成啦!!") - print(f"📦 給你放在{BUILD_OUTPUT}了,") - print(f"📏 檔案尺寸是{BUILD_OUTPUT.stat().st_size // 1024}KB,應該沒啥問題(可能吧") - else: - print("\n🔴「不明瞭的錯誤」 檔案生成失敗") - - except Exception as e: - print(f"\n🔴「啥情況?!」 {str(e)}") - finally: - print("\n🔵「清理完畢」 行啦我給之前創建那個文件夾刪了嗷") - print("🟢「再見咯」 放心的關閉該窗口吧!") - -if __name__ == "__main__": - main() \ No newline at end of file + if result.returncode == 0: + size_kb = BUILD_OUTPUT.stat().st_size // 1024 + print(f"\n🎉「裝箱完成」 成功打包!我放在{BUILD_OUTPUT}咯") + print(f"📏 檔案大小為:約 {size_kb} KB,應該沒問題!") + else: + if result.returncode == 2: + print(f"🔴「錯誤」 裝箱時檔案被佔用了,遊戲是不是開著呀?") + else: + print(f"🔴「錯誤」 打包失敗!錯誤碼:{result.returncode}") + exit(1) +print("\n🔵「清理臨時資料」 收拾乾淨囉~") +print("🟢「完成」 一切搞定,可以關掉這個視窗囉!辛苦啦~")