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 1 commit
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
15 changes: 11 additions & 4 deletions src/common/datetime/format_duration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { HaDurationData } from "../../components/ha-duration-input";
import { HomeAssistant } from "../../types";

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

export const formatDuration = (duration: HaDurationData) => {
export const formatDuration = (
hass: HomeAssistant,
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 `
${hass.localize("ui.duration.day", { count: d })}
${h}:${leftPad(m)}:${leftPad(s)}
`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return `
${hass.localize("ui.duration.day", { count: d })}
${h}:${leftPad(m)}:${leftPad(s)}
`;
return hass.localize("ui.duration.day", { count: d }) + `${h}:${leftPad(m)}:${leftPad(s)}`;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally prefer template strings above concatenation, it is easier to read and causes less mistakes (like you forgot the space between day and hours there)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but the original has a load of whitespace, what if it gets passed to somewhere that doesn't do whitespace collapsing?

}
if (h > 0) {
return `${h}:${leftPad(m)}:${leftPad(s)}`;
Expand All @@ -19,10 +26,10 @@ export const formatDuration = (duration: HaDurationData) => {
return `${m}:${leftPad(s)}`;
}
if (s > 0) {
return `${s} second${s === 1 ? "" : "s"}`;
return `${hass.localize("ui.duration.second", { count: s })}`;
}
if (ms > 0) {
return `${ms} millisecond${ms === 1 ? "" : "s"}`;
return `${hass.localize("ui.duration.millisecond", { count: 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 = (
hass: HomeAssistant,
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(hass, 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, 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, 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, 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, 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, config.delay),
}
);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,7 @@
}
},
"duration": {
"millisecond": "{count} {count, plural,\n one {millisecond}\n other {milliseconds}\n}",
"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}",
Expand Down