Skip to content

Instantly share code, notes, and snippets.

@heiybb
Created September 4, 2023 02:47
Show Gist options
  • Save heiybb/d19cd7ea2ed123bb2c8194653cf6a7e0 to your computer and use it in GitHub Desktop.
Save heiybb/d19cd7ea2ed123bb2c8194653cf6a7e0 to your computer and use it in GitHub Desktop.
火炬之光2 翻译文件清理
from tl2_utils import dat_format
def remove_duplicates_ordered(input_list):
seen = set()
output_list = []
for item in input_list:
if item not in seen:
seen.add(item)
output_list.append(item)
return output_list
translation_file = 'E:\Program Files (x86)\Steam\steamapps\common\Torchlight II\mods\IMBA-符文觉醒-暗黑传奇\MEDIA\TRANSLATIONS\MANDARIN\TRANSLATION.DAT'
list_original = []
list_translation = []
PATTERN_ORI = '<STRING>ORIGINAL:'
PATTERN_TRA = '<STRING>TRANSLATION:'
with open(translation_file, 'r', encoding='utf_16_le') as translation_file_handler:
translation_block_lines = translation_file_handler.readlines()[1:-1]
for idx, line in enumerate(translation_block_lines):
if PATTERN_ORI in line:
list_original.append(line.strip().replace(PATTERN_ORI, ''))
if PATTERN_TRA not in translation_block_lines[idx + 1]:
print("TRANSLATION not found in the following line: " + idx)
if PATTERN_TRA in line:
list_translation.append(line.strip().replace(PATTERN_TRA, ''))
if PATTERN_ORI not in translation_block_lines[idx - 1]:
print("ORIGINAL not found in the upper line: " + idx)
assert len(list_original) == len(list_translation)
clean_o = remove_duplicates_ordered(list_original)
translation_file_new = 'E:\Program Files (x86)\Steam\steamapps\common\Torchlight II\mods\IMBA-符文觉醒-暗黑传奇\MEDIA\TRANSLATIONS\MANDARIN\TRANSLATION_NEW.DAT'
with open(translation_file_new, 'w', encoding="utf-16") as translation_file_new_handler:
translation_file_new_handler.write(dat_format('[TRANSLATIONS]', 0))
for orig in clean_o:
text_o = orig
text_t = list_translation[list_original.index(orig)]
translation_file_new_handler.write(dat_format('[TRANSLATION]', 1))
translation_file_new_handler.write(dat_format('<STRING>ORIGINAL:' + text_o, 2))
translation_file_new_handler.write(dat_format('<STRING>TRANSLATION:' + text_t, 2))
translation_file_new_handler.write(dat_format('[/TRANSLATION]', 1))
translation_file_new_handler.write(dat_format('[/TRANSLATIONS]', 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment