mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-16 09:49:49 +00:00
Enhancement: locale option for date & relativeDate format (#2658)
This commit is contained in:
parent
641eb25047
commit
7837864841
4 changed files with 39 additions and 2 deletions
|
@ -100,6 +100,17 @@ function uptime(uptimeInSeconds, i18next) {
|
|||
return (moDisplay + dDisplay + hDisplay + mDisplay + sDisplay).replace(/,\s*$/, "");
|
||||
}
|
||||
|
||||
function relativeDate(date, formatter) {
|
||||
const cutoffs = [60, 3600, 86400, 86400 * 7, 86400 * 30, 86400 * 365, Infinity];
|
||||
const units = ["second", "minute", "hour", "day", "week", "month", "year"];
|
||||
|
||||
const delta = Math.round((date.getTime() - Date.now()) / 1000);
|
||||
const unitIndex = cutoffs.findIndex((cutoff) => cutoff > Math.abs(delta));
|
||||
const divisor = unitIndex ? cutoffs[unitIndex - 1] : 1;
|
||||
|
||||
return formatter.format(Math.floor(delta / divisor), units[unitIndex]);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
i18n: {
|
||||
defaultLocale: "en",
|
||||
|
@ -142,6 +153,9 @@ module.exports = {
|
|||
i18next.services.formatter.add("date", (value, lng, options) =>
|
||||
new Intl.DateTimeFormat(lng, { ...options }).format(new Date(value)),
|
||||
);
|
||||
i18next.services.formatter.add("relativeDate", (value, lng, options) =>
|
||||
relativeDate(new Date(value), new Intl.RelativeTimeFormat(lng, { ...options })),
|
||||
);
|
||||
i18next.services.formatter.add("uptime", (value, lng) => uptime(value, i18next));
|
||||
},
|
||||
type: "3rdParty",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue