From 0730ac3b00ebe79f3b4625319e1bd943978c19d6 Mon Sep 17 00:00:00 2001 From: Elena Poelman Date: Thu, 26 Jan 2023 15:09:16 +0100 Subject: [PATCH 1/2] update human-friendly-date to accept a locale and convert it to an date-fns locale --- app/components/app-chrome.hbs | 2 +- app/components/app-chrome.js | 1 + app/helpers/human-friendly-date.js | 20 ++++++++++---------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/components/app-chrome.hbs b/app/components/app-chrome.hbs index fccca668..5bb9ce0d 100644 --- a/app/components/app-chrome.hbs +++ b/app/components/app-chrome.hbs @@ -15,7 +15,7 @@ {{#if @editorDocument.updatedOn}}
  • - {{t "utility.savedOn"}} {{human-friendly-date @editorDocument.updatedOn}} + {{t "utility.savedOn"}} {{human-friendly-date @editorDocument.updatedOn locale=this.intl.primaryLocale}}
  • {{else}} diff --git a/app/components/app-chrome.js b/app/components/app-chrome.js index 569d2193..c4348d86 100644 --- a/app/components/app-chrome.js +++ b/app/components/app-chrome.js @@ -5,6 +5,7 @@ import { action } from '@ember/object'; export default class AppChromeComponent extends Component { @service currentSession; @service features; + @service intl; get documentStatus() { const status = this.args.documentContainer?.get('status'); diff --git a/app/helpers/human-friendly-date.js b/app/helpers/human-friendly-date.js index 73ff9219..f5e2b148 100644 --- a/app/helpers/human-friendly-date.js +++ b/app/helpers/human-friendly-date.js @@ -1,18 +1,18 @@ -import { helper } from '@ember/component/helper'; import formatRelative from 'date-fns/formatRelative'; -import { nl } from 'date-fns/locale'; +import locales from 'date-fns/locale'; -// this is a helper to mimic the moment-calendar behaviour -export default helper(function humanFriendlyDate( - [referenceDatetime] /*, hash*/ -) { - //If not a date (e.g. date is undefined) return "" for printing on screen. - if (!(referenceDatetime instanceof Date)) return ''; +function getDateFnsLocale(locale) { + return locales[locale] ?? locales[locale.substring(0, 2)]; +} +export default function humanFriendlyDate(date, { locale = 'nl-BE' }) { + if (!(date instanceof Date)) return ''; try { - return formatRelative(referenceDatetime, new Date(), { locale: nl }); + return formatRelative(date, new Date(), { + locale: getDateFnsLocale(locale), + }); } catch (e) { console.error(e); return ''; } -}); +} From c387c5920ff687d6e225ae0f2bf888329ca4749f Mon Sep 17 00:00:00 2001 From: abeforgit Date: Thu, 26 Jan 2023 19:39:00 +0100 Subject: [PATCH 2/2] fix(dates): add proper default arg for human-friendly-date --- app/helpers/human-friendly-date.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/human-friendly-date.js b/app/helpers/human-friendly-date.js index f5e2b148..8de64558 100644 --- a/app/helpers/human-friendly-date.js +++ b/app/helpers/human-friendly-date.js @@ -5,7 +5,7 @@ function getDateFnsLocale(locale) { return locales[locale] ?? locales[locale.substring(0, 2)]; } -export default function humanFriendlyDate(date, { locale = 'nl-BE' }) { +export default function humanFriendlyDate(date, { locale = 'nl-BE' } = {}) { if (!(date instanceof Date)) return ''; try { return formatRelative(date, new Date(), {