Skip to content

Commit

Permalink
add: support for custom time formats
Browse files Browse the repository at this point in the history
  • Loading branch information
YUCLing committed Oct 2, 2024
1 parent 8db78fe commit 7f336dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
14 changes: 13 additions & 1 deletion framework/core/js/src/common/Translator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import username from './helpers/username';
import User from './models/User';
import extract from './utils/extract';
import extractText from './utils/extractText';
import ItemList from './utils/ItemList';

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

export default class Translator {
/**
* A map of translation keys to their translated values.
*/
translations: Translations = {};

/**
* A item list of date time format callbacks.
*/
dateTimeFormats: ItemList<DateTimeFormatCallback> = new ItemList();

/**
* The underlying ICU MessageFormatter util.
*/
Expand Down Expand Up @@ -98,7 +105,12 @@ export default class Translator {
* - The provided fallback format.
* - DayJS default format.
*/
format(time: Dayjs, id?: string, fallback?: string): string {
formatDateTime(time: Dayjs, id?: string, fallback?: string): string {
const formatCallback = (id && this.dateTimeFormats.has(id)) && this.dateTimeFormats.get(id);
if (formatCallback) {
const result = formatCallback(fallback);
if (result) return result;
}
return time.format(id && (this.translations[id] ?? fallback));
}
}
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.format(d, 'core.lib.datetime_formats.human_time_short');
ago = app.translator.formatDateTime(d, 'core.lib.datetime_formats.human_time_short');
} else {
ago = app.translator.format(d, 'core.lib.datetime_formats.human_time_full');
ago = app.translator.formatDateTime(d, 'core.lib.datetime_formats.human_time_full');
}
} 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.format(dayjs(period), 'core.lib.datetime_formats.post_stream_scrubber');
if (period) this.stream.description = app.translator.formatDateTime(dayjs(period), 'core.lib.datetime_formats.post_stream_scrubber');
}

/**
Expand Down

0 comments on commit 7f336dc

Please sign in to comment.