Skip to content

[1.x] feat: allow to customize time formats through translations #4053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading