Chromium Code Reviews| Index: src/extensions/experimental/i18n-locale.cc |
| =================================================================== |
| --- src/extensions/experimental/i18n-locale.cc (revision 7735) |
| +++ src/extensions/experimental/i18n-locale.cc (working copy) |
| @@ -27,146 +27,78 @@ |
| #include "i18n-locale.h" |
| -#include <algorithm> |
| -#include <string> |
| - |
| +#include "language-matcher.h" |
| #include "unicode/locid.h" |
| #include "unicode/uloc.h" |
| namespace v8 { |
| namespace internal { |
| +const char* const I18NLocale::kLocaleID = "localeID"; |
| +const char* const I18NLocale::kRegionID = "regionID"; |
| +const char* const I18NLocale::kICULocaleID = "icuLocaleID"; |
| + |
| v8::Handle<v8::Value> I18NLocale::JSLocale(const v8::Arguments& args) { |
| - // TODO(cira): Fetch browser locale. Accept en-US as good default for now. |
| - // We could possibly pass browser locale as a parameter in the constructor. |
| - std::string locale_name("en-US"); |
| - if (args.Length() == 1 && args[0]->IsString()) { |
| - locale_name = *v8::String::Utf8Value(args[0]->ToString()); |
| - } |
| + v8::HandleScope handle_scope; |
| - v8::Local<v8::Object> locale = v8::Object::New(); |
| - locale->Set(v8::String::New("locale"), v8::String::New(locale_name.c_str())); |
| - |
| - icu::Locale icu_locale(locale_name.c_str()); |
| - |
| - const char* language = icu_locale.getLanguage(); |
| - locale->Set(v8::String::New("language"), v8::String::New(language)); |
| - |
| - const char* script = icu_locale.getScript(); |
| - if (strlen(script)) { |
| - locale->Set(v8::String::New("script"), v8::String::New(script)); |
| + if (args.Length() != 1 || !args[0]->IsObject()) { |
| + return v8::Undefined(); |
| } |
| - const char* region = icu_locale.getCountry(); |
| - if (strlen(region)) { |
| - locale->Set(v8::String::New("region"), v8::String::New(region)); |
| - } |
| + v8::Local<v8::Object> settings = args[0]->ToObject(); |
| - return locale; |
| -} |
| - |
| -// TODO(cira): Filter out locales that Chrome doesn't support. |
| -v8::Handle<v8::Value> I18NLocale::JSAvailableLocales( |
| - const v8::Arguments& args) { |
| - v8::Local<v8::Array> all_locales = v8::Array::New(); |
| - |
| - int count = 0; |
| - const icu::Locale* icu_locales = icu::Locale::getAvailableLocales(count); |
| - for (int i = 0; i < count; ++i) { |
| - all_locales->Set(i, v8::String::New(icu_locales[i].getName())); |
| - } |
| - |
| - return all_locales; |
| -} |
| - |
| -// Use - as tag separator, not _ that ICU uses. |
| -static std::string NormalizeLocale(const std::string& locale) { |
| - std::string result(locale); |
| - // TODO(cira): remove STL dependency. |
| - std::replace(result.begin(), result.end(), '_', '-'); |
| - return result; |
| -} |
| - |
| -v8::Handle<v8::Value> I18NLocale::JSMaximizedLocale(const v8::Arguments& args) { |
| - if (!args.Length() || !args[0]->IsString()) { |
| + // Get best match for locale. |
| + v8::TryCatch try_catch; |
| + v8::Handle<v8::Value> locale_id = settings->Get(v8::String::New(kLocaleID)); |
| + if (try_catch.HasCaught()) { |
| return v8::Undefined(); |
| } |
| - UErrorCode status = U_ZERO_ERROR; |
| - std::string locale_name = *v8::String::Utf8Value(args[0]->ToString()); |
| - char max_locale[ULOC_FULLNAME_CAPACITY]; |
| - uloc_addLikelySubtags(locale_name.c_str(), max_locale, |
| - sizeof(max_locale), &status); |
| - if (U_FAILURE(status)) { |
| - return v8::Undefined(); |
| + LocaleIDMatch result; |
| + if (locale_id->IsArray()) { |
| + LanguageMatcher::GetBestMatchForPriorityList( |
| + v8::Handle<v8::Array>::Cast(locale_id), &result); |
| + } else if (locale_id->IsString()) { |
|
jungshik at Google
2011/05/02 20:00:15
perhaps, ASCII-ness has to be checked for input.
Nebojša Ćirić
2011/05/02 22:44:01
I made the change in language-matcher.cc:CompareTo
|
| + LanguageMatcher::GetBestMatchForString(locale_id->ToString(), &result); |
| + } else { |
| + LanguageMatcher::GetBestMatchForString(v8::String::New(""), &result); |
| } |
| - return v8::String::New(NormalizeLocale(max_locale).c_str()); |
| -} |
| + // Get best match for region. |
| + char region_id[ULOC_COUNTRY_CAPACITY]; |
| + sprintf(region_id, ""); |
| -v8::Handle<v8::Value> I18NLocale::JSMinimizedLocale(const v8::Arguments& args) { |
| - if (!args.Length() || !args[0]->IsString()) { |
| + v8::Handle<v8::Value> region = settings->Get(v8::String::New(kRegionID)); |
| + if (try_catch.HasCaught()) { |
| return v8::Undefined(); |
| } |
| + GetBestMatchForRegionID(result.icu_locale, region, region_id); |
| - UErrorCode status = U_ZERO_ERROR; |
| - std::string locale_name = *v8::String::Utf8Value(args[0]->ToString()); |
| - char min_locale[ULOC_FULLNAME_CAPACITY]; |
| - uloc_minimizeSubtags(locale_name.c_str(), min_locale, |
| - sizeof(min_locale), &status); |
| - if (U_FAILURE(status)) { |
| - return v8::Undefined(); |
| - } |
| + // Build JavaScript object that contains bcp and icu locale ID and region ID. |
| + v8::Handle<v8::Object> locale = v8::Object::New(); |
| + locale->Set(v8::String::New(kLocaleID), v8::String::New(result.bcp47_locale)); |
| + locale->Set(v8::String::New(kICULocaleID), |
| + v8::String::New(result.icu_locale)); |
| + locale->Set(v8::String::New(kRegionID), v8::String::New(region_id)); |
| - return v8::String::New(NormalizeLocale(min_locale).c_str()); |
| + return handle_scope.Close(locale); |
| } |
| -// Common code for JSDisplayXXX methods. |
| -static v8::Handle<v8::Value> GetDisplayItem(const v8::Arguments& args, |
| - const std::string& item) { |
| - if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) { |
| - return v8::Undefined(); |
| - } |
| - |
| - std::string base_locale = *v8::String::Utf8Value(args[0]->ToString()); |
| - icu::Locale icu_locale(base_locale.c_str()); |
| - icu::Locale display_locale = |
| - icu::Locale(*v8::String::Utf8Value(args[1]->ToString())); |
| - icu::UnicodeString result; |
| - if (item == "language") { |
| - icu_locale.getDisplayLanguage(display_locale, result); |
| - } else if (item == "script") { |
| - icu_locale.getDisplayScript(display_locale, result); |
| - } else if (item == "region") { |
| - icu_locale.getDisplayCountry(display_locale, result); |
| - } else if (item == "name") { |
| - icu_locale.getDisplayName(display_locale, result); |
| +void I18NLocale::GetBestMatchForRegionID( |
| + const char* locale_id, v8::Handle<v8::Value> region_id, char* result) { |
| + if (region_id->IsString() && region_id->ToString()->Length() != 0) { |
|
jungshik at Google
2011/05/02 19:57:23
just checking: IsString() covers cases where kRegi
Nebojša Ćirić
2011/05/02 22:44:01
My test behaves as expected for this case, and v8
|
| + icu::Locale user_locale( |
| + icu::Locale("und", *v8::String::Utf8Value(region_id->ToString()))); |
| + snprintf(result, ULOC_COUNTRY_CAPACITY, "%s", user_locale.getCountry()); |
| } else { |
| - return v8::Undefined(); |
| + // Expand "de" into "de-Latn-DE" and grab country info. |
|
jungshik at Google
2011/05/02 19:57:23
nit: This command seems to get out of blue. How ab
Nebojša Ćirić
2011/05/02 22:44:01
Done.
|
| + UErrorCode status = U_ZERO_ERROR; |
| + char maximized_locale[ULOC_FULLNAME_CAPACITY]; |
| + uloc_addLikelySubtags(locale_id, maximized_locale, |
| + ULOC_FULLNAME_CAPACITY, &status); |
| + status = U_ZERO_ERROR; |
|
jungshik at Google
2011/05/02 19:57:23
YOu're not supposed to reset the error code here.
Nebojša Ćirić
2011/05/02 22:44:01
Done.
|
| + uloc_getCountry(maximized_locale, result, ULOC_COUNTRY_CAPACITY, &status); |
| } |
| - |
| - if (result.length()) { |
| - return v8::String::New( |
| - reinterpret_cast<const uint16_t*>(result.getBuffer()), result.length()); |
| - } |
| - |
| - return v8::Undefined(); |
| } |
| -v8::Handle<v8::Value> I18NLocale::JSDisplayLanguage(const v8::Arguments& args) { |
| - return GetDisplayItem(args, "language"); |
| -} |
| - |
| -v8::Handle<v8::Value> I18NLocale::JSDisplayScript(const v8::Arguments& args) { |
| - return GetDisplayItem(args, "script"); |
| -} |
| - |
| -v8::Handle<v8::Value> I18NLocale::JSDisplayRegion(const v8::Arguments& args) { |
| - return GetDisplayItem(args, "region"); |
| -} |
| - |
| -v8::Handle<v8::Value> I18NLocale::JSDisplayName(const v8::Arguments& args) { |
| - return GetDisplayItem(args, "name"); |
| -} |
| - |
| } } // namespace v8::internal |