diff --git a/frontend/src/metabase/lib/i18n.js b/frontend/src/metabase/lib/i18n.js index d90a115665bde38e4aceb291f3b859b67d7cd5f1..79df86d869e01862f97d02a552c14b98d25a0337 100644 --- a/frontend/src/metabase/lib/i18n.js +++ b/frontend/src/metabase/lib/i18n.js @@ -1,6 +1,8 @@ import { addLocale, useLocale } from "ttag"; import moment from "moment-timezone"; +import MetabaseSettings from "metabase/lib/settings"; + // note this won't refresh strings that are evaluated at load time export async function loadLocalization(locale) { // we need to be sure to set the initial localization before loading any files @@ -23,6 +25,45 @@ export async function loadLocalization(locale) { setLocalization(translationsObject); } +// Tell Moment.js to use the value of the start-of-week Setting for its current locale +function updateMomentStartOfWeek() { + const startOfWeekDayName = MetabaseSettings.get("start-of-week"); + if (!startOfWeekDayName) { + return; + } + + const START_OF_WEEK_DAYS = [ + "sunday", + "monday", + "tuesday", + "wednesday", + "thursday", + "friday", + "saturday", + ]; + + const startOfWeekDayNumber = START_OF_WEEK_DAYS.indexOf(startOfWeekDayName); + if (startOfWeekDayNumber === -1) { + return; + } + console.log( + "Setting moment.js start of week for Locale", + moment.locale(), + "to", + startOfWeekDayName, + ); + + moment.updateLocale(moment.locale(), { + week: { + // Moment.js dow range Sunday (0) - Saturday (6) + dow: startOfWeekDayNumber, + }, + }); +} + +// if the start of week Setting is updated, update the moment start of week +MetabaseSettings.on("start-of-week", updateMomentStartOfWeek); + export function setLocalization(translationsObject) { const locale = translationsObject.headers.language; @@ -33,6 +74,7 @@ export function setLocalization(translationsObject) { useLocale(locale); moment.locale(locale); + updateMomentStartOfWeek(locale); } // Format a fixed timestamp in local time to see if the current locale defaults diff --git a/frontend/src/metabase/lib/time.js b/frontend/src/metabase/lib/time.js index cfb56c7675f371aeaaf9f7d9f4a0a68f00ec104d..f68559fb94fb17e0dadf4fc64b9746ed893fa3d6 100644 --- a/frontend/src/metabase/lib/time.js +++ b/frontend/src/metabase/lib/time.js @@ -16,7 +16,7 @@ const NUMERIC_UNIT_FORMATS = { .startOf("hour"), "day-of-week": value => moment() - .day(value - 1) + .weekday(value - 1) .startOf("day"), "day-of-month": value => moment("2016-01-01") // initial date must be in month with 31 days to format properly diff --git a/frontend/test/metabase/scenarios/admin/settings/localization.cy.spec.js b/frontend/test/metabase/scenarios/admin/settings/localization.cy.spec.js index 5de16a81eb404ffd999330fe5e2dad6e2da14e61..ad180115312b35d25e1b12474e85b5a555d2b77e 100644 --- a/frontend/test/metabase/scenarios/admin/settings/localization.cy.spec.js +++ b/frontend/test/metabase/scenarios/admin/settings/localization.cy.spec.js @@ -41,7 +41,7 @@ describe("scenarios > admin > permissions", () => { cy.get(".axis.x").contains("April 25, 2016"); }); - it.skip("should display days on X-axis correctly when grouped by 'Day of the Week' (metabase#13604)", () => { + it("should display days on X-axis correctly when grouped by 'Day of the Week' (metabase#13604)", () => { withSampleDataset(({ ORDERS }) => { cy.request("POST", "/api/card", { name: "13604", diff --git a/jsconfig.json b/jsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..fb52ce6717a22ec5297b1bc4e55a0c9780b2dd97 --- /dev/null +++ b/jsconfig.json @@ -0,0 +1 @@ +{"include": ["frontend/src/**", "enterprise/frontent/src/**"]}