Skip to content

Commit d5a1653

Browse files
authored
[1.x] feat: allow to customize time formats through translations (#4053)
* Allow to customize time formats * Fix CS
1 parent db605bd commit d5a1653

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

framework/core/js/src/common/utils/humanTime.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import app from '../../common/app';
12
import dayjs from 'dayjs';
3+
import extractText from './extractText';
24

35
/**
46
* The `humanTime` utility converts a date to a localized, human-readable time-
@@ -23,9 +25,9 @@ export default function humanTime(time: dayjs.ConfigType): string {
2325
// in the string. If it wasn't this year, we'll show the year as well.
2426
if (diff < -30 * day) {
2527
if (d.year() === dayjs().year()) {
26-
ago = d.format('D MMM');
28+
ago = d.format(extractText(app.translator.trans('core.lib.datetime_formats.humanTimeShort')));
2729
} else {
28-
ago = d.format('ll');
30+
ago = d.format(extractText(app.translator.trans('core.lib.datetime_formats.humanTimeLong')));
2931
}
3032
} else {
3133
ago = d.fromNow();

framework/core/js/src/forum/components/PostStream.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PostLoading from './LoadingPost';
55
import ReplyPlaceholder from './ReplyPlaceholder';
66
import Button from '../../common/components/Button';
77
import ItemList from '../../common/utils/ItemList';
8+
import extractText from '../../common/utils/extractText';
89

910
/**
1011
* The `PostStream` component displays an infinitely-scrollable wall of posts in
@@ -292,7 +293,7 @@ export default class PostStream extends Component {
292293
// set the index to the last post.
293294
this.stream.index = indexFromViewPort !== null ? indexFromViewPort + 1 : this.stream.count();
294295
this.stream.visible = visible;
295-
if (period) this.stream.description = dayjs(period).format('MMMM YYYY');
296+
if (period) this.stream.description = dayjs(period).format(extractText(app.translator.trans('core.lib.datetime_formats.scrubber')));
296297
}
297298

298299
/**

framework/core/locale/core.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,12 @@ core:
655655
kilo_text: K
656656
mega_text: M
657657

658+
# These translations are used for formatting dates using dayjs.
659+
datetime_formats:
660+
humanTimeShort: D MMM
661+
humanTimeLong: ll
662+
scrubber: MMMM YYYY
663+
658664
# These translations are used to punctuate a series of items.
659665
series:
660666
glue_text: ", "

0 commit comments

Comments
 (0)