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

Add unit preferences and "smart" unit resolution #277

Open
sffc opened this issue Oct 5, 2018 · 7 comments
Open

Add unit preferences and "smart" unit resolution #277

sffc opened this issue Oct 5, 2018 · 7 comments
Assignees
Labels
c: numbers Component: numbers, currency, units Proposal Larger change requiring a proposal s: in progress Status: the issue has an active proposal

Comments

@sffc
Copy link
Contributor

sffc commented Oct 5, 2018

#215 adds the ability to format unit strings. It does not attempt to solve the problem of picking the correct unit to use given a locale, context, and magnitude.

CLDR has some limited data for this: https://unicode.org/cldr/trac/browser/tags/release-33-1-d04/common/supplemental/supplementalData.xml#L4770

I have issues filed to improve this data structure. For example: https://unicode.org/cldr/trac/ticket/11452

Ultimately, what I would like to eventually see is one function that maps from context, locale, and magnitude to unit identifier, and then let #215 map from unit identifier, locale, and number to string.

Additional question: should this new hypothetical API also include unit conversion?

@rxaviers
Copy link
Member

rxaviers commented Oct 5, 2018

One argument against it is the analogous situation for currency. Trying to guess the currency based on locale is considered a bad pattern according to UTS#35: "Note: Currency values should never be interchanged without a known currency code. You never want the number 3.5 interpreted as $3.50 by one user and €3.50 by another. Locale data contains localization information for currencies, not a currency value for a country. A currency amount logically consists of a numeric value, plus an accompanying currency code (or equivalent). The currency code may be implicit in a protocol, such as where USD is implicit. But if the raw numeric value is transmitted without any context, then it has no definitive interpretation."

Following that line of thought, I suggest exposing the preferred units given a locale, context, and magnitude instead. Leave to the user the decision on which to use and any needed conversions.

@sffc
Copy link
Contributor Author

sffc commented Oct 5, 2018

That quotation sounds like it's about parsing an unknown currency. For measurement units, what I was thinking by "conversion" was basically, ok, I have a road length in meters from my database, and I want to show it to a user in en-GB; convert from meters to the locale's unit and then display.

@rxaviers
Copy link
Member

rxaviers commented Oct 5, 2018

The quotation refers to a bad function formatCurrency(3.5) that would display '$3.50' for one locale and '€3.50' for another.

I brought this analogy considering your suggestion is about a formatUnit(3.5) that would do something similar. If it's about a formatUnit(100, 'meter', {style: "smart"}) that would pick the localized unit and do any necessary conversions, then I think it would be fine.

@sffc
Copy link
Contributor Author

sffc commented Oct 5, 2018

Ah, I see what you mean now.

An all-in-one API could be as simple as,

new Intl.NumberFormat("en-us", {
    style: "smart-unit",
    unitContext: "length-road",
});

For measurement units, we could declare a standard unit that the number is to be provided in. For the "length" type, I would pick meters, for example. If we want to allow users to change it, though, that would be relatively easy:

new Intl.NumberFormat("en-us", {
    style: "smart-unit",
    unitContext: "length-road",
    inputUnit: "length-mile"
});

I would also like to see a standalone library that lets you get the "smart" unit without formatting to a string. Maybe that could be something for the standard library, https://github.com/tc39/proposal-javascript-standard-library.

@littledan
Copy link
Member

I can definitely see the case for this standalone library. What's harder for me to understand is why you'd want this to be integrated into Intl.NumberFormat--it seems like saving a little bit of typing for some broader confusion. Wouldn't you typically need to go and get the unit back out with resolvedOptions(), negating much of the length benefit?

@sffc sffc added c: numbers Component: numbers, currency, units s: comment Status: more info is needed to move forward labels Mar 19, 2019
@sffc
Copy link
Contributor Author

sffc commented Apr 19, 2019

ICU ticket with some ideas:

https://unicode-org.atlassian.net/browse/ICU-20568

@sffc sffc added Proposal Larger change requiring a proposal s: in progress Status: the issue has an active proposal and removed s: comment Status: more info is needed to move forward labels Jun 5, 2020
@younies
Copy link
Member

younies commented Jun 9, 2020

Here is the proposal for supporting smart unit preferences:
https://github.com/tc39/proposal-smart-unit-preferences

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: numbers Component: numbers, currency, units Proposal Larger change requiring a proposal s: in progress Status: the issue has an active proposal
Projects
Archived in project
Development

No branches or pull requests

4 participants