From 16153507631da8f60d113d9c38bf9bd95fe19b8e Mon Sep 17 00:00:00 2001 From: Nacosia <161319046+Nacosia@users.noreply.github.com> Date: Thu, 17 Oct 2024 23:03:07 +0800 Subject: [PATCH] Optimize some issues --- .github/scripts/format-translation-script.py | 25 ++++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/.github/scripts/format-translation-script.py b/.github/scripts/format-translation-script.py index 11af8ca8c..6e3336980 100644 --- a/.github/scripts/format-translation-script.py +++ b/.github/scripts/format-translation-script.py @@ -25,6 +25,19 @@ "KDVersionStr" ] +class LineCountingWriter: + def __init__(self, file, init_count=1): + self.file = file + self.count_lines = init_count + + def write(self, content): + lines = content.count('\n') + self.count_lines += lines + self.file.write(content) + + def line_count(self): + return self.count_lines + # Read the csv file def read_csv_with_empty_lines(file_path) -> list: with open(file_path, newline='', encoding='utf-8') as csvfile: @@ -57,18 +70,20 @@ def read_translation_file(file_path) -> list: def write_translated_file(csv_lines : list, translations : list, output_path : str): translated = set() with open(output_path, 'w', encoding='utf-8') as file: + writer = LineCountingWriter(file) for line in csv_lines: if not line or not line[1]: - file.write('\n') + writer.write('\n') continue key, original, *_ = line - if original in translated: continue - translated.add(original) + if writer.line_count() % 2 == 0: + writer.write('\n') + # The next line of the original text corresponding to the List is the translation. If the translation is not found,will use "### Original" instead. # When the translation is the same as the original text, "### Original" is also used because it is meaningless. try: @@ -77,10 +92,10 @@ def write_translated_file(csv_lines : list, translations : list, output_path : s if not translation or (original == translation) : raise ValueError except (ValueError, IndexError): - file.write(f'### {original}\n') + writer.write(f'### {original}\n') continue - file.write(f'{original}\n{translation}\n') + writer.write(f'{original}\n{translation}\n') original_csv_path = 'Screens/MiniGame/KinkyDungeon/Text_KinkyDungeon.csv'