-
Notifications
You must be signed in to change notification settings - Fork 57
/
zh-merge_old2new_trans.py
executable file
·60 lines (50 loc) · 1.46 KB
/
zh-merge_old2new_trans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import argparse
parser = argparse.ArgumentParser(description='Merge translated strings to new firmware strings')
parser.add_argument('-o', '--output', type=str, dest='output',
help='file with strings from new firmware', required=True)
parser.add_argument('-i', '--input', type=str, dest='input',
help='input translated file (old firmware strings)', required=True)
args = parser.parse_args()
old = {}
with open(args.input, 'r') as f:
# skip version
f.readline()
for line in f.readlines():
addr, hex_, cn, oth = line.split('|')
old[cn] = oth
new_ = {}
version = ''
with open(args.output, 'r') as f:
version = f.readline()
for i, line in enumerate(f.readlines()):
addr, hex_, cn, lang = line.split('|')
if cn not in new_:
new_[cn] = []
new_[cn].append({
'addr': addr,
'hex': hex_,
'cn': cn,
'lang': lang,
'pos': i
})
tmp_out_filename = args.output + ".out"
bak_out_filename = args.output + ".bak"
out_list = []
for k in new_:
if k in old:
for item in new_[k]:
out_list.append((item['pos'], '%(addr)s|%(hex)s|%(cn)s|' % item + old[k]))
else:
for item in new_[k]:
out_list.append((item['pos'], '%(addr)s|%(hex)s|%(cn)s|%(lang)s' % item))
with open(tmp_out_filename, 'w') as f:
f.write(version)
for item in sorted(out_list, key=lambda x: x[0]):
f.write(item[1])
os.rename(args.output, bak_out_filename)
os.rename(tmp_out_filename, args.output)
os.remove(bak_out_filename)