diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2019-10-10 15:27:23 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2019-10-14 14:12:30 +0200 |
| commit | e351a2b757e9c18d6312f89952233aca1c3c330b (patch) | |
| tree | cf37db452eca75c5b6ba6858d359455781dd6f45 /gui/scripts | |
| parent | 2bd86da17f2dcf1be913ba18c7ec1f5d87472e99 (diff) | |
| download | mullvadvpn-e351a2b757e9c18d6312f89952233aca1c3c330b.tar.xz mullvadvpn-e351a2b757e9c18d6312f89952233aca1c3c330b.zip | |
Output countries into relay-locations.pot
Diffstat (limited to 'gui/scripts')
| -rw-r--r-- | gui/scripts/extract-geo-data.py | 112 |
1 files changed, 101 insertions, 11 deletions
diff --git a/gui/scripts/extract-geo-data.py b/gui/scripts/extract-geo-data.py index 0719dfc92e..f54085a052 100644 --- a/gui/scripts/extract-geo-data.py +++ b/gui/scripts/extract-geo-data.py @@ -308,6 +308,16 @@ def extract_relay_locations_pot(countries): print "Generating relay-locations.pot" for country in countries: + country_name = country.get("name") + if country_name is not None: + entry = POEntry( + msgid=country_name, + msgstr=u"", + comment=country.get("code").upper() + ) + pot.append(entry) + print u"{} ({})".format(country_name, country.get("code")).encode('utf-8') + cities = country.get("cities") if cities is not None: for city in cities: @@ -319,7 +329,7 @@ def extract_relay_locations_pot(countries): comment=u"{} {}".format(country.get("code").upper(), city.get("code").upper()) ) pot.append(entry) - print u" {} ({})".format(city["name"], city["code"]).encode('utf-8') + print u" {} ({})".format(city_name, city.get("code")).encode('utf-8') pot.save(output_path) @@ -352,20 +362,21 @@ def print_stats_table(title, data): def translate_relay_locations_pot(countries): - place_translator = PlaceTranslator() + country_translator = CountryTranslator() + city_translator = PlaceTranslator() stats = [] for locale in os.listdir(LOCALE_DIR): locale_dir = path.join(LOCALE_DIR, locale) if path.isdir(locale_dir): print "Generating {}/relay-locations.po".format(locale) - (hits, misses) = translate_relay_locations(place_translator, countries, locale) + (hits, misses) = translate_relay_locations(country_translator, city_translator, countries, locale) stats.append((locale, hits, misses)) print_stats_table("Relay location translations", stats) -def translate_relay_locations(place_translator, countries, locale): +def translate_relay_locations(country_translator, city_translator, countries, locale): po = POFile(encoding='utf-8', check_for_duplicates=True) po.metadata = {"Content-Type": "text/plain; charset=utf-8"} locale_out_dir = path.join(LOCALE_OUT_DIR, locale) @@ -380,8 +391,33 @@ def translate_relay_locations(place_translator, countries, locale): for country in countries: country_name = country.get("name") country_code = country.get("code") - cities = country.get("cities") + translated_country_name = country_translator.translate(locale, country_code) + found_country_translation = translated_country_name is not None + # Default to empty string if no translation was found + if found_country_translation: + hits += 1 + else: + translated_country_name = "" + misses += 1 + + log_message = u"{} ({}) -> \"{}\"".format( + country_name, country_code, translated_country_name).encode('utf-8') + if found_country_translation: + print c.green(log_message) + else: + print c.orange(log_message) + + # translate country + entry = POEntry( + msgid=country_name, + msgstr=translated_country_name, + comment=country_code.upper() + ) + po.append(entry) + + # translate cities + cities = country.get("cities") if cities is None: print c.orange(u"Skip {} ({}) because no cities were found.".format( country_name, country_code)) @@ -396,12 +432,12 @@ def translate_relay_locations(place_translator, countries, locale): # Make sure to append the US state back to the translated name of the city if country_code == "us": split = city_name.rsplit(",", 2) - translated_name = place_translator.translate(locale, split[0].strip()) + translated_name = city_translator.translate(locale, split[0].strip()) if translated_name is not None and len(split) > 1: translated_name = u"{}, {}".format(translated_name, split[1].strip()) else: - translated_name = place_translator.translate(locale, city_name) + translated_name = city_translator.translate(locale, city_name) # Default to empty string if no translation was found found_translation = translated_name is not None @@ -421,7 +457,7 @@ def translate_relay_locations(place_translator, countries, locale): entry = POEntry( msgid=city_name, msgstr=translated_name, - comment=u"{} {}".format(country.get("code").upper(), city.get("code").upper()) + comment=u"{} {}".format(country_code.upper(), city_code.upper()) ) po.append(entry) @@ -432,6 +468,60 @@ def translate_relay_locations(place_translator, countries, locale): ### HELPERS ### +class CountryTranslator(object): + """ + This class provides facilities for translating countries + """ + + def __init__(self): + super(CountryTranslator, self).__init__() + + self.dataset = self.__build_index() + + def translate(self, locale, iso_a2): + """ + Lookup the countries dataset for the country matching by ISO A2 code + + When there is a match, the function looks for the translation using the given locale or using + the language component of it. + + Returns None when either there is no match or there is no translation for the matched city. + """ + props = self.dataset.get(iso_a2.upper()) + + if props is not None: + name_key = "name_" + map_locale(locale) + value = props.get(name_key) + + if value is None: + print c.orange(u"Missing translation for {} ({}) under the {} key".format( + iso_a2, locale, name_key).encode('utf-8')) + else: + return value + + return None + + + def __build_index(self): + """ + Private helper to build the index for the geo dataset, that can be used to speed up the + translations lookup. + """ + shape_path = get_shape_path("ne_50m_admin_0_countries") + dataset = dict() + + # build a hash map of the entire datasource in memory + with fiona.open(shape_path, "r") as source: + for feat in source: + props = lower_dict_keys(feat["properties"]) + + iso_a2 = props.get("iso_a2") + if iso_a2 is not None: + dataset[iso_a2.upper()] = props + + return dataset + + class PlaceTranslator(object): """ This class provides facilities for translating places from English. @@ -442,7 +532,7 @@ class PlaceTranslator(object): self.dataset = self.__build_index() - def translate(self, locale, english_city_name): + def translate(self, locale, english_name): """ Lookup the populated places dataset for the city matching by name, par name or name representation in ASCII. @@ -452,7 +542,7 @@ class PlaceTranslator(object): Returns None when either there is no match or there is no translation for the matched city. """ - props = self.dataset.get(english_city_name) + props = self.dataset.get(english_name) if props is not None: name_key = "name_" + map_locale(locale) @@ -460,7 +550,7 @@ class PlaceTranslator(object): if value is None: print c.orange(u"Missing translation for {} ({}) under the {} key".format( - english_city_name, locale, name_key).encode('utf-8')) + english_name, locale, name_key).encode('utf-8')) else: return value |
