Skip to content

Commit

Permalink
Merge pull request #59
Browse files Browse the repository at this point in the history
Update human-friendly date to accept a specific locale
  • Loading branch information
abeforgit authored Jan 26, 2023
2 parents bdd3d7e + c387c59 commit 76b71e9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/components/app-chrome.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{{#if @editorDocument.updatedOn}}
<li class="au-c-list-horizontal__item">
<span class="au-c-app-chrome__status">
{{t "utility.savedOn"}} {{human-friendly-date @editorDocument.updatedOn}}
{{t "utility.savedOn"}} {{human-friendly-date @editorDocument.updatedOn locale=this.intl.primaryLocale}}
</span>
</li>
{{else}}
Expand Down
1 change: 1 addition & 0 deletions app/components/app-chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
20 changes: 10 additions & 10 deletions app/helpers/human-friendly-date.js
Original file line number Diff line number Diff line change
@@ -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 '';
}
});
}

0 comments on commit 76b71e9

Please sign in to comment.