From f9a80a6e84ec3dd9e47f547e44c3bce1d134bda0 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Thu, 24 Jan 2019 16:39:41 +0000 Subject: [PATCH] [FIX] web: calendar: take locale to format dates Backport of d9fed5381a78c19ce14ddc8b9233f60d53c65453 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 OPW 1934127 closes odoo/odoo#30909 --- .../js/views/calendar/calendar_renderer.js | 6 +++ .../web/static/tests/views/calendar_tests.js | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/addons/web/static/src/js/views/calendar/calendar_renderer.js b/addons/web/static/src/js/views/calendar/calendar_renderer.js index 6565cbb8385..4c0fabadfdf 100644 --- a/addons/web/static/src/js/views/calendar/calendar_renderer.js +++ b/addons/web/static/src/js/views/calendar/calendar_renderer.js @@ -317,6 +317,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) { @@ -370,6 +375,7 @@ return AbstractRenderer.extend({ }, height: 'parent', unselectAuto: false, + locale: locale, // reset locale when fullcalendar has already been instanciated before now }); this.$calendar.fullCalendar(fc_options); diff --git a/addons/web/static/tests/views/calendar_tests.js b/addons/web/static/tests/views/calendar_tests.js index 00cdfa24dd2..fad84cc43c3 100644 --- a/addons/web/static/tests/views/calendar_tests.js +++ b/addons/web/static/tests/views/calendar_tests.js @@ -6,6 +6,7 @@ var CalendarRenderer = require('web.CalendarRenderer'); var fieldUtils = require('web.field_utils'); var mixins = require('web.mixins'); var testUtils = require('web.test_utils'); +var time = require('web.time'); var session = require('web.session'); @@ -2146,6 +2147,53 @@ QUnit.module('Views', { calendar.destroy(); }); + QUnit.test('fullcalendar initializes with right locale', function (assert) { + assert.expect(1); + + // Do not forward port this part in versions >= saas11.3 + testUtils.patch(time, { + getLangDateFormat: function () { + return 'DD/MM'; + }, + }); + + 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: ' '+ + ''+ + '', + 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); + // Do not forward port this part in versions >= saas11.3 + testUtils.unpatch(time); + + calendar.destroy(); + }); + }); });