forked from iWangJiaxiang/autosub
-
Notifications
You must be signed in to change notification settings - Fork 246
/
update_po_files.py
130 lines (112 loc) · 3.85 KB
/
update_po_files.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Import built-in modules
import os
import sys
import subprocess
name = "autosub"
def run_cmd(cmd):
print(cmd)
cmd_prcs = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
while True:
line_ = cmd_prcs.stdout.readline()
if not line_:
break
print (line_)
if __name__ == "__main__":
if len(sys.argv) > 1:
locale_name = sys.argv[1]
else:
locale_name = "zh_CN"
os.chdir(os.pardir)
locale_dir = "{name}/data/locale/{ln}/LC_MESSAGES/".format(
name=name,
ln=locale_name)
if not os.path.exists(locale_dir):
os.makedirs(locale_dir)
locale_codes = []
for item in os.listdir(name):
filename = os.path.splitext(item)
if filename[-1] == ".py":
locale_codes.append(filename[0])
for python_code in locale_codes:
code = "{name}/{py}.py".format(
name=name,
py=python_code)
if python_code != "__init__":
old_po = "{ld}{name}.{py}.po".format(
ld=locale_dir,
name=name,
py=python_code)
new_mo = "{ld}{name}.{py}.mo".format(
ld=locale_dir,
name=name,
py=python_code)
if os.path.exists(old_po):
new_po = "{ld}{name}.{py}.new.po".format(
ld=locale_dir,
name=name,
py=python_code)
new_po_name = "{name}.{py}.new".format(
name=name,
py=python_code)
xgt_cmd = "xgettext \"{code}\" -L Python -d \"{new_po_name}\" -p \"{ld}\"".format(
code=code,
new_po_name=new_po_name,
ld=locale_dir)
msgmerge_cmd = "msgmerge \"{old_po}\" \"{new_po}\" -U".format(
old_po=old_po,
new_po=new_po
)
run_cmd(xgt_cmd)
run_cmd(msgmerge_cmd)
os.remove(new_po)
else:
old_po = "{name}.{py}".format(
name=name,
py=python_code)
xgt_cmd = "xgettext \"{code}\" -L Python -d \"{old_po}\" -p \"{ld}\"".format(
code=code,
old_po=old_po,
ld=locale_dir)
run_cmd(xgt_cmd)
else:
old_po = "{ld}{name}.po".format(
ld=locale_dir,
name=name)
new_mo = "{ld}{name}.mo".format(
ld=locale_dir,
name=name)
if os.path.exists(old_po):
new_po = "{ld}{name}.new.po".format(
ld=locale_dir,
name=name)
new_po_name = "{name}.new".format(
name=name)
xgt_cmd = "xgettext \"{code}\" -L Python -d \"{new_po_name}\" -p \"{ld}\"".format(
code=code,
new_po_name=new_po_name,
ld=locale_dir)
msgmerge_cmd = "msgmerge \"{old_po}\" \"{new_po}\" -U".format(
old_po=old_po,
new_po=new_po
)
run_cmd(xgt_cmd)
run_cmd(msgmerge_cmd)
os.remove(new_po)
else:
old_po = "{name}".format(
name=name)
xgt_cmd = "xgettext \"{code}\" -L Python -d \"{old_po}\" -p \"{ld}\"".format(
code=code,
old_po=old_po,
ld=locale_dir)
run_cmd(xgt_cmd)
msgfmt_cmd = "msgfmt \"{old_po}\" -o \"{new_mo}\"".format(
old_po=old_po,
new_mo=new_mo
)
run_cmd(msgfmt_cmd)