| OLD | NEW |
| (Empty) |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | |
| 2 // Redistribution and use in source and binary forms, with or without | |
| 3 // modification, are permitted provided that the following conditions are | |
| 4 // met: | |
| 5 // | |
| 6 // * Redistributions of source code must retain the above copyright | |
| 7 // notice, this list of conditions and the following disclaimer. | |
| 8 // * Redistributions in binary form must reproduce the above | |
| 9 // copyright notice, this list of conditions and the following | |
| 10 // disclaimer in the documentation and/or other materials provided | |
| 11 // with the distribution. | |
| 12 // * Neither the name of Google Inc. nor the names of its | |
| 13 // contributors may be used to endorse or promote products derived | |
| 14 // from this software without specific prior written permission. | |
| 15 // | |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 27 | |
| 28 #include "src/extensions/experimental/i18n-locale.h" | |
| 29 | |
| 30 #include "src/extensions/experimental/i18n-utils.h" | |
| 31 #include "src/extensions/experimental/language-matcher.h" | |
| 32 #include "unicode/locid.h" | |
| 33 #include "unicode/uloc.h" | |
| 34 | |
| 35 namespace v8 { | |
| 36 namespace internal { | |
| 37 | |
| 38 const char* const I18NLocale::kLocaleID = "localeID"; | |
| 39 const char* const I18NLocale::kRegionID = "regionID"; | |
| 40 const char* const I18NLocale::kICULocaleID = "icuLocaleID"; | |
| 41 | |
| 42 v8::Handle<v8::Value> I18NLocale::JSLocale(const v8::Arguments& args) { | |
| 43 v8::HandleScope handle_scope; | |
| 44 | |
| 45 if (args.Length() != 1 || !args[0]->IsObject()) { | |
| 46 return v8::Undefined(); | |
| 47 } | |
| 48 | |
| 49 v8::Local<v8::Object> settings = args[0]->ToObject(); | |
| 50 | |
| 51 // Get best match for locale. | |
| 52 v8::TryCatch try_catch; | |
| 53 v8::Handle<v8::Value> locale_id = settings->Get(v8::String::New(kLocaleID)); | |
| 54 if (try_catch.HasCaught()) { | |
| 55 return v8::Undefined(); | |
| 56 } | |
| 57 | |
| 58 LocaleIDMatch result; | |
| 59 if (locale_id->IsArray()) { | |
| 60 LanguageMatcher::GetBestMatchForPriorityList( | |
| 61 v8::Handle<v8::Array>::Cast(locale_id), &result); | |
| 62 } else if (locale_id->IsString()) { | |
| 63 LanguageMatcher::GetBestMatchForString(locale_id->ToString(), &result); | |
| 64 } else { | |
| 65 LanguageMatcher::GetBestMatchForString(v8::String::New(""), &result); | |
| 66 } | |
| 67 | |
| 68 // Get best match for region. | |
| 69 char region_id[ULOC_COUNTRY_CAPACITY]; | |
| 70 I18NUtils::StrNCopy(region_id, ULOC_COUNTRY_CAPACITY, ""); | |
| 71 | |
| 72 v8::Handle<v8::Value> region = settings->Get(v8::String::New(kRegionID)); | |
| 73 if (try_catch.HasCaught()) { | |
| 74 return v8::Undefined(); | |
| 75 } | |
| 76 | |
| 77 if (!GetBestMatchForRegionID(result.icu_id, region, region_id)) { | |
| 78 // Set region id to empty string because region couldn't be inferred. | |
| 79 I18NUtils::StrNCopy(region_id, ULOC_COUNTRY_CAPACITY, ""); | |
| 80 } | |
| 81 | |
| 82 // Build JavaScript object that contains bcp and icu locale ID and region ID. | |
| 83 v8::Handle<v8::Object> locale = v8::Object::New(); | |
| 84 locale->Set(v8::String::New(kLocaleID), v8::String::New(result.bcp47_id)); | |
| 85 locale->Set(v8::String::New(kICULocaleID), v8::String::New(result.icu_id)); | |
| 86 locale->Set(v8::String::New(kRegionID), v8::String::New(region_id)); | |
| 87 | |
| 88 return handle_scope.Close(locale); | |
| 89 } | |
| 90 | |
| 91 bool I18NLocale::GetBestMatchForRegionID( | |
| 92 const char* locale_id, v8::Handle<v8::Value> region_id, char* result) { | |
| 93 if (region_id->IsString() && region_id->ToString()->Length() != 0) { | |
| 94 icu::Locale user_locale( | |
| 95 icu::Locale("und", *v8::String::Utf8Value(region_id->ToString()))); | |
| 96 I18NUtils::StrNCopy( | |
| 97 result, ULOC_COUNTRY_CAPACITY, user_locale.getCountry()); | |
| 98 return true; | |
| 99 } | |
| 100 // Maximize locale_id to infer the region (e.g. expand "de" to "de-Latn-DE" | |
| 101 // and grab "DE" from the result). | |
| 102 UErrorCode status = U_ZERO_ERROR; | |
| 103 char maximized_locale[ULOC_FULLNAME_CAPACITY]; | |
| 104 uloc_addLikelySubtags( | |
| 105 locale_id, maximized_locale, ULOC_FULLNAME_CAPACITY, &status); | |
| 106 uloc_getCountry(maximized_locale, result, ULOC_COUNTRY_CAPACITY, &status); | |
| 107 | |
| 108 return !U_FAILURE(status); | |
| 109 } | |
| 110 | |
| 111 } } // namespace v8::internal | |
| OLD | NEW |