diff --git a/app/assets/javascripts/discourse-i18n/src/index.js b/app/assets/javascripts/discourse-i18n/src/index.js index 236f0e1e91f..eafece820b8 100644 --- a/app/assets/javascripts/discourse-i18n/src/index.js +++ b/app/assets/javascripts/discourse-i18n/src/index.js @@ -33,6 +33,10 @@ export class I18n { return this.locale || this.defaultLocale; } + get currentBcp47Locale() { + return this.currentLocale().replace("_", "-"); + } + get pluralizationNormalizedLocale() { if (this.currentLocale() === "pt") { return "pt_PT"; diff --git a/app/assets/javascripts/discourse/app/components/topic-map/topic-views-chart.gjs b/app/assets/javascripts/discourse/app/components/topic-map/topic-views-chart.gjs index 1940de73768..e8f188438dd 100644 --- a/app/assets/javascripts/discourse/app/components/topic-map/topic-views-chart.gjs +++ b/app/assets/javascripts/discourse/app/components/topic-map/topic-views-chart.gjs @@ -161,7 +161,7 @@ export default class TopicViewsChart extends Component { maxTicksLimit: 15, callback: function (value) { const date = new Date(value + oneDay); - return date.toLocaleDateString(undefined, { + return date.toLocaleDateString(I18n.currentBcp47Locale, { month: "2-digit", day: "2-digit", }); @@ -189,7 +189,7 @@ export default class TopicViewsChart extends Component { const today = new Date(); date = today.getUTCDate(); } - return date.toLocaleDateString(undefined, { + return date.toLocaleDateString(I18n.currentBcp47Locale, { month: "2-digit", day: "2-digit", year: "numeric", diff --git a/app/assets/javascripts/discourse/app/components/topic-map/topic-views.gjs b/app/assets/javascripts/discourse/app/components/topic-map/topic-views.gjs index c8c04493d17..254a9135964 100644 --- a/app/assets/javascripts/discourse/app/components/topic-map/topic-views.gjs +++ b/app/assets/javascripts/discourse/app/components/topic-map/topic-views.gjs @@ -1,4 +1,5 @@ import Component from "@glimmer/component"; +import I18n from "I18n"; export default class TopicViews extends Component { adjustAggregatedData(stats) { @@ -6,11 +7,14 @@ export default class TopicViews extends Component { stats.forEach((stat) => { const localDate = new Date(`${stat.viewed_at}T00:00:00Z`); - const localDateStr = localDate.toLocaleDateString(undefined, { - year: "numeric", - month: "2-digit", - day: "2-digit", - }); + const localDateStr = localDate.toLocaleDateString( + I18n.currentBcp47Locale, + { + year: "numeric", + month: "2-digit", + day: "2-digit", + } + ); const existingStat = adjustedStats.find( (s) => s.dateStr === localDateStr @@ -34,7 +38,7 @@ export default class TopicViews extends Component { } formatDate(date) { - return date.toLocaleDateString(undefined, { + return date.toLocaleDateString(I18n.currentBcp47Locale, { month: "2-digit", day: "2-digit", }); diff --git a/app/assets/javascripts/discourse/tests/unit/lib/i18n-test.js b/app/assets/javascripts/discourse/tests/unit/lib/i18n-test.js index 4ec3a448b34..9f068bef4fe 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/i18n-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/i18n-test.js @@ -373,4 +373,22 @@ module("Unit | Utility | i18n", function (hooks) { "Degrades gracefully if MF definitions are not available." ); }); + + test("currentBcp47Locale", function (assert) { + Object.entries({ + pt_BR: "pt-BR", + en_GB: "en-GB", + bs_BA: "bs-BA", + en: "en", + it: "it", + es: "es", + }).forEach(([input, output]) => { + I18n.locale = input; + assert.strictEqual( + I18n.currentBcp47Locale, + output, + `returns '${output}' for '${input}'` + ); + }); + }); });