Skip to content
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

feat: date time formats from locales #4029

Merged
merged 21 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
13 changes: 13 additions & 0 deletions framework/core/js/src/common/Translator.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Dayjs } from 'dayjs';
import { RichMessageFormatter, mithrilRichHandler, NestedStringArray } from '@askvortsov/rich-icu-message-formatter';
import { pluralTypeHandler, selectTypeHandler } from '@ultraq/icu-message-formatter';
import username from './helpers/username';
Expand Down Expand Up @@ -88,4 +89,16 @@ export default class Translator {

return id;
}

/**
* Formats the time.
*
* The format of the time will be chosen by the following order:
* - The format ID defined in current locale.
* - The provided fallback format.
* - DayJS default format.
*/
format(time: Dayjs, id?: string, fallback?: string): string {
YUCLing marked this conversation as resolved.
Show resolved Hide resolved
return time.format(id && (this.translations[id] ?? fallback));
}
YUCLing marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion framework/core/js/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'bootstrap/js/transition';
import 'jquery.hotkeys/jquery.hotkeys';

import relativeTime from 'dayjs/plugin/relativeTime';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import localizedFormat from "dayjs/plugin/localizedFormat";

dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);
Expand Down
11 changes: 5 additions & 6 deletions framework/core/js/src/common/utils/humanTime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import app from '../app';
import dayjs from 'dayjs';

/**
Expand All @@ -15,17 +16,15 @@ export default function humanTime(time: dayjs.ConfigType): string {
d = now;
}

const day = 864e5;
const diff = d.diff(dayjs());
let ago: string;

// If this date was more than a month ago, we'll show the name of the month
// 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');
if (d.diff(now, 'day') < -30) {
if (d.isSame(now, 'year')) {
ago = app.translator.format(d, 'core.lib.datetime_formats.human_time_short');
} else {
ago = d.format('ll');
ago = app.translator.format(d, 'core.lib.datetime_formats.human_time_full');
}
} else {
ago = d.fromNow();
Expand Down
2 changes: 1 addition & 1 deletion framework/core/js/src/common/utils/liveHumanTimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function updateHumanTimes() {
}

/**
* The `liveHumanTimes` initializer sets up a loop every 1 second to update
* The `liveHumanTimes` initializer sets up a loop every 10 seconds to update
* timestamps rendered with the `humanTime` helper.
*/
export default function liveHumanTimes() {
Expand Down
2 changes: 1 addition & 1 deletion framework/core/js/src/forum/components/PostStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,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 = app.translator.format(dayjs(period), 'core.lib.datetime_formats.post_stream_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 @@ -843,6 +843,12 @@ core:
username:
deleted_text: "[deleted]"

# These are DayJS formats used in core.
datetime_formats:
human_time_short: D MMM
human_time_full: ll
post_stream_scrubber: MMMM YYYY

# Translations in this namespace are used in views other than Flarum's normal JS client.
views:
# Translations in this namespace are displayed by the basic HTML admin index.
Expand Down
Loading