Datepicker locale support

Also adds general language info to the
horizon JS object.

Change-Id: I8fbf20635ac0a2ae1f3b8f618ce5a37eb53e9cc0
Closes-Bug: 1274145
This commit is contained in:
Randy Bertram 2014-09-09 17:28:17 -04:00
parent e0e46374de
commit 78c917570e
6 changed files with 96 additions and 1 deletions

View File

View File

@ -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',
}

View File

@ -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();

View File

@ -1,4 +1,8 @@
{% load compress %}
{% load datepicker_locale from horizon %}
{% datepicker_locale as DATEPICKER_LOCALE %}
{% compress js %}
<script src='{{ STATIC_URL }}horizon/lib/jquery/jquery.js' type='text/javascript' charset="utf-8"></script>
<script src='{{ STATIC_URL }}horizon/lib/jquery/jquery-migrate.js' type='text/javascript' charset="utf-8"></script>
@ -8,6 +12,11 @@
{% endcompress %}
<script type='text/javascript' charset='utf-8'>
// make translation info available on client
horizon.languageCode = '{{ LANGUAGE_CODE }}';
horizon.languageBidi = '{{ LANGUAGE_BIDI }}';
horizon.datepickerLocale = '{{ DATEPICKER_LOCALE }}';
/* Load angular modules extensions list before we include angular/horizon.js */
var angularModuleExtension = {{ HORIZON_CONFIG.angular_modules|default:"[]"|safe }};
</script>

View File

@ -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 %}
<script type="text/javascript" src="{% url 'horizon:jsi18n' 'horizon' %}"></script>
@ -29,6 +32,9 @@
<script src="{{ STATIC_URL }}bootstrap/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
<script src='{{ STATIC_URL }}horizon/lib/bootstrap_datepicker/bootstrap-datepicker.js' type='text/javascript' charset='utf-8'></script>
{% ifnotequal DATEPICKER_LOCALE 'en' %}
<script src='{{ STATIC_URL }}horizon/lib/bootstrap_datepicker/locales/bootstrap-datepicker.{{ DATEPICKER_LOCALE }}.js' type='text/javascript' charset='utf-8'></script>
{% endifnotequal %}
<script src="{{ STATIC_URL }}horizon/lib/hogan.js" type="text/javascript" charset='utf-8'></script>

View File

@ -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')