Skip to content

Commit

Permalink
chore: pass id to custom callback
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Oct 22, 2024
1 parent 486de88 commit f5dc334
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion framework/core/js/src/admin/components/UserListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export default class UserListPage extends AdminPage {
name: app.translator.trans('core.admin.users.grid.columns.join_time.title'),
content: (user: User) => (
<span className="UserList-joinDate" title={user.joinTime()}>
{dayjs(user.joinTime()).format('LLL')}
{app.translator.formatDateTime(dayjs(user.joinTime()), 'core.lib.datetime_formats.fullTimeShort')}
</span>
),
},
Expand Down
13 changes: 7 additions & 6 deletions framework/core/js/src/common/Translator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ItemList from './utils/ItemList';

type Translations = Record<string, string>;
type TranslatorParameters = Record<string, unknown>;
type DateTimeFormatCallback = (format?: string) => string | void;
type DateTimeFormatCallback = (id?: string) => string | void;

export default class Translator {
/**
Expand Down Expand Up @@ -105,13 +105,14 @@ export default class Translator {
* - The format defined in current locale.
* - DayJS default format.
*/
formatDateTime(time: Dayjs, id?: string): string {
const format = id && (this.translations[id] ?? id);
const formatCallback = id && this.dateTimeFormats.has(id) && this.dateTimeFormats.get(id);
formatDateTime(time: Dayjs, id: string): string {
const formatCallback = this.dateTimeFormats.has(id) && this.dateTimeFormats.get(id);

if (formatCallback) {
const result = formatCallback.apply(this, [format]);
const result = formatCallback.apply(this, [id]);
if (result) return result;
}
return time.format(format);

return time.format(this.translations[id]);
}
}
2 changes: 1 addition & 1 deletion framework/core/js/src/common/helpers/fullTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function fullTime(time: Date): Mithril.Vnode {
const d = dayjs(time);

const datetime = d.format();
const full = app.translator.formatDateTime(d, 'core.lib.datetime_formats.full_time');
const full = app.translator.formatDateTime(d, 'core.lib.datetime_formats.fullTime');

return (
<time pubdate datetime={datetime}>
Expand Down
2 changes: 1 addition & 1 deletion framework/core/js/src/common/helpers/humanTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function humanTime(time: Date): Mithril.Vnode {
const d = dayjs(time);

const datetime = d.format();
const full = app.translator.formatDateTime(d, 'core.lib.datetime_formats.full_time');
const full = app.translator.formatDateTime(d, 'core.lib.datetime_formats.fullTime');
const ago = humanTimeUtil(time);

return (
Expand Down
4 changes: 2 additions & 2 deletions framework/core/js/src/common/utils/humanTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,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 (d.diff(now, 'day') < -30) {
if (d.isSame(now, 'year')) {
ago = app.translator.formatDateTime(d, 'core.lib.datetime_formats.human_time_short');
ago = app.translator.formatDateTime(d, 'core.lib.datetime_formats.humanTimeShort');
} else {
ago = app.translator.formatDateTime(d, 'core.lib.datetime_formats.human_time_full');
ago = app.translator.formatDateTime(d, 'core.lib.datetime_formats.humanTimeFull');
}
} else {
ago = d.fromNow();
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 = app.translator.formatDateTime(dayjs(period), 'core.lib.datetime_formats.post_stream_scrubber');
if (period) this.stream.description = app.translator.formatDateTime(dayjs(period), 'core.lib.datetime_formats.scrubber');
}

/**
Expand Down
9 changes: 5 additions & 4 deletions framework/core/locale/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,11 @@ core:

# These are DayJS formats used in core.
datetime_formats:
full_time: LLLL
human_time_short: D MMM
human_time_full: ll
post_stream_scrubber: MMMM YYYY
fullTime: LLLL
fullTimeShort: LLL
humanTimeShort: D MMM
humanTimeLong: ll
scrubber: MMMM YYYY

# Translations in this namespace are used in views other than Flarum's normal JS client.
views:
Expand Down

0 comments on commit f5dc334

Please sign in to comment.