Commit f97022cf authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

Reland "[Intl] Replace uloc_(to|for)Language w/ Locale API"

This is a reland of 7b744e3a

I cannot reproduce the problem in this reland CL. I think
the origin breakage is just due to test flakiness.
Try to reland it without changes

TBR=jshin@chromium.org

Original change's description:
> [Intl] Replace uloc_(to|for)Language w/ Locale API
>
> Bug: v8:8468
> Change-Id: Id2f8d165e5f29f429821b44def2512fe760c0a51
> Reviewed-on: https://chromium-review.googlesource.com/c/1377989
> Reviewed-by: Jungshik Shin <jshin@chromium.org>
> Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
> Commit-Queue: Frank Tang <ftang@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#58260}

Bug: v8:8468
Change-Id: I5f34d061d630d07f5c9da07f9adb1efa040d66d5
Reviewed-on: https://chromium-review.googlesource.com/c/1378658Reviewed-by: 's avatarFrank Tang <ftang@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58297}
parent 57d4b8e6
...@@ -407,21 +407,9 @@ icu::Locale Intl::CreateICULocale(const std::string& bcp47_locale) { ...@@ -407,21 +407,9 @@ icu::Locale Intl::CreateICULocale(const std::string& bcp47_locale) {
// Convert BCP47 into ICU locale format. // Convert BCP47 into ICU locale format.
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
char icu_result[ULOC_FULLNAME_CAPACITY];
int parsed_length = 0;
// bcp47_locale_str should be a canonicalized language tag, which icu::Locale icu_locale = icu::Locale::forLanguageTag(bcp47_locale, status);
// means this shouldn't fail.
uloc_forLanguageTag(bcp47_locale.c_str(), icu_result, ULOC_FULLNAME_CAPACITY,
&parsed_length, &status);
CHECK(U_SUCCESS(status)); CHECK(U_SUCCESS(status));
// bcp47_locale is already checked for its structural validity
// so that it should be parsed completely.
size_t bcp47_length = bcp47_locale.length();
CHECK_EQ(bcp47_length, parsed_length);
icu::Locale icu_locale(icu_result);
if (icu_locale.isBogus()) { if (icu_locale.isBogus()) {
FATAL("Failed to create ICU locale, are ICU data files missing?"); FATAL("Failed to create ICU locale, are ICU data files missing?");
} }
......
...@@ -284,12 +284,9 @@ MaybeHandle<JSObject> JSDateTimeFormat::ResolvedOptions( ...@@ -284,12 +284,9 @@ MaybeHandle<JSObject> JSDateTimeFormat::ResolvedOptions(
CHECK(!date_time_format->icu_locale().is_null()); CHECK(!date_time_format->icu_locale().is_null());
CHECK_NOT_NULL(date_time_format->icu_locale()->raw()); CHECK_NOT_NULL(date_time_format->icu_locale()->raw());
UErrorCode status = U_ZERO_ERROR; icu::Locale icu_locale = *(date_time_format->icu_locale()->raw());
char language[ULOC_FULLNAME_CAPACITY]; Handle<String> locale = factory->NewStringFromAsciiChecked(
uloc_toLanguageTag(date_time_format->icu_locale()->raw()->getName(), language, Intl::ToLanguageTag(icu_locale).c_str());
ULOC_FULLNAME_CAPACITY, FALSE, &status);
CHECK(U_SUCCESS(status));
Handle<String> locale = factory->NewStringFromAsciiChecked(language);
icu::SimpleDateFormat* icu_simple_date_format = icu::SimpleDateFormat* icu_simple_date_format =
date_time_format->icu_simple_date_format()->raw(); date_time_format->icu_simple_date_format()->raw();
...@@ -313,7 +310,7 @@ MaybeHandle<JSObject> JSDateTimeFormat::ResolvedOptions( ...@@ -313,7 +310,7 @@ MaybeHandle<JSObject> JSDateTimeFormat::ResolvedOptions(
const icu::TimeZone& tz = calendar->getTimeZone(); const icu::TimeZone& tz = calendar->getTimeZone();
icu::UnicodeString time_zone; icu::UnicodeString time_zone;
tz.getID(time_zone); tz.getID(time_zone);
status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString canonical_time_zone; icu::UnicodeString canonical_time_zone;
icu::TimeZone::getCanonicalID(time_zone, canonical_time_zone, status); icu::TimeZone::getCanonicalID(time_zone, canonical_time_zone, status);
Handle<Object> timezone_value; Handle<Object> timezone_value;
......
...@@ -312,48 +312,34 @@ MaybeHandle<JSLocale> JSLocale::Initialize(Isolate* isolate, ...@@ -312,48 +312,34 @@ MaybeHandle<JSLocale> JSLocale::Initialize(Isolate* isolate,
} }
namespace { namespace {
Handle<String> MorphLocale(Isolate* isolate, String locale,
Handle<String> MorphLocale(Isolate* isolate, String language_tag, void (*morph_func)(icu::Locale* l,
int32_t (*morph_func)(const char*, char*, int32_t, UErrorCode* status)) {
UErrorCode*)) {
Factory* factory = isolate->factory();
char localeBuffer[ULOC_FULLNAME_CAPACITY];
char morphBuffer[ULOC_FULLNAME_CAPACITY];
UErrorCode status = U_ZERO_ERROR; UErrorCode status = U_ZERO_ERROR;
// Convert from language id to locale. icu::Locale icu_locale =
int32_t parsed_length; icu::Locale::forLanguageTag(locale.ToCString().get(), status);
int32_t length = CHECK(U_SUCCESS(status));
uloc_forLanguageTag(language_tag->ToCString().get(), localeBuffer, CHECK(!icu_locale.isBogus());
ULOC_FULLNAME_CAPACITY, &parsed_length, &status); (*morph_func)(&icu_locale, &status);
CHECK(parsed_length == language_tag->length()); CHECK(U_SUCCESS(status));
DCHECK(U_SUCCESS(status)); CHECK(!icu_locale.isBogus());
DCHECK_GT(length, 0); return isolate->factory()->NewStringFromAsciiChecked(
DCHECK_NOT_NULL(morph_func); Intl::ToLanguageTag(icu_locale).c_str());
// Add the likely subtags or Minimize the subtags on the locale id
length =
(*morph_func)(localeBuffer, morphBuffer, ULOC_FULLNAME_CAPACITY, &status);
DCHECK(U_SUCCESS(status));
DCHECK_GT(length, 0);
// Returns a well-formed language tag
length = uloc_toLanguageTag(morphBuffer, localeBuffer, ULOC_FULLNAME_CAPACITY,
false, &status);
DCHECK(U_SUCCESS(status));
DCHECK_GT(length, 0);
std::string lang(localeBuffer, length);
std::replace(lang.begin(), lang.end(), '_', '-');
return factory->NewStringFromAsciiChecked(lang.c_str());
} }
} // namespace } // namespace
Handle<String> JSLocale::Maximize(Isolate* isolate, String locale) { Handle<String> JSLocale::Maximize(Isolate* isolate, String locale) {
return MorphLocale(isolate, locale, uloc_addLikelySubtags); return MorphLocale(isolate, locale,
[](icu::Locale* icu_locale, UErrorCode* status) {
icu_locale->addLikelySubtags(*status);
});
} }
Handle<String> JSLocale::Minimize(Isolate* isolate, String locale) { Handle<String> JSLocale::Minimize(Isolate* isolate, String locale) {
return MorphLocale(isolate, locale, uloc_minimizeSubtags); return MorphLocale(isolate, locale,
[](icu::Locale* icu_locale, UErrorCode* status) {
icu_locale->minimizeSubtags(*status);
});
} }
Handle<String> JSLocale::CaseFirstAsString() const { Handle<String> JSLocale::CaseFirstAsString() const {
......
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