Commit 0b42d61d authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

[Intl] Move AvailableLocalesOf, DefaultLocale to Intl

Bug: v8:5751
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I0661d6503b66e71de56bdc37e22ef45ef77c0806
Reviewed-on: https://chromium-review.googlesource.com/1141351
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54666}
parent d2abbc13
......@@ -1163,6 +1163,66 @@ std::set<std::string> Intl::GetAvailableLocales(const IcuService& service) {
return locales;
}
namespace {
IcuService StringToIcuService(Handle<String> service) {
if (service->IsUtf8EqualTo(CStrVector("collator"))) {
return IcuService::kCollator;
} else if (service->IsUtf8EqualTo(CStrVector("numberformat"))) {
return IcuService::kNumberFormat;
} else if (service->IsUtf8EqualTo(CStrVector("dateformat"))) {
return IcuService::kDateFormat;
} else if (service->IsUtf8EqualTo(CStrVector("breakiterator"))) {
return IcuService::kBreakIterator;
} else if (service->IsUtf8EqualTo(CStrVector("pluralrules"))) {
return IcuService::kPluralRules;
} else if (service->IsUtf8EqualTo(CStrVector("relativetimeformat"))) {
return IcuService::kRelativeDateTimeFormatter;
}
UNREACHABLE();
}
} // namespace
V8_WARN_UNUSED_RESULT MaybeHandle<JSObject> Intl::AvailableLocalesOf(
Isolate* isolate, Handle<String> service) {
Factory* factory = isolate->factory();
std::set<std::string> results =
Intl::GetAvailableLocales(StringToIcuService(service));
Handle<JSObject> locales = factory->NewJSObject(isolate->object_function());
int32_t i = 0;
for (auto iter = results.begin(); iter != results.end(); ++iter) {
RETURN_ON_EXCEPTION(
isolate,
JSObject::SetOwnPropertyIgnoreAttributes(
locales, factory->NewStringFromAsciiChecked(iter->c_str()),
factory->NewNumber(i++), NONE),
JSObject);
}
return locales;
}
V8_WARN_UNUSED_RESULT Handle<String> Intl::DefaultLocale(Isolate* isolate) {
Factory* factory = isolate->factory();
icu::Locale default_locale;
// Translate ICU's fallback locale to a well-known locale.
if (strcmp(default_locale.getName(), "en_US_POSIX") == 0) {
return factory->NewStringFromStaticChars("en-US");
}
// Set the locale
char result[ULOC_FULLNAME_CAPACITY];
UErrorCode status = U_ZERO_ERROR;
uloc_toLanguageTag(default_locale.getName(), result, ULOC_FULLNAME_CAPACITY,
FALSE, &status);
if (U_SUCCESS(status)) {
return factory->NewStringFromAsciiChecked(result);
}
return factory->NewStringFromStaticChars("und");
}
bool Intl::IsObjectOfType(Isolate* isolate, Handle<Object> input,
Intl::Type expected_type) {
if (!input->IsJSObject()) return false;
......
......@@ -227,6 +227,11 @@ class Intl {
// pa_IN.
static std::set<std::string> GetAvailableLocales(const IcuService& service);
static V8_WARN_UNUSED_RESULT MaybeHandle<JSObject> AvailableLocalesOf(
Isolate* isolate, Handle<String> service);
static V8_WARN_UNUSED_RESULT Handle<String> DefaultLocale(Isolate* isolate);
// If locale has a script tag then return true and the locale without the
// script else return false and an empty string
static bool RemoveLocaleScriptTag(const std::string& icu_locale,
......
......@@ -97,85 +97,19 @@ RUNTIME_FUNCTION(Runtime_CanonicalizeLanguageTag) {
RUNTIME_FUNCTION(Runtime_AvailableLocalesOf) {
HandleScope scope(isolate);
Factory* factory = isolate->factory();
DCHECK_EQ(1, args.length());
CONVERT_ARG_HANDLE_CHECKED(String, service, 0);
const icu::Locale* available_locales = nullptr;
int32_t count = 0;
if (service->IsUtf8EqualTo(CStrVector("collator"))) {
available_locales = icu::Collator::getAvailableLocales(count);
} else if (service->IsUtf8EqualTo(CStrVector("numberformat"))) {
available_locales = icu::NumberFormat::getAvailableLocales(count);
} else if (service->IsUtf8EqualTo(CStrVector("dateformat"))) {
available_locales = icu::DateFormat::getAvailableLocales(count);
} else if (service->IsUtf8EqualTo(CStrVector("breakiterator"))) {
available_locales = icu::BreakIterator::getAvailableLocales(count);
} else if (service->IsUtf8EqualTo(CStrVector("pluralrules"))) {
// TODO(littledan): For PluralRules, filter out locales that
// don't support PluralRules.
// PluralRules is missing an appropriate getAvailableLocales method,
// so we should filter from all locales, but it's not clear how; see
// https://ssl.icu-project.org/trac/ticket/12756
available_locales = icu::Locale::getAvailableLocales(count);
} else if (service->IsUtf8EqualTo(CStrVector("relativetimeformat"))) {
// TODO(ftang): for now just use
// icu::NumberFormat::getAvailableLocales(count) until we migrate to
// Intl::GetAvailableLocales()
available_locales = icu::NumberFormat::getAvailableLocales(count);
} else {
UNREACHABLE();
}
UErrorCode error = U_ZERO_ERROR;
char result[ULOC_FULLNAME_CAPACITY];
Handle<JSObject> locales = factory->NewJSObject(isolate->object_function());
for (int32_t i = 0; i < count; ++i) {
const char* icu_name = available_locales[i].getName();
error = U_ZERO_ERROR;
// No need to force strict BCP47 rules.
uloc_toLanguageTag(icu_name, result, ULOC_FULLNAME_CAPACITY, FALSE, &error);
if (U_FAILURE(error) || error == U_STRING_NOT_TERMINATED_WARNING) {
// This shouldn't happen, but lets not break the user.
continue;
}
RETURN_FAILURE_ON_EXCEPTION(
isolate, JSObject::SetOwnPropertyIgnoreAttributes(
locales, factory->NewStringFromAsciiChecked(result),
factory->NewNumber(i), NONE));
}
Handle<JSObject> locales;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, locales, Intl::AvailableLocalesOf(isolate, service));
return *locales;
}
RUNTIME_FUNCTION(Runtime_GetDefaultICULocale) {
HandleScope scope(isolate);
Factory* factory = isolate->factory();
DCHECK_EQ(0, args.length());
icu::Locale default_locale;
// Translate ICU's fallback locale to a well-known locale.
if (strcmp(default_locale.getName(), "en_US_POSIX") == 0) {
return *factory->NewStringFromStaticChars("en-US");
}
// Set the locale
char result[ULOC_FULLNAME_CAPACITY];
UErrorCode status = U_ZERO_ERROR;
uloc_toLanguageTag(default_locale.getName(), result, ULOC_FULLNAME_CAPACITY,
FALSE, &status);
if (U_SUCCESS(status)) {
return *factory->NewStringFromAsciiChecked(result);
}
return *factory->NewStringFromStaticChars("und");
return *Intl::DefaultLocale(isolate);
}
RUNTIME_FUNCTION(Runtime_IsInitializedIntlObjectOfType) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment