From df62b48d51b7b83c449dcd04cc98ba7d6ca49479 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 31 Oct 2018 11:55:31 +1100 Subject: [PATCH] UX: remove "at" word from relative dates in local dates Previously we would render "Today at 2:42 PM" Now we render "Today 2:42 PM" This also introduces new strings so it can be properly translated Finally it introduces tests so we can make sure this keeps working --- .../javascripts/discourse-local-dates.js | 22 ++++++++++++++- .../config/locales/client.en.yml | 4 +++ .../acceptance/local-dates-test.js.es6 | 27 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js b/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js index a043ec3ea7d..aad239019cf 100644 --- a/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js +++ b/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js @@ -56,6 +56,26 @@ var displayTimezone = moment.tz.guess(); var relativeTime = relativeTime.tz(displayTimezone); + var d = function(key) { + var translated = I18n.t("discourse_local_dates.relative_dates." + key, { + time: "LT" + }); + translated = translated + .split("LT") + .map(function(w) { + return "[" + w + "]"; + }) + .join("LT"); + return translated; + }; + + var relativeFormat = { + sameDay: d("today"), + nextDay: d("tomorrow"), + lastDay: d("yesterday"), + sameElse: "L" + }; + if ( options.format !== "YYYY-MM-DD HH:mm:ss" && relativeTime.isBetween( @@ -63,7 +83,7 @@ moment().add(2, "day") ) ) { - relativeTime = relativeTime.calendar(); + relativeTime = relativeTime.calendar(null, relativeFormat); } else { relativeTime = relativeTime.format(options.format); } diff --git a/plugins/discourse-local-dates/config/locales/client.en.yml b/plugins/discourse-local-dates/config/locales/client.en.yml index 13f5ccd2b01..8e6b5ee496e 100644 --- a/plugins/discourse-local-dates/config/locales/client.en.yml +++ b/plugins/discourse-local-dates/config/locales/client.en.yml @@ -1,6 +1,10 @@ en: js: discourse_local_dates: + relative_dates: + today: Today %{time} + tomorrow: Tomorrow %{time} + yesterday: Yesterday %{time} title: Insert date create: modal_title: Insert date diff --git a/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-test.js.es6 b/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-test.js.es6 index f672a868e34..a9e9b2461ac 100644 --- a/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-test.js.es6 +++ b/plugins/discourse-local-dates/test/javascripts/acceptance/local-dates-test.js.es6 @@ -6,9 +6,36 @@ acceptance("Local Dates", { settings: { discourse_local_dates_enabled: true }, beforeEach() { clearPopupMenuOptionsCallback(); + }, + afterEach() { + sinon.restore(); } }); +test("at removal", assert => { + let now = moment("2018-06-20").valueOf(); + let timezone = moment.tz.guess(); + + sinon.useFakeTimers(now); + + let html = ``; + + let yesterday = $(html.replace("DATE", "2018-06-19")); + yesterday.applyLocalDates(); + + assert.equal(yesterday.text(), "Yesterday 2:42 PM"); + + let today = $(html.replace("DATE", "2018-06-20")); + today.applyLocalDates(); + + assert.equal(today.text(), "Today 2:42 PM"); + + let tomorrow = $(html.replace("DATE", "2018-06-21")); + tomorrow.applyLocalDates(); + + assert.equal(tomorrow.text(), "Tomorrow 2:42 PM"); +}); + test("local dates bbcode", async assert => { await visit("/"); await click("#create-topic");