Last active
October 4, 2017 07:29
-
-
Save imankulov/4f73077794ecaaf7a493accd4ab6af9f to your computer and use it in GitHub Desktop.
Sample script to convert timezone names to localized city names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from babel import Locale | |
es = Locale('es') | |
pt = Locale('pt') | |
ru = Locale('ru') | |
print es.time_zones['America/New_York']['city'] | |
print pt.time_zones['America/New_York']['city'] | |
print ru.time_zones['America/New_York']['city'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import zipfile | |
import xml.etree.ElementTree as ET | |
# Download this file from | |
# http://www.unicode.org/Public/cldr/31.0.1/cldr-common-31.0.1.zip | |
DEFAULT_FILENAME = 'cldr-common-31.0.1.zip' | |
class Locale(object): | |
def __init__(self, locale_name, filename=DEFAULT_FILENAME): | |
self.locale_name = locale_name | |
self.filename = filename | |
self._xml_tree = None | |
def get_city(self, timezone_name): | |
tree = self.get_xml_tree() | |
el = tree.getroot().find('.//timeZoneNames/zone[@type="%s"]' % timezone_name) | |
if el: | |
return el.findtext('./exemplarCity') | |
def get_xml_tree(self): | |
if self._xml_tree is None: | |
with zipfile.ZipFile(self.filename) as zf: | |
fd = zf.open('common/main/%s.xml' % self.locale_name) | |
self._xml_tree = ET.parse(fd) | |
return self._xml_tree | |
# some examples | |
ru = Locale('ru') | |
pt = Locale('pt') | |
es = Locale('es') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: import cldr_timezones | |
In [2]: cldr_timezones.es.get_city('America/New_York') | |
Out[2]: 'Nueva York' | |
In [3]: cldr_timezones.pt.get_city('America/New_York') | |
Out[3]: 'Nova York' | |
In [4]: cldr_timezones.ru.get_city('America/New_York') | |
Out[4]: 'Нью-Йорк' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment