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

Conversation

silamon
Copy link
Contributor

@silamon silamon commented Sep 29, 2023

Proposed change

Localize durations using Intl.NumberFormat
It's not entirely clear why some statements are displayed as "04:05:06" instead of 4 hours, 5 minutes and 6 seconds.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

Comment on lines 17 to 20
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?

bramkragten
bramkragten previously approved these changes Oct 5, 2023
@bramkragten
Copy link
Member

We can btw also use something like this:

Intl.NumberFormat("en", {
    style: "unit",
    unit: "second",
    unitDisplay: "long",
  }).format(23)

->
23 seconds

@bramkragten
Copy link
Member

bramkragten commented Oct 5, 2023

Or even in the localization if we want to (we dont 😄 ):

{seconds, number, :: unit-width-full-name unit/second}

@silamon silamon changed the title Localize format_duration function Localize durations using Intl.NumberFormat Oct 6, 2023
hass.localize("ui.duration.day", { count: d }) +
` ${h}:${leftPad(m)}:${leftPad(s)}`
);
return `${Intl.NumberFormat("en", {
Copy link
Member

Choose a reason for hiding this comment

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

Can we pass the locale of the user?

Copy link
Member

Choose a reason for hiding this comment

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

And move it to format date/format time

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you may have reviewed the wrong commit. User locale is already passed in.

@@ -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 "../../resources/intl-polyfill";
Copy link
Member

Choose a reason for hiding this comment

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

To prevent having to manage this everywhere, we should just move the format function to a file that imports this.

@bramkragten bramkragten merged commit eae3c13 into home-assistant:dev Oct 9, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Oct 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing in Lokalise: automations related strings
3 participants