|
| 1 | +# This script converts an i18n file containing Japanese strings into Shift-JIS |
| 2 | +# strings. |
| 3 | + |
| 4 | +import sys |
| 5 | + |
| 6 | +file_contents = b"" |
| 7 | + |
| 8 | +with open(sys.argv[1], "rb") as f: |
| 9 | + lines = f.readlines() |
| 10 | + |
| 11 | +new_lines = [] |
| 12 | + |
| 13 | +for line in lines: |
| 14 | + idx1 = line.find(b'"') |
| 15 | + idx2 = line.find(b'"', idx1 + 1) |
| 16 | + if idx1 == -1 or idx2 == -1: |
| 17 | + # skip this line because it contains no quotation marks |
| 18 | + new_lines.append(line) |
| 19 | + continue |
| 20 | + |
| 21 | + # check if this " is being escaped |
| 22 | + if line[idx2 - 1] == ord("\\"): |
| 23 | + # find the last " that's not escaped |
| 24 | + while idx2 != -1: |
| 25 | + if line[idx2 - 1] != ord("\\"): |
| 26 | + break |
| 27 | + |
| 28 | + idx2 = line.find(b'"', idx2 + 1) |
| 29 | + |
| 30 | + sjis_str = line[idx1 + 1 : idx2].decode("utf-8").encode("shift_jis") |
| 31 | + |
| 32 | + converted_str = b"" |
| 33 | + |
| 34 | + i = 0 |
| 35 | + while i < len(sjis_str): |
| 36 | + byte = sjis_str[i] |
| 37 | + |
| 38 | + if byte == ord("\\") and len(sjis_str) > (i + 1): |
| 39 | + second_char = sjis_str[i + 1] |
| 40 | + |
| 41 | + # handle escape sequences |
| 42 | + if second_char in [ord("r"), ord("n"), ord("\\"), ord('"'), ord("'")]: |
| 43 | + converted_str += b"\\" + bytes(chr(second_char), "utf-8") |
| 44 | + # skip the \ and escaped character |
| 45 | + i += 2 |
| 46 | + continue |
| 47 | + |
| 48 | + converted_str += "\\x{:02X}".format(byte).encode("utf-8") |
| 49 | + i += 1 |
| 50 | + |
| 51 | + new_line = line[: idx1 + 1] + converted_str + line[idx2:] |
| 52 | + new_lines.append(new_line) |
| 53 | + |
| 54 | +file_contents = b"".join(new_lines) |
| 55 | + |
| 56 | +with open(sys.argv[2], "wb") as f: |
| 57 | + f.write(file_contents) |
0 commit comments