Skip to content

Commit

Permalink
UX: pass the current locale to FullCalendar (#458)
Browse files Browse the repository at this point in the history
* UX: pass the current locale to FullCalendar

* DEV: acceptance test
  • Loading branch information
renato authored Oct 20, 2023
1 parent 6c65902 commit e21f970
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
21 changes: 20 additions & 1 deletion assets/javascripts/discourse/initializers/discourse-calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ function loadFullCalendar() {
);
}

function getCurrentBcp47Locale() {
return I18n.currentLocale().replace("_", "-");
}

function getCalendarButtonsText() {
return {
today: I18n.t("discourse_calendar.toolbar_button.today"),
month: I18n.t("discourse_calendar.toolbar_button.month"),
week: I18n.t("discourse_calendar.toolbar_button.week"),
day: I18n.t("discourse_calendar.toolbar_button.day"),
list: I18n.t("discourse_calendar.toolbar_button.list"),
};
}

let eventPopper;
const EVENT_POPOVER_ID = "event-popover";

Expand Down Expand Up @@ -124,7 +138,10 @@ function initializeDiscourseCalendar(api) {
loadFullCalendar().then(() => {
let fullCalendar = new window.FullCalendar.Calendar(
categoryEventNode,
{}
{
locale: getCurrentBcp47Locale(),
buttonText: getCalendarButtonsText(),
}
);
const loadEvents = ajax(
`/discourse-post-event/events.json?category_id=${browsedCategory.id}`
Expand Down Expand Up @@ -284,6 +301,8 @@ function initializeDiscourseCalendar(api) {
return new window.FullCalendar.Calendar($calendar[0], {
timeZone,
timeZoneImpl: "moment-timezone",
locale: getCurrentBcp47Locale(),
buttonText: getCalendarButtonsText(),
nextDayThreshold: "06:00:00",
displayEventEnd: true,
height: 650,
Expand Down
6 changes: 6 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ en:
ve: "Venezuela"
vi: "Virgin Islands (U.S.)"
za: "South Africa"
toolbar_button:
today: "Today"
month: "Month"
week: "Week"
day: "Day"
list: "List"
group_timezones:
search: "Search..."
group_availability: "%{group} availability"
Expand Down
17 changes: 16 additions & 1 deletion test/javascripts/acceptance/category-events-calendar-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import { acceptance, queryAll } from "discourse/tests/helpers/qunit-helpers";
import { test } from "qunit";
import { visit } from "@ember/test-helpers";
import I18n from "discourse-i18n";

acceptance("Discourse Calendar - Category Events Calendar", function (needs) {
needs.user();
Expand Down Expand Up @@ -75,4 +76,18 @@ acceptance("Discourse Calendar - Category Events Calendar", function (needs) {
.exists("Events calendar div exists.");
assert.dom(".fc-view-container").exists("FullCalendar is loaded.");
});

test("uses current locale to display calendar weekday names", async (assert) => {
I18n.locale = "pt_BR";

await visit("/c/bug/1");

assert.deepEqual(
[...queryAll(".fc-day-header span")].map((el) => el.innerText),
["dom.", "seg.", "ter.", "qua.", "qui.", "sex.", "sáb."],
"Week days are translated in the calendar header"
);

I18n.locale = "en";
});
});

0 comments on commit e21f970

Please sign in to comment.