Handle symbols/commands at the start of line
This commit is contained in:
		
							parent
							
								
									f5d41d3068
								
							
						
					
					
						commit
						13f21c6d11
					
				
							
								
								
									
										58
									
								
								src/orig.py
								
								
								
								
							
							
						
						
									
										58
									
								
								src/orig.py
								
								
								
								
							|  | @ -9,14 +9,37 @@ import parser | ||||||
| import flow | import flow | ||||||
| import fix | import fix | ||||||
| 
 | 
 | ||||||
| japanese_ranges = [ | def line_should_be_translated(line: str) -> bool: | ||||||
|  |     japanese_ranges = [ | ||||||
|         (0x4E00, 0x9FFF),  # Kanji |         (0x4E00, 0x9FFF),  # Kanji | ||||||
|         (0x3040, 0x309F),  # Hiragana |         (0x3040, 0x309F),  # Hiragana | ||||||
|         (0x30A0, 0x30FF),  # Katakana |         (0x30A0, 0x30FF),  # Katakana | ||||||
|         (0xFF00, 0xFFEF),  # Full-width Roman characters and symbols |         (0xFF00, 0xFFEF),  # Full-width Roman characters and symbols | ||||||
|         (0x3000, 0x303F),  # CJK symbols and punctuation (including 「」) |         (0x3000, 0x303F),  # CJK symbols and punctuation (including 「」) | ||||||
|         (8220, 8220), # The character “ |         (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 | debug_current_line = -1 | ||||||
| 
 | 
 | ||||||
|  | @ -50,8 +73,9 @@ def process_sections(): | ||||||
|     origfile.close() |     origfile.close() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def get_symbols(line: str) -> list[str]: | def get_symbols(line: str) -> (str, list[str]): | ||||||
|     res = [] |     res = [] | ||||||
|  |     start_symbol = '' | ||||||
| 
 | 
 | ||||||
|     inbetween = 0 |     inbetween = 0 | ||||||
|     i = 0 |     i = 0 | ||||||
|  | @ -81,14 +105,23 @@ def get_symbols(line: str) -> list[str]: | ||||||
|             continue |             continue | ||||||
| 
 | 
 | ||||||
|         # Only reaches this if it's a symbol |         # 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: |         if inbetween > 0: | ||||||
|             res.append(symbol) |             res.append(symbol) | ||||||
|  |         else: | ||||||
|  |             # 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: |             else: | ||||||
|                 res[-1] += symbol |                 res[-1] += symbol | ||||||
|         inbetween = 0 |         inbetween = 0 | ||||||
| 
 | 
 | ||||||
|     return res |     return start_symbol, res | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| # Given a set of translation files, the original file and the output file | # 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 |             debug_current_line += 1 | ||||||
| 
 | 
 | ||||||
|             # Check if the current line is a dialogue line or not |             # 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: |             if line_should_be_translated(line): | ||||||
|                 symbols = get_symbols(line) |                 start, symbols = get_symbols(line) | ||||||
|                 print("\n-", debug_current_line, transfilepath, symbols) |                 print("\n-", debug_current_line, transfilepath, symbols) | ||||||
| 
 | 
 | ||||||
|                 outfile.write('`') |                 outfile.write('`') | ||||||
|  |                 outfile.write(start) | ||||||
| 
 | 
 | ||||||
|                 _printed_line_jp = "" |                 print([start]) | ||||||
|                 _printed_line_en = "" | 
 | ||||||
|  |                 _printed_line_jp = start | ||||||
|  |                 _printed_line_en = start | ||||||
|                 while True: |                 while True: | ||||||
|                     _printed_line_jp += structure[0].text_jp |                     _printed_line_jp += structure[0].text_jp | ||||||
|                     _printed_line_en += structure[0].text_en |                     _printed_line_en += structure[0].text_en | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue