From 5df6d39d13e707eee9f5147abd424dfd47eff367 Mon Sep 17 00:00:00 2001 From: Felix Nagel Date: Thu, 19 Jun 2014 18:59:28 +0200 Subject: [PATCH] Datepicker: Improve localization handling, code style --- tests/unit/calendar/calendar_core.js | 8 ++- ui/calendar.js | 79 ++++++++++++++++------------ 2 files changed, 52 insertions(+), 35 deletions(-) diff --git a/tests/unit/calendar/calendar_core.js b/tests/unit/calendar/calendar_core.js index ba424b7b8..0a33363c9 100644 --- a/tests/unit/calendar/calendar_core.js +++ b/tests/unit/calendar/calendar_core.js @@ -64,7 +64,7 @@ test( "base structure", function() { }); test( "Localization", function() { - expect( 5 ); + expect( 10 ); var defaultLocale = Globalize.locale(), element = $( "#calendar" ), @@ -102,6 +102,12 @@ test( "Localization", function() { testLocalization( "Init: " ); element.calendar( "destroy" ); + Globalize.locale( defaultLocale.locale ); + initCalendar(); + Globalize.locale( "de-DE" ); + element.calendar( "refresh" ); + testLocalization( "After init: " ); + Globalize.locale( defaultLocale.locale ); }); diff --git a/ui/calendar.js b/ui/calendar.js index 8e228146a..0d96d4030 100644 --- a/ui/calendar.js +++ b/ui/calendar.js @@ -43,6 +43,7 @@ return $.widget( "ui.calendar", { _create: function() { this.id = this.element.uniqueId().attr( "id" ); + this.labels = Globalize.translate( "datepicker" ); this.date = $.date( this.options.value, this.options.dateFormat ).select(); this.date.eachDay = this.options.eachDay; @@ -198,41 +199,43 @@ return $.widget( "ui.calendar", { _buildHeader: function() { return "
" + - this._buildPreviousLink() + - this._buildNextLink() + - this._buildTitlebar() + - "
"; + this._buildPreviousLink() + + this._buildNextLink() + + this._buildTitlebar() + + ""; }, _buildPreviousLink: function() { - var labels = Globalize.translate( "datepicker" ); + var prevText = this._getTranslation( "prevText" ); - return ""; }, _buildNextLink: function() { - var labels = Globalize.translate( "datepicker" ); + var nextText = this._getTranslation( "nextText" ); - return ""; }, _buildTitlebar: function() { - var labels = Globalize.translate( "datepicker" ); - return "
" + "
" + - this._buildTitle() + - "
" + - ", " + labels.datePickerRole + "" + - "
"; + this._buildTitle() + + "" + + ", " + + this._getTranslation( "datePickerRole" ) + + "" + + ""; }, _buildTitle: function() { @@ -246,36 +249,36 @@ return $.widget( "ui.calendar", { _buildGrid: function() { return "" + - this._buildGridHeading() + - this._buildGridBody() + - "
"; + "aria-labelledby='" + this.id + "-month-label' tabindex='0' " + + "aria-activedescendant='" + this.id + "-" + this.date.day() + "'>" + + this._buildGridHeading() + + this._buildGridBody() + + ""; }, _buildGridHeading: function() { var cells = "", i = 0, - labels = Globalize.translate( "datepicker" ), weekDayLength = this.date.weekdays().length; if ( this.options.showWeek ) { - cells += "" + labels.weekHeader + ""; + cells += "" + this._getTranslation( "weekHeader" ) + ""; } for ( ; i < weekDayLength; i++ ) { cells += this._buildGridHeaderCell( this.date.weekdays()[ i ] ); } return "" + - "" + cells + "" + - ""; + "" + cells + "" + + ""; }, _buildGridHeaderCell: function( day ) { return "" + - "" + - day.shortname + - "" + - ""; + "" + + day.shortname + + "" + + ""; }, _buildGridBody: function() { @@ -330,7 +333,6 @@ return $.widget( "ui.calendar", { _buildDayElement: function( day, selectable ) { var classes = [ "ui-state-default" ], - labels = Globalize.translate( "datepicker" ), content = ""; if ( day === this.date && selectable ) { @@ -355,18 +357,18 @@ return $.widget( "ui.calendar", { } if ( day.today ) { - content += ", " + labels.currentText + ""; + content += ", " + this._getTranslation( "currentText" ) + ""; } return content; }, _buildButtons: function() { - var labels = Globalize.translate( "datepicker" ); - return "
" + - "" + - "
"; + "" + + ""; }, // Refreshing the entire calendar during interaction confuses screen readers, specifically @@ -375,6 +377,7 @@ return $.widget( "ui.calendar", { // with the prev and next links would cause loss of focus issues because the links being // interacted with will disappear while focused. refresh: function() { + this.labels = Globalize.translate( "datepicker" ); // Determine which day gridcell to focus after refresh // TODO: Prevent disabled cells from being focused @@ -382,6 +385,10 @@ return $.widget( "ui.calendar", { this.grid = $( this._buildGrid() ); this.element.find( ".ui-calendar-title" ).html( this._buildTitle() ); this.element.find( ".ui-calendar-calendar" ).replaceWith( this.grid ); + $( ".ui-calendar-prev", this.element ).attr( "title", this.labels.prevText ) + .children().html( this.labels.prevText ); + $( ".ui-calendar-next", this.element ).attr( "title", this.labels.nextText ) + .children().html( this.labels.nextText ); } else { this._refreshMultiplePicker(); } @@ -402,6 +409,10 @@ return $.widget( "ui.calendar", { this.element.find( ".ui-state-focus" ).not( ":first" ).removeClass( "ui-state-focus" ); }, + _getTranslation: function( key ) { + return $( "" ).text( this.labels[ key ] ).html(); + }, + _setHiddenPicker: function() { this.element.attr({ "aria-hidden": "true", -- 2.39.5