-
Notifications
You must be signed in to change notification settings - Fork 158
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 support for Temporal proposal #129
Comments
Here are partial notes from a ~2hr brainstorming session with @pipobscure, myself (@sffc), @gibson042, @fabalbon, and @littledan in New York after TC39. The rest of this message contains a digest of our conclusions. First, the following Temporal types are the ones that make sense to be formatted.
We considered adding separate Intl objects for each type: Intl.CivilDateFormat, Intl.CivilTimeFormat, etc. We preferred not to go down this route because:
The next question was whether to overload Intl.DateTimeFormat.prototype.format or to add new methods like Intl.DateTimeFormat.prototype.formatCivilDate. Given that TC39 opted to prefer overloading of Intl.NumberFormat.prototype.format for BigInt, it makes sense to follow that precedent and perform overloading of Intl.DateTimeFormat.prototype.format for Temporal types. The behavior in the default case makes sense. For example: let cd = new CivilDate(2018, 5, 3);
let dtf = new Intl.DateTimeFormat("bn");
console.log(dtf.format(cd)); // ৩/৫/২০১৮ The tricky part is when you pass settings to Intl.DateTimeFormat that don't make sense for the given Temporal type. For example, what should happen when you run the following code? let cd = new CivilDate(2018, 5, 3);
let dtf = new Intl.DateTimeFormat("fr", {
hour: "2-digit"
});
console.log(dtf.format(cd)); We considered four options:
No one really liked options 1-3 that much, but it took a while to come up with a sensible condition on which to throw the exception. To explain that condition, let me explain how we go about implementing the common case in the spec. Intl.DateTimeFormat currently has an internal slot named
Here's the key part: the way each of those patterns get populated is by taking the subset of skeleton keys (input keys) that are needed for each Civil type, as shown below.
If the subset is empty, then set the corresponding internal slot to null. For example, if the user gives the option bag Additional note: for formatRange, @fabalbon said that it should be possible to merge the changes required to Temporal with that proposal with somewhat minimal effort. |
@littledan said:
I think I like
(edited with additional line items) |
To give some more background, Chrome believes that built-in modules should be truly modularized: they should not have cross-cutting dependencies or interactions with built-in globals, or other modules. This allows the code for implementing a built-in module to not interact with any of the code for those other features, and e.g. then be lazy-loaded only into realms that need it. So, if types in the
I prefer the former; I think clean modularization boundaries is a big improvement for ECMAScript standard library features. On your specific questions:
|
I agee with @domenic there is no reason std:temporal cannot deliver a district TemporalFormat that delivers this functionality. So long as it keeps the same API as DateFormat that could work well, be clean & consistent. I’ll be updating the spec soonish. Definitely in time for it to have been discussed online by June. |
It's really great to have all of this written up, @sffc. Whether we do this within With respect to the points in #129 (comment), what sorts of patterns should we have in built-in modules going forward in cases like this? Is this a good chance to set a new default, or is the overriding concern ergonomics and consistency with existing functionality? cc @domenic @tschneidereit @annevk @Ms2ger @msaboff @mattijs |
Thanks for the explanations.
I don't see how this prevents the act of importing |
Changing the behavior of a global object upon importing a module is the kind of anti-modular side effects we would like to avoid. Also note that dynamic import is async and format() is not. |
Let's say that in a world where Temporal is a built-in module, then the i18n support for Temporal should also be in its own built-in module. That seems reasonable. Moving in this direction with Temporal i18n would set strong precedent for what Intl would look like in the future. Intl-as-a-module is a very interesting and important design space. I would be a bit uneasy moving forward in this direction without investing the time to design a proper module system for Intl as a whole, or else we may set precedent that we may regret later. To be clear, though, as I have stated before, I do not want to see Temporal reach Stage 3 until we have an i18n solution for it. Not only does it send the wrong message if we don't make i18n a first-class citizen, but the ecosystem will induce technical debt in the form of workarounds to add that functionality, potentially in ways that we don't want them to do. An expedient option would be to introduce Temporal as a global object as originally proposed. I understand that Temporal could be an opportunity to move the needle on built-in modules. However, I am not convinced that this proposal is the right way to move the needle. I could easily see an effort in the not-so-distant future in which TC39 comes together and makes a runtime-wide effort to modularize the globals in JavaScript: not just Temporal but also Intl, Map/Set, and so forth. We could add syntax along the same vein as ES6 In the end, assuming we see a future for ourselves in which we convert existing globals to built-in modules, I do not see any major downsides of adding Temporal today as a new global in ECMAScript. It could potentially be the last one we add. |
The get-originals proposal provides a path towards modularizing globals; however, it looks a bit different from some of the drafts for new built-in modules, like kv-storage in the sense that it uses the |
The trick there is getting consensus on what constitutes “improvement”, and often that will mean ensuring consistency. |
@littledan @sffc I guess this can be closed now that we've explored all avenues of interaction with 402 (including more specific issues and the ongoing work on |
We would like to add support for Temporal toLocaleString as well as formatting of relevant Temporal objects in Intl.DateTimeFormat before Temporal reaches Stage 3. Use this thread to track these discussions.
I will shortly post notes from a meeting we had last week where we came up with some initial ideas on how to design this API.
The text was updated successfully, but these errors were encountered: