Skip to content

Commit

Permalink
refactor: use locale for all time formats
Browse files Browse the repository at this point in the history
  • Loading branch information
YUCLing committed Oct 2, 2024
1 parent 6782636 commit 8db78fe
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 90 deletions.
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 {
return time.format(id && (this.translations[id] ?? fallback));
}
}
5 changes: 2 additions & 3 deletions framework/core/js/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import 'bootstrap/js/transition';
import 'jquery.hotkeys/jquery.hotkeys';

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

dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);

import './registry';

import { customFormats } from './utils/localizedFormat';
dayjs.extend(customFormats);

import patchMithril from './utils/patchMithril';

patchMithril(window);
Expand Down
5 changes: 3 additions & 2 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 @@ -21,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 = d.format('f');
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
84 changes: 0 additions & 84 deletions framework/core/js/src/common/utils/localizedFormat.ts

This file was deleted.

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('FF');
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

0 comments on commit 8db78fe

Please sign in to comment.