From d5a1653d24d33984654acc1fb2aff2f11eb6bfb5 Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Thu, 3 Oct 2024 10:47:48 +0200 Subject: [PATCH] [1.x] feat: allow to customize time formats through translations (#4053) * Allow to customize time formats * Fix CS --- framework/core/js/src/common/utils/humanTime.ts | 6 ++++-- framework/core/js/src/forum/components/PostStream.js | 3 ++- framework/core/locale/core.yml | 6 ++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/framework/core/js/src/common/utils/humanTime.ts b/framework/core/js/src/common/utils/humanTime.ts index 870153ae28..2c60659a55 100644 --- a/framework/core/js/src/common/utils/humanTime.ts +++ b/framework/core/js/src/common/utils/humanTime.ts @@ -1,4 +1,6 @@ +import app from '../../common/app'; import dayjs from 'dayjs'; +import extractText from './extractText'; /** * The `humanTime` utility converts a date to a localized, human-readable time- @@ -23,9 +25,9 @@ export default function humanTime(time: dayjs.ConfigType): string { // in the string. If it wasn't this year, we'll show the year as well. if (diff < -30 * day) { if (d.year() === dayjs().year()) { - ago = d.format('D MMM'); + ago = d.format(extractText(app.translator.trans('core.lib.datetime_formats.humanTimeShort'))); } else { - ago = d.format('ll'); + ago = d.format(extractText(app.translator.trans('core.lib.datetime_formats.humanTimeLong'))); } } else { ago = d.fromNow(); diff --git a/framework/core/js/src/forum/components/PostStream.js b/framework/core/js/src/forum/components/PostStream.js index 13aa8aeb7f..8f3d487ab3 100644 --- a/framework/core/js/src/forum/components/PostStream.js +++ b/framework/core/js/src/forum/components/PostStream.js @@ -5,6 +5,7 @@ import PostLoading from './LoadingPost'; import ReplyPlaceholder from './ReplyPlaceholder'; import Button from '../../common/components/Button'; import ItemList from '../../common/utils/ItemList'; +import extractText from '../../common/utils/extractText'; /** * The `PostStream` component displays an infinitely-scrollable wall of posts in @@ -292,7 +293,7 @@ export default class PostStream extends Component { // set the index to the last post. this.stream.index = indexFromViewPort !== null ? indexFromViewPort + 1 : this.stream.count(); this.stream.visible = visible; - if (period) this.stream.description = dayjs(period).format('MMMM YYYY'); + if (period) this.stream.description = dayjs(period).format(extractText(app.translator.trans('core.lib.datetime_formats.scrubber'))); } /** diff --git a/framework/core/locale/core.yml b/framework/core/locale/core.yml index 8e2184cc6f..7808c5d0aa 100644 --- a/framework/core/locale/core.yml +++ b/framework/core/locale/core.yml @@ -655,6 +655,12 @@ core: kilo_text: K mega_text: M + # These translations are used for formatting dates using dayjs. + datetime_formats: + humanTimeShort: D MMM + humanTimeLong: ll + scrubber: MMMM YYYY + # These translations are used to punctuate a series of items. series: glue_text: ", "