-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
lib: set default ICU locale per cli option --icu-locale
#50538
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
base: main
Are you sure you want to change the base?
Conversation
|
Review requested:
|
--locale--locale
--locale--locale or LOCALE env var
a5b21c6 to
ad61226
Compare
tniessen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand the motivation here, as in, providing a portable way of defining the default ICU locale.
However, ICU already does a good job at determining the correct locale based on the environment, and changing ICU's locale may introduce inconsistencies within Node.js applications. It won't affect the locale used by the C and C++ standard libraries, it won't affect other dependencies of Node.js that don't use ICU, it won't correctly propagate to native addons, it won't correctly propagate to child processes, etc.
See also #28099 (comment). cc @bnoordhuis
|
Another thing: this should use NODE_OPTIONS instead of inventing a new environment variable. |
|
I will have a look later regarding implementing it as NODE_OPTIONS. |
ad61226 to
423f20b
Compare
--locale or LOCALE env var--locale
|
I hope this is to your liking? |
423f20b to
49423d0
Compare
|
@bnoordhuis PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code changes themselves LGTM. I don't know if it's a good change for the reasons Tobias points out.
tniessen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like Ben, I am also not sure if this is a good change.
In any case, if this doesn't properly change the locale for the entire process tree, it should probably be called --icu-locale. See also #50538 (review) and #28099 (comment).
49423d0 to
9aea65e
Compare
--locale--icu-locale
|
@tniessen |
|
Anybody knows how I could get the value of Intl?.Collator().resolvedOptions().locale without a roundtrip to js and back? |
|
It's |
|
Do you have a code example, please? It doesnt seem trivial to use it. |
70a8143 to
7863b37
Compare
|
What about now? |
7863b37 to
fe4f31b
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #50538 +/- ##
=======================================
Coverage 87.09% 87.09%
=======================================
Files 648 648
Lines 182213 182238 +25
Branches 34961 34968 +7
=======================================
+ Hits 158703 158729 +26
+ Misses 16794 16784 -10
- Partials 6716 6725 +9
|
| representing the language version as defined in [RFC 5646][] (also known as | ||
| BCP 47). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| representing the language version as defined in [RFC 5646][] (also known as | |
| BCP 47). | |
| representing the language version as defined in [IETF BCP 47][]. |
don't refer to 5646, that's the reason there's a BCP 47.
| [OSSL_PROVIDER-legacy]: https://www.openssl.org/docs/man3.0/man7/OSSL_PROVIDER-legacy.html | ||
| [Permission Model]: permissions.md#permission-model | ||
| [REPL]: repl.md | ||
| [RFC 5646]: https://www.rfc-editor.org/rfc/rfc5646.txt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| [RFC 5646]: https://www.rfc-editor.org/rfc/rfc5646.txt | |
| [BCP 47]: https://www.rfc-editor.org/info/bcp47 |
| const char* newDefaultLocale = uloc_getDefault(); | ||
| int newDefaultLocaleLen = strlen(newDefaultLocale); | ||
| int32_t locCount = 0; | ||
| const icu::Locale* supportedLocales = | ||
| icu::Locale::getAvailableLocales(locCount); | ||
| for (int32_t i = 0; i < locCount; ++i) { | ||
| if (strncmp(newDefaultLocale, | ||
| supportedLocales[i].getName(), | ||
| newDefaultLocaleLen) == 0) { | ||
| return; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure of the purpose of this. Just let the user set the default locale to whatever they want. The user's own system might be set to something not available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can set the --icu-locale to invalid values and as long as it is in the right format, it will set it. It will only go to und if it is an invalid locale.This ensures, that the locale is existing and valid.
Tbh. I didnt know how I could get the BestMatcher Class running. So I wrote this logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the purpose of trying to protect the user from invalid values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment by @bnoordhuis
| '-p', | ||
| 'new Date(Date.UTC(2012, 11, 20, 3, 0, 0)).toLocaleString(undefined, { timeZone: "UTC" })', | ||
| ]); | ||
| assert.strictEqual(child.stdout.toString(), '2012. 12. 20. 오전 3:00:00\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like these kinds of tests. At all. You are hard coding linguistic data that is guaranteed to change with every release, and even with local modifications the user makes.
I would recommend NOT testing for unsupported locales (you can't predict which locales are included), and not testing for specific formats. We've discussed this elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took these examples from MDN to ensure it works as expected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't expect it. The data changes every single time. See https://www.unicode.org/cldr/charts/latest/delta/index.html and click on any locale.
I think a better test would simply be to verify that the output is DIFFERENT when a different locale is chosen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right. that is also a good approach.
| '-p', | ||
| 'new Intl.ListFormat(undefined, {style: "long", type:"conjunction"}).format(["a", "b", "c"])', | ||
| ]); | ||
| assert.strictEqual(child.stdout.toString(), 'a, b und c\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps just keep ONE of these tests, to see that setting the locale makes a difference. Knowing that test might need updates. Delete the rest of the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think two are needed each?! On my machine the locale is de-DE so this test would pass always on my machine, even if the locale flag would not work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe two. you can also set LC_ALL on a unix type env to show the distinction betwen the env and the parameter
$ env LC_ALL=de node -p 'new Date().toLocaleString()'
6.9.2024, 12:14:35
$ env LC_ALL=en node -p 'new Date().toLocaleString()'
9/6/2024, 12:14:39 PMThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then i could not test it on windows?
|
@srl295 |
This PR adds a standardized way via the cli option
--icu-localeto set the default locale of the ICU used in node process.How to use:
then in the repl you can do
Todo: