Skip to content

Commit e74d0f4

Browse files
joyeecheungV8 LUCI CQ
authored andcommitted
[api] add v8::Isolate::GetDefaultLocale()
This allows embedders to query the default locale used by Intl APIs. This information is already available to user land via Intl?.Collator().resolvedOptions().locale, the issue with this is that it's a lot slower and requires dynamic access to Intl API, which is subject to patching and prototype pollution so it's not as reliable for embedders than having a V8 API to query the default locale directly. Refs: nodejs/node#54279 Change-Id: I5a1823993c9ae79f8f61f54c6464daf882a09ba3 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5772938 Reviewed-by: Leszek Swirski <[email protected]> Commit-Queue: Joyee Cheung <[email protected]> Cr-Commit-Position: refs/heads/main@{#95678}
1 parent 9e9e1ee commit e74d0f4

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

include/v8-isolate.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdint.h>
1010

1111
#include <memory>
12+
#include <string>
1213
#include <utility>
1314

1415
#include "cppgc/common.h"
@@ -1723,6 +1724,12 @@ class V8_EXPORT Isolate {
17231724
*/
17241725
void LocaleConfigurationChangeNotification();
17251726

1727+
/**
1728+
* Returns the default locale in a string if Intl support is enabled.
1729+
* Otherwise returns an empty string.
1730+
*/
1731+
std::string GetDefaultLocale();
1732+
17261733
Isolate() = delete;
17271734
~Isolate() = delete;
17281735
Isolate(const Isolate&) = delete;

src/api/api.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10774,6 +10774,17 @@ void v8::Isolate::LocaleConfigurationChangeNotification() {
1077410774
#endif // V8_INTL_SUPPORT
1077510775
}
1077610776

10777+
std::string Isolate::GetDefaultLocale() {
10778+
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
10779+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
10780+
10781+
#ifdef V8_INTL_SUPPORT
10782+
return i_isolate->DefaultLocale();
10783+
#else
10784+
return std::string();
10785+
#endif
10786+
}
10787+
1077710788
#if defined(V8_OS_WIN) && defined(V8_ENABLE_ETW_STACK_WALKING)
1077810789
void Isolate::SetFilterETWSessionByURLCallback(
1077910790
FilterETWSessionByURLCallback callback) {

0 commit comments

Comments
 (0)