Skip to content

Commit 5df6d39

Browse files
fnagelscottgonzalez
authored andcommitted
Datepicker: Improve localization handling, code style
1 parent 2e2787d commit 5df6d39

File tree

2 files changed

+52
-35
lines changed

2 files changed

+52
-35
lines changed

tests/unit/calendar/calendar_core.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ test( "base structure", function() {
6464
});
6565

6666
test( "Localization", function() {
67-
expect( 5 );
67+
expect( 10 );
6868

6969
var defaultLocale = Globalize.locale(),
7070
element = $( "#calendar" ),
@@ -102,6 +102,12 @@ test( "Localization", function() {
102102
testLocalization( "Init: " );
103103
element.calendar( "destroy" );
104104

105+
Globalize.locale( defaultLocale.locale );
106+
initCalendar();
107+
Globalize.locale( "de-DE" );
108+
element.calendar( "refresh" );
109+
testLocalization( "After init: " );
110+
105111
Globalize.locale( defaultLocale.locale );
106112
});
107113

ui/calendar.js

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ return $.widget( "ui.calendar", {
4343

4444
_create: function() {
4545
this.id = this.element.uniqueId().attr( "id" );
46+
this.labels = Globalize.translate( "datepicker" );
4647

4748
this.date = $.date( this.options.value, this.options.dateFormat ).select();
4849
this.date.eachDay = this.options.eachDay;
@@ -198,41 +199,43 @@ return $.widget( "ui.calendar", {
198199

199200
_buildHeader: function() {
200201
return "<div class='ui-calendar-header ui-widget-header ui-helper-clearfix ui-corner-all'>" +
201-
this._buildPreviousLink() +
202-
this._buildNextLink() +
203-
this._buildTitlebar() +
204-
"</div>";
202+
this._buildPreviousLink() +
203+
this._buildNextLink() +
204+
this._buildTitlebar() +
205+
"</div>";
205206
},
206207

207208
_buildPreviousLink: function() {
208-
var labels = Globalize.translate( "datepicker" );
209+
var prevText = this._getTranslation( "prevText" );
209210

210-
return "<button class='ui-calendar-prev ui-corner-all' title='" + labels.prevText + "'>" +
211+
return "<button class='ui-calendar-prev ui-corner-all' title='" +
212+
prevText + "'>" +
211213
"<span class='ui-icon ui-icon-circle-triangle-w'>" +
212-
labels.prevText +
214+
prevText +
213215
"</span>" +
214216
"</button>";
215217
},
216218

217219
_buildNextLink: function() {
218-
var labels = Globalize.translate( "datepicker" );
220+
var nextText = this._getTranslation( "nextText" );
219221

220-
return "<button class='ui-calendar-next ui-corner-all' title='" + labels.nextText + "'>" +
222+
return "<button class='ui-calendar-next ui-corner-all' title='" +
223+
nextText + "'>" +
221224
"<span class='ui-icon ui-icon-circle-triangle-e'>" +
222-
labels.nextText +
225+
nextText +
223226
"</span>" +
224227
"</button>";
225228
},
226229

227230
_buildTitlebar: function() {
228-
var labels = Globalize.translate( "datepicker" );
229-
230231
return "<div role='header' id='" + this.id + "-title'>" +
231232
"<div id='" + this.id + "-month-label' class='ui-calendar-title'>" +
232-
this._buildTitle() +
233-
"</div>" +
234-
"<span class='ui-helper-hidden-accessible'>, " + labels.datePickerRole + "</span>" +
235-
"</div>";
233+
this._buildTitle() +
234+
"</div>" +
235+
"<span class='ui-helper-hidden-accessible'>, " +
236+
this._getTranslation( "datePickerRole" ) +
237+
"</span>" +
238+
"</div>";
236239
},
237240

238241
_buildTitle: function() {
@@ -246,36 +249,36 @@ return $.widget( "ui.calendar", {
246249

247250
_buildGrid: function() {
248251
return "<table class='ui-calendar-calendar' role='grid' aria-readonly='true' " +
249-
"aria-labelledby='" + this.id + "-month-label' tabindex='0' aria-activedescendant='" + this.id + "-" + this.date.day() + "'>" +
250-
this._buildGridHeading() +
251-
this._buildGridBody() +
252-
"</table>";
252+
"aria-labelledby='" + this.id + "-month-label' tabindex='0' " +
253+
"aria-activedescendant='" + this.id + "-" + this.date.day() + "'>" +
254+
this._buildGridHeading() +
255+
this._buildGridBody() +
256+
"</table>";
253257
},
254258

255259
_buildGridHeading: function() {
256260
var cells = "",
257261
i = 0,
258-
labels = Globalize.translate( "datepicker" ),
259262
weekDayLength = this.date.weekdays().length;
260263

261264
if ( this.options.showWeek ) {
262-
cells += "<th class='ui-calendar-week-col'>" + labels.weekHeader + "</th>";
265+
cells += "<th class='ui-calendar-week-col'>" + this._getTranslation( "weekHeader" ) + "</th>";
263266
}
264267
for ( ; i < weekDayLength; i++ ) {
265268
cells += this._buildGridHeaderCell( this.date.weekdays()[ i ] );
266269
}
267270

268271
return "<thead role='presentation'>" +
269-
"<tr role='row'>" + cells + "</tr>" +
270-
"</thead>";
272+
"<tr role='row'>" + cells + "</tr>" +
273+
"</thead>";
271274
},
272275

273276
_buildGridHeaderCell: function( day ) {
274277
return "<th role='columnheader' abbr='" + day.fullname + "' aria-label='" + day.fullname + "'>" +
275-
"<span title='" + day.fullname + "'>" +
276-
day.shortname +
277-
"</span>" +
278-
"</th>";
278+
"<span title='" + day.fullname + "'>" +
279+
day.shortname +
280+
"</span>" +
281+
"</th>";
279282
},
280283

281284
_buildGridBody: function() {
@@ -330,7 +333,6 @@ return $.widget( "ui.calendar", {
330333

331334
_buildDayElement: function( day, selectable ) {
332335
var classes = [ "ui-state-default" ],
333-
labels = Globalize.translate( "datepicker" ),
334336
content = "";
335337

336338
if ( day === this.date && selectable ) {
@@ -355,18 +357,18 @@ return $.widget( "ui.calendar", {
355357
}
356358

357359
if ( day.today ) {
358-
content += "<span class='ui-helper-hidden-accessible'>, " + labels.currentText + "</span>";
360+
content += "<span class='ui-helper-hidden-accessible'>, " + this._getTranslation( "currentText" ) + "</span>";
359361
}
360362

361363
return content;
362364
},
363365

364366
_buildButtons: function() {
365-
var labels = Globalize.translate( "datepicker" );
366-
367367
return "<div class='ui-calendar-buttonpane ui-widget-content'>" +
368-
"<button class='ui-calendar-current'>" + labels.currentText + "</button>" +
369-
"</div>";
368+
"<button class='ui-calendar-current'>" +
369+
this._getTranslation( "currentText" ) +
370+
"</button>" +
371+
"</div>";
370372
},
371373

372374
// Refreshing the entire calendar during interaction confuses screen readers, specifically
@@ -375,13 +377,18 @@ return $.widget( "ui.calendar", {
375377
// with the prev and next links would cause loss of focus issues because the links being
376378
// interacted with will disappear while focused.
377379
refresh: function() {
380+
this.labels = Globalize.translate( "datepicker" );
378381

379382
// Determine which day gridcell to focus after refresh
380383
// TODO: Prevent disabled cells from being focused
381384
if ( this.options.numberOfMonths === 1 ) {
382385
this.grid = $( this._buildGrid() );
383386
this.element.find( ".ui-calendar-title" ).html( this._buildTitle() );
384387
this.element.find( ".ui-calendar-calendar" ).replaceWith( this.grid );
388+
$( ".ui-calendar-prev", this.element ).attr( "title", this.labels.prevText )
389+
.children().html( this.labels.prevText );
390+
$( ".ui-calendar-next", this.element ).attr( "title", this.labels.nextText )
391+
.children().html( this.labels.nextText );
385392
} else {
386393
this._refreshMultiplePicker();
387394
}
@@ -402,6 +409,10 @@ return $.widget( "ui.calendar", {
402409
this.element.find( ".ui-state-focus" ).not( ":first" ).removeClass( "ui-state-focus" );
403410
},
404411

412+
_getTranslation: function( key ) {
413+
return $( "<a>" ).text( this.labels[ key ] ).html();
414+
},
415+
405416
_setHiddenPicker: function() {
406417
this.element.attr({
407418
"aria-hidden": "true",

0 commit comments

Comments
 (0)