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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Check translation lacked or disused.
Example:
in OnionShare directory
$ check_lacked_trans.py
de disused choose_file
de disused gui_starting_server
de lacked gui_canceled
de lacked gui_starting_server1
de lacked gui_starting_server2
de lacked gui_starting_server3
en disused choose_file
es disused choose_file
es disused gui_starting_server
...
1. search `{{strings.translation_key}}` and `strings._('translation_key')`
from .py or .html files.
2. load translation key from locale/*.json.
3. compare these.
"""
import argparse
import re
import os
import codecs
import json
import sys
def arg_parser():
desc = __doc__.strip().splitlines()[0]
p = argparse.ArgumentParser(description=desc)
p.add_argument(
"-d",
default=".",
help="onionshare directory",
metavar="ONIONSHARE_DIR",
dest="onionshare_dir",
)
p.add_argument(
"--show-all-keys",
action="store_true",
help="show translation key in source and exit",
),
p.add_argument(
"-l",
default="all",
help="language code (default: all)",
metavar="LANG_CODE",
dest="lang_code",
)
return p
def files_in(*dirs):
dir = os.path.join(*dirs)
files = os.listdir(dir)
return [os.path.join(dir, f) for f in files]
def main():
parser = arg_parser()
args = parser.parse_args()
dir = args.onionshare_dir
src = (
files_in(dir, "onionshare_gui")
+ files_in(dir, "onionshare_gui/tab")
+ files_in(dir, "onionshare_gui/tab/mode")
+ files_in(dir, "onionshare_gui/tab/mode/chat_mode")
+ files_in(dir, "onionshare_gui/tab/mode/receive_mode")
+ files_in(dir, "onionshare_gui/tab/mode/share_mode")
+ files_in(dir, "onionshare_gui/tab/mode/website_mode")
+ files_in(dir, "install/scripts")
)
filenames = [p for p in src if p.endswith(".py")]
lang_code = args.lang_code
translate_keys = set()
for filename in filenames:
# load translate key from python source
with open(filename) as f:
src = f.read()
# find all the starting strings
start_substr = "strings._\("
starting_indices = [m.start() for m in re.finditer(start_substr, src)]
for starting_i in starting_indices:
# are we dealing with single quotes or double quotes?
quote = None
inc = 0
while True:
quote_i = starting_i + len("strings._(") + inc
if src[quote_i] == '"':
quote = '"'
break
if src[quote_i] == "'":
quote = "'"
break
inc += 1
# find the starting quote
starting_i = src.find(quote, starting_i)
if starting_i:
starting_i += 1
# find the ending quote
ending_i = src.find(quote, starting_i)
if ending_i:
key = src[starting_i:ending_i]
translate_keys.add(key)
if args.show_all_keys:
for k in sorted(translate_keys):
print(k)
sys.exit()
if lang_code == "all":
locale_files = [f for f in files_in(dir, "share/locale") if f.endswith(".json")]
else:
locale_files = [
f
for f in files_in(dir, "share/locale")
if f.endswith("%s.json" % lang_code)
]
for locale_file in locale_files:
with codecs.open(locale_file, "r", encoding="utf-8") as f:
trans = json.load(f)
# trans -> {"key1": "translate-text1", "key2": "translate-text2", ...}
locale_keys = set(trans.keys())
disused = locale_keys - translate_keys
lacked = translate_keys - locale_keys
locale, ext = os.path.splitext(os.path.basename(locale_file))
for k in sorted(disused):
print(locale, "disused", k)
for k in sorted(lacked):
print(locale, "lacked", k)
if __name__ == "__main__":
main()
|