From 78c917570e2182bfdd8f5cb7eb5cb2c71d8df6f0 Mon Sep 17 00:00:00 2001 From: Randy Bertram Date: Tue, 9 Sep 2014 17:28:17 -0400 Subject: [PATCH] Datepicker locale support Also adds general language info to the horizon JS object. Change-Id: I8fbf20635ac0a2ae1f3b8f618ce5a37eb53e9cc0 Closes-Bug: 1274145 --- horizon/contrib/__init__.py | 0 horizon/contrib/bootstrap_datepicker.py | 68 ++++++++++++++++++++++ horizon/static/horizon/js/horizon.forms.js | 3 +- horizon/templates/horizon/_conf.html | 9 +++ horizon/templates/horizon/_scripts.html | 6 ++ horizon/templatetags/horizon.py | 11 ++++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 horizon/contrib/__init__.py create mode 100644 horizon/contrib/bootstrap_datepicker.py diff --git a/horizon/contrib/__init__.py b/horizon/contrib/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/horizon/contrib/bootstrap_datepicker.py b/horizon/contrib/bootstrap_datepicker.py new file mode 100644 index 0000000000..3741fa689c --- /dev/null +++ b/horizon/contrib/bootstrap_datepicker.py @@ -0,0 +1,68 @@ +# Copyright 2014 IBM Corp. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +# Map Horizon languages to datepicker locales +LOCALE_MAPPING = { + 'ar': 'ar', + 'az': 'az', + 'bg': 'bg', + 'ca': 'ca', + 'cs': 'cs', + 'cy': 'cy', + 'da': 'da', + 'de': 'de', + 'el': 'el', + 'es': 'es', + 'et': 'et', + 'fa': 'fa', + 'fi': 'fi', + 'fr': 'fr', + 'gl': 'gl', + 'he': 'he', + 'hr': 'hr', + 'hu': 'hu', + 'id': 'id', + 'is': 'is', + 'it': 'it', + 'ja': 'ja', + 'ka': 'ka', + 'kk': 'kk', + 'ko': 'kr', # difference between horizon and datepicker + 'lt': 'lt', + 'lv': 'lv', + 'mk': 'mk', + 'ms': 'ms', + 'nb': 'nb', + 'nl-be': 'nl-BE', + 'nl': 'nl', + 'no': 'no', + 'pl': 'pl', + 'pt-br': 'pt-BR', + 'pt': 'pt', + 'ro': 'ro', + 'rs-latin': 'rs-latin', + 'sr': 'rs', # difference between horizon and datepicker + 'ru': 'ru', + 'sk': 'sk', + 'sl': 'sl', + 'sq': 'sq', + 'sv': 'sv', + 'sw': 'sw', + 'th': 'th', + 'tr': 'tr', + 'ua': 'ua', + 'vi': 'vi', + 'zh-cn': 'zh-CN', + 'zh-tw': 'zh-TW', +} diff --git a/horizon/static/horizon/js/horizon.forms.js b/horizon/static/horizon/js/horizon.forms.js index 3b329466dc..4fa094397e 100644 --- a/horizon/static/horizon/js/horizon.forms.js +++ b/horizon/static/horizon/js/horizon.forms.js @@ -86,7 +86,7 @@ horizon.forms = { }, datepicker: function() { - var startDate = $('input#id_start').datepicker() + var startDate = $('input#id_start').datepicker({ language: horizon.datepickerLocale }) .on('changeDate', function(ev) { if (ev.dates[0].valueOf() > endDate.dates[0].valueOf()) { var newDate = new Date(ev.dates[0]); @@ -100,6 +100,7 @@ horizon.forms = { }).data('datepicker'); var endDate = $('input#id_end').datepicker({ + language: horizon.datepickerLocale, startDate: startDate ? startDate.dates[0] : null }).on('changeDate', function(ev) { endDate.hide(); diff --git a/horizon/templates/horizon/_conf.html b/horizon/templates/horizon/_conf.html index b846cac3d9..7909122212 100644 --- a/horizon/templates/horizon/_conf.html +++ b/horizon/templates/horizon/_conf.html @@ -1,4 +1,8 @@ {% load compress %} +{% load datepicker_locale from horizon %} + +{% datepicker_locale as DATEPICKER_LOCALE %} + {% compress js %} @@ -8,6 +12,11 @@ {% endcompress %} diff --git a/horizon/templates/horizon/_scripts.html b/horizon/templates/horizon/_scripts.html index bd87edebde..3e3ec4fbbe 100644 --- a/horizon/templates/horizon/_scripts.html +++ b/horizon/templates/horizon/_scripts.html @@ -1,5 +1,8 @@ {% load compress %} {% load url from future %} +{% load datepicker_locale from horizon %} + +{% datepicker_locale as DATEPICKER_LOCALE %} {% comment %} Django's JavaScript i18n Implementation {% endcomment %} @@ -29,6 +32,9 @@ +{% ifnotequal DATEPICKER_LOCALE 'en' %} + +{% endifnotequal %} diff --git a/horizon/templatetags/horizon.py b/horizon/templatetags/horizon.py index b18172d120..a5e1b16450 100644 --- a/horizon/templatetags/horizon.py +++ b/horizon/templatetags/horizon.py @@ -14,9 +14,13 @@ from __future__ import absolute_import +from horizon.contrib import bootstrap_datepicker + +from django.conf import settings from django import template from django.utils.datastructures import SortedDict from django.utils.encoding import force_text +from django.utils import translation from django.utils.translation import ugettext_lazy as _ from horizon.base import Horizon # noqa @@ -170,3 +174,10 @@ def jstemplate(parser, token): @register.assignment_tag def load_config(): return conf.HORIZON_CONFIG + + +@register.assignment_tag +def datepicker_locale(): + locale_mapping = getattr(settings, 'DATEPICKER_LOCALES', + bootstrap_datepicker.LOCALE_MAPPING) + return locale_mapping.get(translation.get_language(), 'en')