Skip to content

Commit

Permalink
Allow to customize time formats
Browse files Browse the repository at this point in the history
  • Loading branch information
rob006 committed Oct 1, 2024
1 parent 256c184 commit 97eab2b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions framework/core/js/src/common/utils/humanTime.ts
Original file line number Diff line number Diff line change
@@ -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-
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion framework/core/js/src/forum/components/PostStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions framework/core/locale/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,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: ", "
Expand Down

0 comments on commit 97eab2b

Please sign in to comment.