Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: src/extensions/experimental/i18n-locale.cc

Issue 6932007: Revert r7768 (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #include "i18n-locale.h" 28 #include "i18n-locale.h"
29 29
30 #include "language-matcher.h" 30 #include <algorithm>
31 #include "platform.h" 31 #include <string>
32
32 #include "unicode/locid.h" 33 #include "unicode/locid.h"
33 #include "unicode/uloc.h" 34 #include "unicode/uloc.h"
34 #include "utils.h"
35 35
36 namespace v8 { 36 namespace v8 {
37 namespace internal { 37 namespace internal {
38 38
39 const char* const I18NLocale::kLocaleID = "localeID"; 39 v8::Handle<v8::Value> I18NLocale::JSLocale(const v8::Arguments& args) {
40 const char* const I18NLocale::kRegionID = "regionID"; 40 // TODO(cira): Fetch browser locale. Accept en-US as good default for now.
41 const char* const I18NLocale::kICULocaleID = "icuLocaleID"; 41 // We could possibly pass browser locale as a parameter in the constructor.
42 std::string locale_name("en-US");
43 if (args.Length() == 1 && args[0]->IsString()) {
44 locale_name = *v8::String::Utf8Value(args[0]->ToString());
45 }
42 46
43 v8::Handle<v8::Value> I18NLocale::JSLocale(const v8::Arguments& args) { 47 v8::Local<v8::Object> locale = v8::Object::New();
44 v8::HandleScope handle_scope; 48 locale->Set(v8::String::New("locale"), v8::String::New(locale_name.c_str()));
45 49
46 if (args.Length() != 1 || !args[0]->IsObject()) { 50 icu::Locale icu_locale(locale_name.c_str());
51
52 const char* language = icu_locale.getLanguage();
53 locale->Set(v8::String::New("language"), v8::String::New(language));
54
55 const char* script = icu_locale.getScript();
56 if (strlen(script)) {
57 locale->Set(v8::String::New("script"), v8::String::New(script));
58 }
59
60 const char* region = icu_locale.getCountry();
61 if (strlen(region)) {
62 locale->Set(v8::String::New("region"), v8::String::New(region));
63 }
64
65 return locale;
66 }
67
68 // TODO(cira): Filter out locales that Chrome doesn't support.
69 v8::Handle<v8::Value> I18NLocale::JSAvailableLocales(
70 const v8::Arguments& args) {
71 v8::Local<v8::Array> all_locales = v8::Array::New();
72
73 int count = 0;
74 const icu::Locale* icu_locales = icu::Locale::getAvailableLocales(count);
75 for (int i = 0; i < count; ++i) {
76 all_locales->Set(i, v8::String::New(icu_locales[i].getName()));
77 }
78
79 return all_locales;
80 }
81
82 // Use - as tag separator, not _ that ICU uses.
83 static std::string NormalizeLocale(const std::string& locale) {
84 std::string result(locale);
85 // TODO(cira): remove STL dependency.
86 std::replace(result.begin(), result.end(), '_', '-');
87 return result;
88 }
89
90 v8::Handle<v8::Value> I18NLocale::JSMaximizedLocale(const v8::Arguments& args) {
91 if (!args.Length() || !args[0]->IsString()) {
47 return v8::Undefined(); 92 return v8::Undefined();
48 } 93 }
49 94
50 v8::Local<v8::Object> settings = args[0]->ToObject(); 95 UErrorCode status = U_ZERO_ERROR;
51 96 std::string locale_name = *v8::String::Utf8Value(args[0]->ToString());
52 // Get best match for locale. 97 char max_locale[ULOC_FULLNAME_CAPACITY];
53 v8::TryCatch try_catch; 98 uloc_addLikelySubtags(locale_name.c_str(), max_locale,
54 v8::Handle<v8::Value> locale_id = settings->Get(v8::String::New(kLocaleID)); 99 sizeof(max_locale), &status);
55 if (try_catch.HasCaught()) { 100 if (U_FAILURE(status)) {
56 return v8::Undefined(); 101 return v8::Undefined();
57 } 102 }
58 103
59 LocaleIDMatch result; 104 return v8::String::New(NormalizeLocale(max_locale).c_str());
60 if (locale_id->IsArray()) { 105 }
61 LanguageMatcher::GetBestMatchForPriorityList(
62 v8::Handle<v8::Array>::Cast(locale_id), &result);
63 } else if (locale_id->IsString()) {
64 LanguageMatcher::GetBestMatchForString(locale_id->ToString(), &result);
65 } else {
66 LanguageMatcher::GetBestMatchForString(v8::String::New(""), &result);
67 }
68 106
69 // Get best match for region. 107 v8::Handle<v8::Value> I18NLocale::JSMinimizedLocale(const v8::Arguments& args) {
70 char region_id[ULOC_COUNTRY_CAPACITY]; 108 if (!args.Length() || !args[0]->IsString()) {
71 OS::SNPrintF(Vector<char>(region_id, ARRAY_SIZE("")), "");
72
73 v8::Handle<v8::Value> region = settings->Get(v8::String::New(kRegionID));
74 if (try_catch.HasCaught()) {
75 return v8::Undefined(); 109 return v8::Undefined();
76 } 110 }
77 111
78 if (!GetBestMatchForRegionID(result.icu_id, region, region_id)) { 112 UErrorCode status = U_ZERO_ERROR;
79 // Set region id to empty string because region couldn't be inferred. 113 std::string locale_name = *v8::String::Utf8Value(args[0]->ToString());
80 OS::SNPrintF(Vector<char>(region_id, ARRAY_SIZE("")), ""); 114 char min_locale[ULOC_FULLNAME_CAPACITY];
115 uloc_minimizeSubtags(locale_name.c_str(), min_locale,
116 sizeof(min_locale), &status);
117 if (U_FAILURE(status)) {
118 return v8::Undefined();
81 } 119 }
82 120
83 // Build JavaScript object that contains bcp and icu locale ID and region ID. 121 return v8::String::New(NormalizeLocale(min_locale).c_str());
84 v8::Handle<v8::Object> locale = v8::Object::New();
85 locale->Set(v8::String::New(kLocaleID), v8::String::New(result.bcp47_id));
86 locale->Set(v8::String::New(kICULocaleID), v8::String::New(result.icu_id));
87 locale->Set(v8::String::New(kRegionID), v8::String::New(region_id));
88
89 return handle_scope.Close(locale);
90 } 122 }
91 123
92 bool I18NLocale::GetBestMatchForRegionID( 124 // Common code for JSDisplayXXX methods.
93 const char* locale_id, v8::Handle<v8::Value> region_id, char* result) { 125 static v8::Handle<v8::Value> GetDisplayItem(const v8::Arguments& args,
94 if (region_id->IsString() && region_id->ToString()->Length() != 0) { 126 const std::string& item) {
95 icu::Locale user_locale( 127 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) {
96 icu::Locale("und", *v8::String::Utf8Value(region_id->ToString()))); 128 return v8::Undefined();
97 OS::SNPrintF(Vector<char>(result, ULOC_COUNTRY_CAPACITY),
98 "%s", user_locale.getCountry());
99 return true;
100 } 129 }
101 // Maximize locale_id to infer the region (e.g. expand "de" to "de-Latn-DE"
102 // and grab "DE" from the result).
103 UErrorCode status = U_ZERO_ERROR;
104 char maximized_locale[ULOC_FULLNAME_CAPACITY];
105 uloc_addLikelySubtags(
106 locale_id, maximized_locale, ULOC_FULLNAME_CAPACITY, &status);
107 uloc_getCountry(maximized_locale, result, ULOC_COUNTRY_CAPACITY, &status);
108 130
109 return !U_FAILURE(status); 131 std::string base_locale = *v8::String::Utf8Value(args[0]->ToString());
132 icu::Locale icu_locale(base_locale.c_str());
133 icu::Locale display_locale =
134 icu::Locale(*v8::String::Utf8Value(args[1]->ToString()));
135 icu::UnicodeString result;
136 if (item == "language") {
137 icu_locale.getDisplayLanguage(display_locale, result);
138 } else if (item == "script") {
139 icu_locale.getDisplayScript(display_locale, result);
140 } else if (item == "region") {
141 icu_locale.getDisplayCountry(display_locale, result);
142 } else if (item == "name") {
143 icu_locale.getDisplayName(display_locale, result);
144 } else {
145 return v8::Undefined();
146 }
147
148 if (result.length()) {
149 return v8::String::New(
150 reinterpret_cast<const uint16_t*>(result.getBuffer()), result.length());
151 }
152
153 return v8::Undefined();
154 }
155
156 v8::Handle<v8::Value> I18NLocale::JSDisplayLanguage(const v8::Arguments& args) {
157 return GetDisplayItem(args, "language");
158 }
159
160 v8::Handle<v8::Value> I18NLocale::JSDisplayScript(const v8::Arguments& args) {
161 return GetDisplayItem(args, "script");
162 }
163
164 v8::Handle<v8::Value> I18NLocale::JSDisplayRegion(const v8::Arguments& args) {
165 return GetDisplayItem(args, "region");
166 }
167
168 v8::Handle<v8::Value> I18NLocale::JSDisplayName(const v8::Arguments& args) {
169 return GetDisplayItem(args, "name");
110 } 170 }
111 171
112 } } // namespace v8::internal 172 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/extensions/experimental/i18n-locale.h ('k') | src/extensions/experimental/language-matcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698