[FIX] web: calendar: take locale to format dates

Before this commit, when opening the calendar view with a specific locale
in the "week" view
the days were translated but the date format was wrong and fell back to english
This was because the translated terms were passed explicitly, but the locale did not
get passed

After this commit, we do what it takes to pass the locale to fullcalendar
and the dates are formatted with the right pattern

Also, there may be a bug in fullcalendar, because just passing the locale in the options
won't work, it should be instanciated first in fullcalendar's "locales cache"

OPW 1922092

closes odoo/odoo#30530
This commit is contained in:
Lucas Perais (lpe)
2019-01-29 08:52:28 +00:00
parent 10d558ef2b
commit e7bbdf884f
2 changed files with 44 additions and 0 deletions
@@ -362,6 +362,11 @@ return AbstractRenderer.extend({
this.$calendar = this.$(".o_calendar_widget");
// This seems like a workaround but apparently passing the locale
// in the options is not enough. We should initialize it beforehand
var locale = moment.locale();
$.fullCalendar.locale(locale);
//Documentation here : http://arshaw.com/fullcalendar/docs/
var fc_options = $.extend({}, this.state.fc_options, {
eventDrop: function (event) {
@@ -414,6 +419,7 @@ return AbstractRenderer.extend({
height: 'parent',
unselectAuto: false,
isRTL: _t.database.parameters.direction === "rtl",
locale: locale, // reset locale when fullcalendar has already been instanciated before now
});
this.$calendar.fullCalendar(fc_options);
@@ -2371,6 +2371,44 @@ QUnit.module('Views', {
calendar.destroy();
});
QUnit.test('fullcalendar initializes with right locale', function (assert) {
assert.expect(1);
var initialLocale = moment.locale();
// This will set the locale to zz
moment.defineLocale('zz', {
longDateFormat: {
L: 'DD/MM/YYYY'
},
weekdaysShort: ["zz1.", "zz2.", "zz3.", "zz4.", "zz5.", "zz6.", "zz7."],
});
var calendar = createView({
View: CalendarView,
model: 'event',
data: this.data,
arch: '<calendar class="o_calendar_test" '+
'date_start="start" '+
'date_stop="stop" '+
'mode="week"> '+
'<field name="name"/>'+
'</calendar>',
archs: archs,
viewOptions: {
initialDate: initialDate,
action: {views: [{viewID: 1, type: 'kanban'}, {viewID: 43, type: 'form'}]}
},
});
assert.strictEqual(calendar.$('.fc-day-header:first').text(), "zz1. 11/12",
'The day should be in the given locale specific format');
moment.locale(initialLocale);
calendar.destroy();
});
});
});