2018-01-16 00:17:23 -05:00
|
|
|
#!/usr/bin/env python3
|
2014-09-24 13:18:03 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-11-18 11:39:04 -05:00
|
|
|
"""
|
2014-09-24 13:18:03 -04:00
|
|
|
Check translation lacked or disused.
|
|
|
|
|
|
|
|
Example:
|
2014-10-26 09:40:41 -04:00
|
|
|
in OnionShare directory
|
2014-09-24 13:18:03 -04:00
|
|
|
$ 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.
|
|
|
|
|
2014-11-18 11:39:04 -05:00
|
|
|
"""
|
2014-09-24 13:18:03 -04:00
|
|
|
|
|
|
|
|
2020-09-14 18:57:46 -04:00
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import codecs
|
|
|
|
import json
|
|
|
|
import sys
|
2014-09-24 13:18:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
def arg_parser():
|
|
|
|
desc = __doc__.strip().splitlines()[0]
|
|
|
|
p = argparse.ArgumentParser(description=desc)
|
2019-10-13 00:01:25 -04:00
|
|
|
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",
|
|
|
|
)
|
2014-09-24 13:18:03 -04:00
|
|
|
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
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
src = (
|
2020-09-14 18:57:46 -04:00
|
|
|
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")
|
2019-10-13 00:01:25 -04:00
|
|
|
+ files_in(dir, "install/scripts")
|
|
|
|
)
|
2020-09-14 18:57:46 -04:00
|
|
|
filenames = [p for p in src if p.endswith(".py")]
|
2014-09-24 13:18:03 -04:00
|
|
|
|
2018-01-16 00:32:26 -05:00
|
|
|
lang_code = args.lang_code
|
|
|
|
|
2014-09-24 13:18:03 -04:00
|
|
|
translate_keys = set()
|
2020-09-14 18:57:46 -04:00
|
|
|
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
|
|
|
|
elif src[quote_i] == "'":
|
|
|
|
quote = "'"
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
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)
|
2014-09-24 13:18:03 -04:00
|
|
|
|
|
|
|
if args.show_all_keys:
|
|
|
|
for k in sorted(translate_keys):
|
2018-01-16 00:17:23 -05:00
|
|
|
print(k)
|
2014-09-24 13:18:03 -04:00
|
|
|
sys.exit()
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
if lang_code == "all":
|
|
|
|
locale_files = [f for f in files_in(dir, "share/locale") if f.endswith(".json")]
|
2018-01-16 00:32:26 -05:00
|
|
|
else:
|
2019-10-13 00:01:25 -04:00
|
|
|
locale_files = [
|
|
|
|
f
|
|
|
|
for f in files_in(dir, "share/locale")
|
|
|
|
if f.endswith("%s.json" % lang_code)
|
|
|
|
]
|
2014-09-24 13:18:03 -04:00
|
|
|
for locale_file in locale_files:
|
2019-10-13 00:01:25 -04:00
|
|
|
with codecs.open(locale_file, "r", encoding="utf-8") as f:
|
2014-09-24 13:18:03 -04:00
|
|
|
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):
|
2019-10-13 00:01:25 -04:00
|
|
|
print(locale, "disused", k)
|
2014-09-24 13:18:03 -04:00
|
|
|
|
|
|
|
for k in sorted(lacked):
|
2019-10-13 00:01:25 -04:00
|
|
|
print(locale, "lacked", k)
|
2014-09-24 13:18:03 -04:00
|
|
|
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
if __name__ == "__main__":
|
2014-09-24 13:18:03 -04:00
|
|
|
main()
|