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

Localize durations using Intl.NumberFormat #18067

Merged
merged 6 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 21 additions & 4 deletions src/common/datetime/format_duration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import { HaDurationData } from "../../components/ha-duration-input";
import { FrontendLocaleData } from "../../data/translation";
import "../../resources/intl-polyfill";

const leftPad = (num: number) => (num < 10 ? `0${num}` : num);

export const formatDuration = (duration: HaDurationData) => {
export const formatDuration = (
locale: FrontendLocaleData,
duration: HaDurationData
) => {
const d = duration.days || 0;
const h = duration.hours || 0;
const m = duration.minutes || 0;
const s = duration.seconds || 0;
const ms = duration.milliseconds || 0;

if (d > 0) {
return `${d} day${d === 1 ? "" : "s"} ${h}:${leftPad(m)}:${leftPad(s)}`;
return `${Intl.NumberFormat(locale.language, {
style: "unit",
unit: "day",
unitDisplay: "long",
}).format(d)} ${h}:${leftPad(m)}:${leftPad(s)}`;
}
if (h > 0) {
return `${h}:${leftPad(m)}:${leftPad(s)}`;
Expand All @@ -19,10 +28,18 @@ export const formatDuration = (duration: HaDurationData) => {
return `${m}:${leftPad(s)}`;
}
if (s > 0) {
return `${s} second${s === 1 ? "" : "s"}`;
return Intl.NumberFormat(locale.language, {
style: "unit",
unit: "second",
unitDisplay: "long",
}).format(s);
}
if (ms > 0) {
return `${ms} millisecond${ms === 1 ? "" : "s"}`;
return Intl.NumberFormat(locale.language, {
style: "unit",
unit: "millisecond",
unitDisplay: "long",
}).format(ms);
}
return null;
};
17 changes: 11 additions & 6 deletions src/data/automation_i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ const triggerTranslationBaseKey =
const conditionsTranslationBaseKey =
"ui.panel.config.automation.editor.conditions.type";

const describeDuration = (forTime: number | string | ForDict) => {
const describeDuration = (
locale: FrontendLocaleData,
forTime: number | string | ForDict
) => {
let duration: string | null;
if (typeof forTime === "number") {
duration = secondsToDuration(forTime);
} else if (typeof forTime === "string") {
duration = forTime;
} else {
duration = formatDuration(forTime);
duration = formatDuration(locale, forTime);
}
return duration;
};
Expand Down Expand Up @@ -150,7 +153,9 @@ const tryDescribeTrigger = (
)
: undefined;

const duration = trigger.for ? describeDuration(trigger.for) : undefined;
const duration = trigger.for
? describeDuration(hass.locale, trigger.for)
: undefined;

if (trigger.above && trigger.below) {
return hass.localize(
Expand Down Expand Up @@ -322,7 +327,7 @@ const tryDescribeTrigger = (
}

if (trigger.for) {
const duration = describeDuration(trigger.for);
const duration = describeDuration(hass.locale, trigger.for);
if (duration) {
base += ` for ${duration}`;
}
Expand Down Expand Up @@ -573,7 +578,7 @@ const tryDescribeTrigger = (
if (trigger.platform === "template") {
let duration = "";
if (trigger.for) {
duration = describeDuration(trigger.for) ?? "";
duration = describeDuration(hass.locale, trigger.for) ?? "";
}

return hass.localize(
Expand Down Expand Up @@ -827,7 +832,7 @@ const tryDescribeCondition = (
base += ` ${statesString}`;

if (condition.for) {
const duration = describeDuration(condition.for);
const duration = describeDuration(hass.locale, condition.for);
if (duration) {
base += ` for ${duration}`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/data/script_i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const tryDescribeAction = <T extends ActionType>(
duration = hass.localize(
`${actionTranslationBaseKey}.delay.description.duration_string`,
{
string: formatDuration(config.delay),
string: formatDuration(hass.locale, config.delay),
}
);
} else {
Expand Down
9 changes: 4 additions & 5 deletions src/panels/mailbox/ha-panel-mailbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { HomeAssistant } from "../../types";
import { fireEvent } from "../../common/dom/fire_event";
import { haStyle } from "../../resources/styles";
import "../../components/ha-top-app-bar-fixed";
import { formatDuration } from "../../common/datetime/format_duration";

let registeredDialog = false;

Expand Down Expand Up @@ -89,11 +90,9 @@ class HaPanelMailbox extends LitElement {
<div class="row">
<div>${message.caller}</div>
<div class="tip">
${this.hass.localize(
"ui.duration.second",
"count",
message.duration
)}
${formatDuration(this.hass.locale, {
seconds: message.duration,
})}
</div>
</div>
<div secondary>
Expand Down
7 changes: 0 additions & 7 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1457,13 +1457,6 @@
"nameservers": "Name Servers: {nameservers}"
}
},
"duration": {
"second": "{count} {count, plural,\n one {second}\n other {seconds}\n}",
"minute": "{count} {count, plural,\n one {minute}\n other {minutes}\n}",
"hour": "{count} {count, plural,\n one {hour}\n other {hours}\n}",
"day": "{count} {count, plural,\n one {day}\n other {days}\n}",
"week": "{count} {count, plural,\n one {week}\n other {weeks}\n}"
},
"weekdays": {
"monday": "Monday",
"tuesday": "Tuesday",
Expand Down