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

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

Issue 6901141: Change v8Locale to match proposal - constructor is different and I've added (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: strcpy->snprintf 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 <algorithm> 30 #include "language-matcher.h"
31 #include <string>
32
33 #include "unicode/locid.h" 31 #include "unicode/locid.h"
34 #include "unicode/uloc.h" 32 #include "unicode/uloc.h"
35 33
36 namespace v8 { 34 namespace v8 {
37 namespace internal { 35 namespace internal {
38 36
37 const char* const I18NLocale::kLocaleID = "localeID";
38 const char* const I18NLocale::kRegionID = "regionID";
39 const char* const I18NLocale::kICULocaleID = "icuLocaleID";
40
39 v8::Handle<v8::Value> I18NLocale::JSLocale(const v8::Arguments& args) { 41 v8::Handle<v8::Value> I18NLocale::JSLocale(const v8::Arguments& args) {
40 // TODO(cira): Fetch browser locale. Accept en-US as good default for now. 42 v8::HandleScope handle_scope;
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 }
46 43
47 v8::Local<v8::Object> locale = v8::Object::New(); 44 if (args.Length() != 1 || !args[0]->IsObject()) {
48 locale->Set(v8::String::New("locale"), v8::String::New(locale_name.c_str()));
49
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()) {
92 return v8::Undefined(); 45 return v8::Undefined();
93 } 46 }
94 47
95 UErrorCode status = U_ZERO_ERROR; 48 v8::Local<v8::Object> settings = args[0]->ToObject();
96 std::string locale_name = *v8::String::Utf8Value(args[0]->ToString()); 49
97 char max_locale[ULOC_FULLNAME_CAPACITY]; 50 // Get best match for locale.
98 uloc_addLikelySubtags(locale_name.c_str(), max_locale, 51 v8::TryCatch try_catch;
99 sizeof(max_locale), &status); 52 v8::Handle<v8::Value> locale_id = settings->Get(v8::String::New(kLocaleID));
100 if (U_FAILURE(status)) { 53 if (try_catch.HasCaught()) {
101 return v8::Undefined(); 54 return v8::Undefined();
102 } 55 }
103 56
104 return v8::String::New(NormalizeLocale(max_locale).c_str()); 57 LocaleIDMatch result;
58 if (locale_id->IsArray()) {
59 LanguageMatcher::GetBestMatchForPriorityList(
60 v8::Handle<v8::Array>::Cast(locale_id), &result);
61 } 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
62 LanguageMatcher::GetBestMatchForString(locale_id->ToString(), &result);
63 } else {
64 LanguageMatcher::GetBestMatchForString(v8::String::New(""), &result);
65 }
66
67 // Get best match for region.
68 char region_id[ULOC_COUNTRY_CAPACITY];
69 sprintf(region_id, "");
70
71 v8::Handle<v8::Value> region = settings->Get(v8::String::New(kRegionID));
72 if (try_catch.HasCaught()) {
73 return v8::Undefined();
74 }
75 GetBestMatchForRegionID(result.icu_locale, region, region_id);
76
77 // Build JavaScript object that contains bcp and icu locale ID and region ID.
78 v8::Handle<v8::Object> locale = v8::Object::New();
79 locale->Set(v8::String::New(kLocaleID), v8::String::New(result.bcp47_locale));
80 locale->Set(v8::String::New(kICULocaleID),
81 v8::String::New(result.icu_locale));
82 locale->Set(v8::String::New(kRegionID), v8::String::New(region_id));
83
84 return handle_scope.Close(locale);
105 } 85 }
106 86
107 v8::Handle<v8::Value> I18NLocale::JSMinimizedLocale(const v8::Arguments& args) { 87 void I18NLocale::GetBestMatchForRegionID(
108 if (!args.Length() || !args[0]->IsString()) { 88 const char* locale_id, v8::Handle<v8::Value> region_id, char* result) {
109 return v8::Undefined(); 89 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
90 icu::Locale user_locale(
91 icu::Locale("und", *v8::String::Utf8Value(region_id->ToString())));
92 snprintf(result, ULOC_COUNTRY_CAPACITY, "%s", user_locale.getCountry());
93 } else {
94 // 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.
95 UErrorCode status = U_ZERO_ERROR;
96 char maximized_locale[ULOC_FULLNAME_CAPACITY];
97 uloc_addLikelySubtags(locale_id, maximized_locale,
98 ULOC_FULLNAME_CAPACITY, &status);
99 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.
100 uloc_getCountry(maximized_locale, result, ULOC_COUNTRY_CAPACITY, &status);
110 } 101 }
111
112 UErrorCode status = U_ZERO_ERROR;
113 std::string locale_name = *v8::String::Utf8Value(args[0]->ToString());
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();
119 }
120
121 return v8::String::New(NormalizeLocale(min_locale).c_str());
122 }
123
124 // Common code for JSDisplayXXX methods.
125 static v8::Handle<v8::Value> GetDisplayItem(const v8::Arguments& args,
126 const std::string& item) {
127 if (args.Length() != 2 || !args[0]->IsString() || !args[1]->IsString()) {
128 return v8::Undefined();
129 }
130
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");
170 } 102 }
171 103
172 } } // namespace v8::internal 104 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698