diff --git a/src/orig.py b/src/orig.py index 24f2e1a..a41967f 100755 --- a/src/orig.py +++ b/src/orig.py @@ -9,14 +9,37 @@ import parser import flow import fix -japanese_ranges = [ - (0x4E00, 0x9FFF), # Kanji - (0x3040, 0x309F), # Hiragana - (0x30A0, 0x30FF), # Katakana - (0xFF00, 0xFFEF), # Full-width Roman characters and symbols - (0x3000, 0x303F), # CJK symbols and punctuation (including 「」) - (8220, 8220), # The character “ -] +def line_should_be_translated(line: str) -> bool: + japanese_ranges = [ + (0x4E00, 0x9FFF), # Kanji + (0x3040, 0x309F), # Hiragana + (0x30A0, 0x30FF), # Katakana + (0xFF00, 0xFFEF), # Full-width Roman characters and symbols + (0x3000, 0x303F), # CJK symbols and punctuation (including 「」) + (8220, 8220), # The character “ + ] + + + for start, end in japanese_ranges: + if start <= ord(line[0]) <= end: + return True + + # if line starts with special commands + if line.startswith(('!s', '!w', '!d', '@', '¥')): + # ignore line after comment + comment_i = line.find(';') + if comment_i != -1: + line = line[:comment_i] + + # Check if line has japanese chars + for c in line: + for start, end in japanese_ranges: + if start <= ord(c) <= end: + return True + + return False + + debug_current_line = -1 @@ -50,8 +73,9 @@ def process_sections(): origfile.close() -def get_symbols(line: str) -> list[str]: +def get_symbols(line: str) -> (str, list[str]): res = [] + start_symbol = '' inbetween = 0 i = 0 @@ -81,14 +105,23 @@ def get_symbols(line: str) -> list[str]: continue # Only reaches this if it's a symbol - #print(symbol) + + # Each symbol acts as a separator between dialog texts, if we + # have two symbols next to eachother, then we print more dialog + # than we should. Concatenate consicutive symbols together to + # prevent this if inbetween > 0: res.append(symbol) else: - res[-1] += symbol + # Symbols at the start should not have dialog before them. + # Treat them as a special "start_symbol" + if len(res) == 0: + start_symbol = symbol + else: + res[-1] += symbol inbetween = 0 - return res + return start_symbol, res # Given a set of translation files, the original file and the output file @@ -103,19 +136,18 @@ def write_translated(outfile, origfile, translation_file_paths): debug_current_line += 1 # Check if the current line is a dialogue line or not - found = False - for start, end in japanese_ranges: - if start <= ord(line[0]) <= end: - found = True - if found: - symbols = get_symbols(line) + if line_should_be_translated(line): + start, symbols = get_symbols(line) print("\n-", debug_current_line, transfilepath, symbols) outfile.write('`') + outfile.write(start) - _printed_line_jp = "" - _printed_line_en = "" + print([start]) + + _printed_line_jp = start + _printed_line_en = start while True: _printed_line_jp += structure[0].text_jp _printed_line_en += structure[0].text_en