This repository has been archived by the owner on Nov 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathstandalone-translations.py
56 lines (52 loc) · 1.69 KB
/
standalone-translations.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
#!/usr/bin/env python3
"""
Generate a messages.json translations file for HTTPS Everywhere Standalone
"""
import re
import sys
import os
import json
source_dir = sys.argv[1]
dest_dir = sys.argv[2]
message_regex = re.compile("<!ENTITY https-everywhere\.([\w.-]+) \"(.*?)\">")
strings_needed = [
"about.version",
"about.rulesets_version",
"menu.globalDisable",
"menu.globalEnable",
"menu.encryptAllSitesEligibleOn",
"menu.encryptAllSitesEligibleOff",
"options.enterDisabledSite",
"options.addDisabledSite",
"options.hostNotFormattedCorrectly",
"options.disabledUrlsListed",
"menu.httpNoWhereExplainedBlocked",
"menu.httpNoWhereExplainedAllowed",
"standalone.proxy_server_info_prefix",
"standalone.transparent_true",
"standalone.transparent_false",
]
def convert(locale):
translation_file = os.path.join(source_dir, locale, "https-everywhere.dtd")
if os.path.isfile(translation_file):
target_messages = {}
with open(translation_file, 'r', encoding='utf-8') as f:
for line in f:
m = message_regex.search(line)
if m:
message_name = m.group(1)
if message_name in strings_needed:
message_value = m.group(2)
message_name = re.sub("[.-]", "_", message_name)
target_messages[message_name] = message_value
return target_messages
else:
return False
with open(os.path.join(dest_dir, "messages.json"), "w") as out_file:
all_target_messages = {}
for locale in os.listdir(source_dir):
if not "." in locale:
target_messages = convert(locale)
if target_messages:
all_target_messages[locale] = target_messages
out_file.write(json.dumps(all_target_messages, sort_keys=True, indent=4))