Commit 7b744e3a authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

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

Bug: v8:8468
Change-Id: Id2f8d165e5f29f429821b44def2512fe760c0a51
Reviewed-on: https://chromium-review.googlesource.com/c/1377989Reviewed-by: 's avatarJungshik Shin <jshin@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Commit-Queue: Frank Tang <ftang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58260}
parent 9005da4b
......@@ -407,21 +407,9 @@ icu::Locale Intl::CreateICULocale(const std::string& bcp47_locale) {
// Convert BCP47 into ICU locale format.
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
// means this shouldn't fail.
uloc_forLanguageTag(bcp47_locale.c_str(), icu_result, ULOC_FULLNAME_CAPACITY,
&parsed_length, &status);
icu::Locale icu_locale = icu::Locale::forLanguageTag(bcp47_locale, 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()) {
FATAL("Failed to create ICU locale, are ICU data files missing?");
}
......
......@@ -282,14 +282,9 @@ MaybeHandle<JSObject> JSDateTimeFormat::ResolvedOptions(
Handle<Object> resolved_obj;
CHECK_NOT_NULL(date_time_format->icu_locale());
CHECK_NOT_NULL(date_time_format->icu_locale()->raw());
UErrorCode status = U_ZERO_ERROR;
char language[ULOC_FULLNAME_CAPACITY];
uloc_toLanguageTag(date_time_format->icu_locale()->raw()->getName(), language,
ULOC_FULLNAME_CAPACITY, FALSE, &status);
CHECK(U_SUCCESS(status));
Handle<String> locale = factory->NewStringFromAsciiChecked(language);
icu::Locale icu_locale = *(date_time_format->icu_locale()->raw());
Handle<String> locale = factory->NewStringFromAsciiChecked(
Intl::ToLanguageTag(icu_locale).c_str());
icu::SimpleDateFormat* icu_simple_date_format =
date_time_format->icu_simple_date_format()->raw();
......@@ -313,7 +308,7 @@ MaybeHandle<JSObject> JSDateTimeFormat::ResolvedOptions(
const icu::TimeZone& tz = calendar->getTimeZone();
icu::UnicodeString time_zone;
tz.getID(time_zone);
status = U_ZERO_ERROR;
UErrorCode status = U_ZERO_ERROR;
icu::UnicodeString canonical_time_zone;
icu::TimeZone::getCanonicalID(time_zone, canonical_time_zone, status);
Handle<Object> timezone_value;
......
......@@ -312,48 +312,34 @@ MaybeHandle<JSLocale> JSLocale::Initialize(Isolate* isolate,
}
namespace {
Handle<String> MorphLocale(Isolate* isolate, String language_tag,
int32_t (*morph_func)(const char*, char*, int32_t,
UErrorCode*)) {
Factory* factory = isolate->factory();
char localeBuffer[ULOC_FULLNAME_CAPACITY];
char morphBuffer[ULOC_FULLNAME_CAPACITY];
Handle<String> MorphLocale(Isolate* isolate, String locale,
void (*morph_func)(icu::Locale* l,
UErrorCode* status)) {
UErrorCode status = U_ZERO_ERROR;
// Convert from language id to locale.
int32_t parsed_length;
int32_t length =
uloc_forLanguageTag(language_tag->ToCString().get(), localeBuffer,
ULOC_FULLNAME_CAPACITY, &parsed_length, &status);
CHECK(parsed_length == language_tag->length());
DCHECK(U_SUCCESS(status));
DCHECK_GT(length, 0);
DCHECK_NOT_NULL(morph_func);
// 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());
icu::Locale icu_locale =
icu::Locale::forLanguageTag(locale.ToCString().get(), status);
CHECK(U_SUCCESS(status));
CHECK(!icu_locale.isBogus());
(*morph_func)(&icu_locale, &status);
CHECK(U_SUCCESS(status));
CHECK(!icu_locale.isBogus());
return isolate->factory()->NewStringFromAsciiChecked(
Intl::ToLanguageTag(icu_locale).c_str());
}
} // namespace
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) {
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 {
......
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