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
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.
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.
28
#include "extensions/experimental/i18n-locale.h"
30
#include "extensions/experimental/i18n-utils.h"
31
#include "extensions/experimental/language-matcher.h"
32
#include "unicode/locid.h"
33
#include "unicode/uloc.h"
37
const char* const I18NLocale::kLocaleID = "localeID";
38
const char* const I18NLocale::kRegionID = "regionID";
39
const char* const I18NLocale::kICULocaleID = "icuLocaleID";
41
v8::Handle<v8::Value> I18NLocale::JSLocale(const v8::Arguments& args) {
42
v8::HandleScope handle_scope;
44
if (args.Length() != 1 || !args[0]->IsObject()) {
45
return v8::Undefined();
48
v8::Local<v8::Object> settings = args[0]->ToObject();
50
// Get best match for locale.
51
v8::TryCatch try_catch;
52
v8::Handle<v8::Value> locale_id = settings->Get(v8::String::New(kLocaleID));
53
if (try_catch.HasCaught()) {
54
return v8::Undefined();
58
if (locale_id->IsArray()) {
59
LanguageMatcher::GetBestMatchForPriorityList(
60
v8::Handle<v8::Array>::Cast(locale_id), &result);
61
} else if (locale_id->IsString()) {
62
LanguageMatcher::GetBestMatchForString(locale_id->ToString(), &result);
64
LanguageMatcher::GetBestMatchForString(v8::String::New(""), &result);
67
// Get best match for region.
68
char region_id[ULOC_COUNTRY_CAPACITY];
69
I18NUtils::StrNCopy(region_id, ULOC_COUNTRY_CAPACITY, "");
71
v8::Handle<v8::Value> region = settings->Get(v8::String::New(kRegionID));
72
if (try_catch.HasCaught()) {
73
return v8::Undefined();
76
if (!GetBestMatchForRegionID(result.icu_id, region, region_id)) {
77
// Set region id to empty string because region couldn't be inferred.
78
I18NUtils::StrNCopy(region_id, ULOC_COUNTRY_CAPACITY, "");
81
// Build JavaScript object that contains bcp and icu locale ID and region ID.
82
v8::Handle<v8::Object> locale = v8::Object::New();
83
locale->Set(v8::String::New(kLocaleID), v8::String::New(result.bcp47_id));
84
locale->Set(v8::String::New(kICULocaleID), v8::String::New(result.icu_id));
85
locale->Set(v8::String::New(kRegionID), v8::String::New(region_id));
87
return handle_scope.Close(locale);
90
bool I18NLocale::GetBestMatchForRegionID(
91
const char* locale_id, v8::Handle<v8::Value> region_id, char* result) {
92
if (region_id->IsString() && region_id->ToString()->Length() != 0) {
93
icu::Locale user_locale(
94
icu::Locale("und", *v8::String::Utf8Value(region_id->ToString())));
96
result, ULOC_COUNTRY_CAPACITY, user_locale.getCountry());
99
// Maximize locale_id to infer the region (e.g. expand "de" to "de-Latn-DE"
100
// and grab "DE" from the result).
101
UErrorCode status = U_ZERO_ERROR;
102
char maximized_locale[ULOC_FULLNAME_CAPACITY];
103
uloc_addLikelySubtags(
104
locale_id, maximized_locale, ULOC_FULLNAME_CAPACITY, &status);
105
uloc_getCountry(maximized_locale, result, ULOC_COUNTRY_CAPACITY, &status);
107
return !U_FAILURE(status);
110
} // namespace v8_i18n