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
|
#!/usr/bin/env python3
"""
This module adds relay location city and country names to relay-locations.pot
"""
import os
from os import path
import json
import urllib.request
from polib import POFile, POEntry
import colorful as c
SCRIPT_DIR = path.dirname(path.realpath(__file__))
# The output directory for the generated content
OUT_DIR = path.join(SCRIPT_DIR, "out")
# the directory with the generated localizations content
LOCALE_OUT_DIR = path.join(OUT_DIR, "locales")
# Relay locations gettext catalogue template filename (.pot)
RELAY_LOCATIONS_POT_FILENAME = "relay-locations.pot"
def extract_relay_translations():
try:
response = request_relays()
except Exception as e:
print(c.red("Failed to fetch the relays list: {}".format(e)))
raise
locations = response.get("locations")
countries = structure_locations(locations)
extract_relay_locations_pot(countries)
def structure_locations(locations):
countries = {}
for location_key in locations:
location = locations.get(location_key)
country_name = location.get("country")
city_name = location.get("city")
if not "-" in location_key:
print("Location key incorrectly formatted: {}".format(location_key))
continue
country_code, city_code = location_key.split("-")
if country_name is None:
print("Country name missing for {}".format(location_key))
continue
if city_name is None:
print("City name missing for {}".format(location_key))
continue
if country_code not in countries:
countries[country_code] = {"name": country_name, "cities": {}}
country = countries[country_code]
cities = country["cities"]
if city_code not in cities:
cities[city_code] = city_name
else:
print("There are multiple entries for {} in {}".format(city_name, country_name))
return countries
def extract_relay_locations_pot(countries):
pot = POFile(encoding='utf-8', check_for_duplicates=True)
pot.metadata = {"Content-Type": "text/plain; charset=utf-8"}
output_path = path.join(LOCALE_OUT_DIR, RELAY_LOCATIONS_POT_FILENAME)
print("Generating {}".format(output_path))
for country_code in countries:
country = countries[country_code]
entry = POEntry(
msgid=country["name"],
msgstr="",
comment=country_code.upper()
)
pot.append(entry)
cities = country["cities"]
for city_code in cities:
entry = POEntry(
msgid=cities[city_code],
msgstr="",
comment="{} {}".format(country_code.upper(), city_code.upper())
)
try:
pot.append(entry)
except ValueError as err:
print(c.orange("Cannot add an entry: {}".format(err)))
pot.save(output_path)
def request_relays():
request = urllib.request.Request("https://api.mullvad.net/app/v1/relays")
with urllib.request.urlopen(request) as connection:
return json.load(connection)
# Program main()
def main():
# ensure output path exists
if not path.exists(OUT_DIR):
os.makedirs(OUT_DIR)
# ensure locales output path exists
if not path.exists(LOCALE_OUT_DIR):
os.makedirs(LOCALE_OUT_DIR)
# extract translations
extract_relay_translations()
main()
|