Skip to content

Instantly share code, notes, and snippets.

@Sayamame-beans
Created September 10, 2024 12:17
Show Gist options
  • Save Sayamame-beans/849319b41497fa87a09e14661331119f to your computer and use it in GitHub Desktop.
Save Sayamame-beans/849319b41497fa87a09e14661331119f to your computer and use it in GitHub Desktop.
ALCOMの翻訳キーを並び替えた際に使用した補助用スクリプト
import re
source = r"E:\GitHub\vrc-get\vrc-get-gui\locales\ja.json5"
target = r"E:\GitHub\vrc-get\vrc-get-gui\locales\zh_hans.json5"
ordered = "./ordered.txt"
rest = "./rest.txt"
with open(source, "r", encoding="utf_8") as f:
source_lines = f.readlines()
with open(target, "r", encoding="utf_8") as f:
target_lines = f.readlines()
ordered_strs: list[str] = []
for source_line in source_lines:
if key := re.match(" *?\".*?\"", source_line):
for index, target_line in enumerate(target_lines):
if key.group() in target_line:
ordered_strs.append(target_lines.pop(index))
elif re.match("^ *?//", source_line):
ordered_strs.append(source_line)
elif source_line == "\n":
ordered_strs.append("\n")
with open(ordered, "w", encoding="utf_8") as f:
f.writelines(ordered_strs)
with open(rest, "w", encoding="utf_8") as f:
f.writelines(target_lines)
@Sayamame-beans
Copy link
Author

Sayamame-beans commented Sep 10, 2024

これは、ALCOMの翻訳キーを並び替えた際に使用した補助用スクリプトです。

source, targetに、それぞれ参照元の言語と、キーを並び替えたい言語の翻訳ファイルのパスを指定します。
元ファイルが変更されることはありません。(読み取り専用)

sourceファイルにある行のキー部分と一致する文字列を持つ、targetファイル側の行が書き写されていきます。
source側を順番に見ていく中で、コメント行や空行があった場合、それらはsource側をそのまま持ってくるようになっています。
(並び替え時に追加した、整理用のコメントや区切りを持ち込むため)
ただし、これにより、target側のコメント行や、pluralなどによる差分は取り残されてしまいます。

書き写された(ある程度並び替えられた)部分はordered.txtに、取り残された部分はrest.txtに書き込みます。

私はordered.txtとrest.txt、実際のファイル(source, targetのもの)を参照しながら半分手動で並び替え作業を完了させました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment