-
Notifications
You must be signed in to change notification settings - Fork 106
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
Intl.Locale #106
Comments
WIP of the proposal: https://github.com/zbraniecki/proposal-intl-locale |
Backpointer to a more descriptive issue: #105 |
I'm not sure of the usefulness of this. Is the goal to provide a factory-kind of structure to just build the locale string with certain options? or just a way to understand the keys in a locale? How will users use this in conjunction with other |
The idea would be that Other Later, we could become smarter and not The goal is to make operations on extension keys vs. intl options more interchangeable. We don't have to do this, but I think @littledan's solution is a very elegant way to make any operations between Intl options and extension keys unified. |
Still I don't get it! what is the gain here? Definitely not performance IMO. If |
The gain here is that you can transition between options and extension keys. Without it, taking Modifying it to just alter, say h12 to h24, is also hard. And lastly, building the string out of bits that you have (you know that hourCycle is h12, you know that calendar is buddhist, now build the locale string) is hard as well. With let x = new Intl.DateTimeFormat('de', {
calendar: 'buddhist',
hourCycle: 'h12'
});
let loc = new Intl.Locale('de', x.resolvedOptions());
let y = new Intl.DateTimeFormat(loc, {
hourCycle: 'h24'
}); or let loc = new Intl.Locale('de-hc-h12', {
calendar: 'buddhist'
});
let loc2 = new Intl.Locale(loc, {
calendar: 'gregory'
});
if (loc2.hourCycle === 'h12') {
} else {
} imagine doing the same without |
let locale = new Intl.Locale('en-US');
locale.toString(); // 'en-US'
locale.calendar; // undefined This example is a bit confusing to me. Why wouldn't it tell you the default calendar system for en-US? I am guessing you are trying to separate out "passed options" from "actual options" (maybe "actual options" = |
An alternative to creating a class like this is to create a library of string manipulation methods to parse, normalize and construct BCP 47 extended locales. The API presented here accomplishes the same.The difference between the two options is largely ergonomic. Intuitively, to me, a class feels "more JavaScripty", though this is a value judgement. |
I understand the confusion because I fell for it multiple times myself. Basically, when we deal with locales we may end up with thinking about
The former is what we're suggesting right now. As @littledan said, we could replace it with The latter, is also something that is popping up every now and then in conversations and it's - how to retrieve "negotiated" values for a locale? What is the default calendar for In the latter scenario, you expect to do There's also a third level in that we currently only recognize values from the locale string, options or locale defaults, but we're also talking about optionally retrieving host environment preferences where possible before we reach for locale defaults. I would argue that we should not build APIs that expose user ability to select where they want the data to come from. I believe my suggested order would be:
If we always return 'calendar' field on let lang = 'pl';
let loc = new Intl.Locale(lang);
loc.calendar; // 'gregory'
loc.toString(); // 'pl-u-ca-gregory'
loc.toString() !== lang; One option, would be to do: let lang = 'pl';
let loc = new Intl.Locale(lang);
loc.calendar; // undefined
loc.resolvedOptions().calendar; // 'gregory'
loc.toString(); // 'pl'
loc.toString() === lang; The only cost of this is that it would have to resolve all options for all formatters when you ask only for one. We discussed doing this as a set of APIs: If we ended up adding let loc = new Intl.Locale('pl');
loc.calendar; // undefined
loc.resolvedDateTimeOptions(); {calendar: 'gregory', ...}
loc.resolvedNumberOptions(); {numberingSystem: 'latn'} to prevent having to collect all options from all formatters. Does it make sense @domenic ? |
This makes sense to me. Mark Davis suggested that ECMA 402 should have a library for BCP 47 manipulation, and it seems like this amounts to that plus a bit more. Any more thoughts, @jungshik? |
@zbraniecki: thanks for explaining; it makes sense now. I guess I don't have a preference between the class and the functions except if there are useful methods than of course a class is nice. Maybe your I don't understand why it would be bad to resolve all options, and introducing new APIs to avoid that seems unnecessary to me, but I don't know this problem space very well so I'll defer to your judgment there. |
The way implementations will have to handle this is to pull those bits from CLDR separately or using some intermediary API like ICU. It starts fairly small - let's say that the I understand that adding methods is not the funniest way to solve the problem, but it's one of them... let loc = new Intl.Locale('pl');
loc.resolvedOptions({type: 'datetime'}); // {calendar: ..., numberingSystem: ..., timeZone: ...} or a list of options user wants to retrieve: let loc = new Intl.Locale('pl');
loc.resolvedOptions(['calendar', 'measuringSystem', 'timeZone']); // {calendar: ..., measuringSystem: ..., timeZone: ...} |
You would already be able to get at the resolved options of the locale with respect to e.g. |
|
|
PR for Stage 4 reached on February 5th 2020. |
#406 was merged, so this issue can be closed. |
Yep, thanks @zbraniecki and @anba! |
This is the second spin-off from #68.
Proposing a new class
Intl.Locale
which creates objects with a list of extension keys used by any of the formatters and withtoString
method which produces a BCP47 locale string with relevant extension keys.Examples:
Proposed list of supported extension keys and their option key names:
ca
--calendar
nu
--numberingSystem
hc
--hourCycle
tz
--timeZone
cu
--currency
co
--collation
kn
--numeric
kf
--caseFirst
Looking for feedback
The text was updated successfully, but these errors were encountered: