Skip to content

Commit bf73786

Browse files
authored
Merge pull request #393 from xmvziron/backport-i18n
Backport i18n script
2 parents 91b0967 + f620dc0 commit bf73786

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

scripts/configure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def configure(build_type):
6464
writer.rule("extracticon", "python scripts/extract_icon.py --output $out")
6565
writer.rule(
6666
"geni18n",
67-
"""python -c "import sys; open(sys.argv[2], 'wb').write(open(sys.argv[1], 'rb').read().decode('utf8').encode('shift_jis'))" $in $out""",
67+
"python scripts/generate_i18n.py $in $out",
6868
)
6969
writer.rule(
7070
"genstubs",

scripts/generate_i18n.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)