Make timezones translatable

Use timezone info in Babel to translate timezones.  Sort
timezones by offset.  Update format to:
    UTC -06:00: United States (Chicago) Time

Thanks amotoki for suggesting to use Babel to handle this!

Not updating environment_version in run_tests.sh because Babel isn't
a new requirement; it's just being made explicit.

Change-Id: I15d39b94d441e9e155870a0593d11e67e68f9bd9
Closes-Bug: #1333927
This commit is contained in:
Doug Fish 2014-09-26 10:10:54 -05:00
parent 91d20d9a6c
commit 8c50905a29
3 changed files with 30 additions and 11 deletions

View File

@ -15,6 +15,8 @@
from datetime import datetime # noqa
import string
import babel
import babel.dates
from django.conf import settings
from django import shortcuts
from django.utils import encoding
@ -43,6 +45,14 @@ class UserSettingsForm(forms.SelfHandlingForm):
help_text=_("Number of items to show per "
"page"))
@staticmethod
def _sorted_zones():
d = datetime(datetime.today().year, 1, 1)
zones = [(tz, pytz.timezone(tz).localize(d).strftime('%z'))
for tz in pytz.common_timezones]
zones.sort(key=lambda zone: int(zone[1]))
return zones
def __init__(self, *args, **kwargs):
super(UserSettingsForm, self).__init__(*args, **kwargs)
@ -61,19 +71,27 @@ class UserSettingsForm(forms.SelfHandlingForm):
self.fields['language'].choices = languages
# Timezones
d = datetime(datetime.today().year, 1, 1)
timezones = []
for tz in pytz.common_timezones:
language = translation.get_language()
current_locale = translation.to_locale(language)
babel_locale = babel.Locale.parse(current_locale)
for tz, offset in self._sorted_zones():
try:
utc_offset = pytz.timezone(tz).localize(d).strftime('%z')
utc_offset = " (UTC %s:%s)" % (utc_offset[:3], utc_offset[3:])
utc_offset = _("UTC %(hour)s:%(min)s") % {"hour": offset[:3],
"min": offset[3:]}
except Exception:
utc_offset = ""
if tz != "UTC":
tz_name = "%s%s" % (tz, utc_offset)
if tz == "UTC":
tz_name = _("UTC")
elif tz == "GMT":
tz_name = _("GMT")
else:
tz_name = tz
tz_label = babel.dates.get_timezone_location(tz,
locale=babel_locale)
# Translators: UTC offset and timezone label
tz_name = _("%(offset)s: %(label)s") % {"offset": utc_offset,
"label": tz_label}
timezones.append((tz, tz_name))
self.fields['timezone'].choices = timezones

View File

@ -26,10 +26,10 @@ class UserSettingsTest(test.TestCase):
def test_timezone_offset_is_displayed(self):
res = self.client.get(INDEX_URL)
self.assertContains(res, "Australia/Melbourne (UTC +11:00)")
self.assertContains(res, "Europe/Moscow (UTC +04:00)")
self.assertContains(res, "Atlantic/Stanley (UTC -03:00)")
self.assertContains(res, "Pacific/Honolulu (UTC -10:00)")
self.assertContains(res, "UTC +11:00: Australia (Melbourne) Time")
self.assertContains(res, "UTC +04:00: Russia (Moscow) Time")
self.assertContains(res, "UTC -03:00: Falkland Islands Time")
self.assertContains(res, "UTC -10:00: United States (Honolulu) Time")
def test_display_language(self):
# Add an unknown language to LANGUAGES list

View File

@ -9,6 +9,7 @@
# PBR should always appear first
pbr>=0.6,!=0.7,<1.0
# Horizon Core Requirements
Babel>=1.3
Django>=1.4.2,<1.7
django_compressor>=1.4
django_openstack_auth>=1.1.7