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

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

Issue 18487004: Import the v8-i18n extension into v8 (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 7 years, 5 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
(Empty)
1 // Copyright 2013 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 // limitations under the License.
28
29 #include "locale.h"
30
31 #include <string.h>
32
33 #include "unicode/brkiter.h"
34 #include "unicode/coll.h"
35 #include "unicode/datefmt.h"
36 #include "unicode/numfmt.h"
37 #include "unicode/uloc.h"
38 #include "unicode/uversion.h"
39
40 namespace v8_i18n {
41
42 void JSCanonicalizeLanguageTag(
43 const v8::FunctionCallbackInfo<v8::Value>& args) {
44 // Expect locale id which is a string.
45 if (args.Length() != 1 || !args[0]->IsString()) {
46 v8::ThrowException(v8::Exception::SyntaxError(
47 v8::String::New("Locale identifier, as a string, is required.")));
48 return;
49 }
50
51 UErrorCode error = U_ZERO_ERROR;
52
53 char icu_result[ULOC_FULLNAME_CAPACITY];
54 int icu_length = 0;
55
56 // Return value which denotes invalid language tag.
57 const char* const kInvalidTag = "invalid-tag";
58
59 v8::String::AsciiValue locale_id(args[0]->ToString());
60 if (*locale_id == NULL) {
61 args.GetReturnValue().Set(v8::String::New(kInvalidTag));
62 return;
63 }
64
65 uloc_forLanguageTag(*locale_id, icu_result, ULOC_FULLNAME_CAPACITY,
66 &icu_length, &error);
67 if (U_FAILURE(error) || icu_length == 0) {
68 args.GetReturnValue().Set(v8::String::New(kInvalidTag));
69 return;
70 }
71
72 char result[ULOC_FULLNAME_CAPACITY];
73
74 // Force strict BCP47 rules.
75 uloc_toLanguageTag(icu_result, result, ULOC_FULLNAME_CAPACITY, TRUE, &error);
76
77 if (U_FAILURE(error)) {
78 args.GetReturnValue().Set(v8::String::New(kInvalidTag));
79 return;
80 }
81
82 args.GetReturnValue().Set(v8::String::New(result));
83 }
84
85 void JSAvailableLocalesOf(const v8::FunctionCallbackInfo<v8::Value>& args) {
86 // Expect service name which is a string.
87 if (args.Length() != 1 || !args[0]->IsString()) {
88 v8::ThrowException(v8::Exception::SyntaxError(
89 v8::String::New("Service identifier, as a string, is required.")));
90 return;
91 }
92
93 const icu::Locale* available_locales = NULL;
94
95 int32_t count = 0;
96 v8::String::AsciiValue service(args[0]->ToString());
97 if (strcmp(*service, "collator") == 0) {
98 available_locales = icu::Collator::getAvailableLocales(count);
99 } else if (strcmp(*service, "numberformat") == 0) {
100 available_locales = icu::NumberFormat::getAvailableLocales(count);
101 } else if (strcmp(*service, "dateformat") == 0) {
102 available_locales = icu::DateFormat::getAvailableLocales(count);
103 } else if (strcmp(*service, "breakiterator") == 0) {
104 available_locales = icu::BreakIterator::getAvailableLocales(count);
105 }
106
107 v8::TryCatch try_catch;
108 UErrorCode error = U_ZERO_ERROR;
109 char result[ULOC_FULLNAME_CAPACITY];
110 v8::Handle<v8::Object> locales = v8::Object::New();
111
112 for (int32_t i = 0; i < count; ++i) {
113 const char* icu_name = available_locales[i].getName();
114
115 error = U_ZERO_ERROR;
116 // No need to force strict BCP47 rules.
117 uloc_toLanguageTag(icu_name, result, ULOC_FULLNAME_CAPACITY, FALSE, &error);
118 if (U_FAILURE(error)) {
119 // This shouldn't happen, but lets not break the user.
120 continue;
121 }
122
123 // Index is just a dummy value for the property value.
124 locales->Set(v8::String::New(result), v8::Integer::New(i));
125 if (try_catch.HasCaught()) {
126 // Ignore error, but stop processing and return.
127 break;
128 }
129 }
130
131 args.GetReturnValue().Set(locales);
132 }
133
134 void JSGetDefaultICULocale(const v8::FunctionCallbackInfo<v8::Value>& args) {
135 icu::Locale default_locale;
136
137 // Set the locale
138 char result[ULOC_FULLNAME_CAPACITY];
139 UErrorCode status = U_ZERO_ERROR;
140 uloc_toLanguageTag(
141 default_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status);
142 if (U_SUCCESS(status)) {
143 args.GetReturnValue().Set(v8::String::New(result));
144 return;
145 }
146
147 args.GetReturnValue().Set(v8::String::New("und"));
148 }
149
150 void JSGetLanguageTagVariants(const v8::FunctionCallbackInfo<v8::Value>& args) {
151 v8::TryCatch try_catch;
152
153 // Expect an array of strings.
154 if (args.Length() != 1 || !args[0]->IsArray()) {
155 v8::ThrowException(v8::Exception::SyntaxError(
156 v8::String::New("Internal error. Expected Array<String>.")));
157 return;
158 }
159
160 v8::Local<v8::Array> input = v8::Local<v8::Array>::Cast(args[0]);
161 v8::Handle<v8::Array> output = v8::Array::New(input->Length());
162 for (unsigned int i = 0; i < input->Length(); ++i) {
163 v8::Local<v8::Value> locale_id = input->Get(i);
164 if (try_catch.HasCaught()) {
165 break;
166 }
167
168 if (!locale_id->IsString()) {
169 v8::ThrowException(v8::Exception::SyntaxError(
170 v8::String::New("Internal error. Array element is missing "
171 "or it isn't a string.")));
172 return;
173 }
174
175 v8::String::AsciiValue ascii_locale_id(locale_id);
176 if (*ascii_locale_id == NULL) {
177 v8::ThrowException(v8::Exception::SyntaxError(
178 v8::String::New("Internal error. Non-ASCII locale identifier.")));
179 return;
180 }
181
182 UErrorCode error = U_ZERO_ERROR;
183
184 // Convert from BCP47 to ICU format.
185 // de-DE-u-co-phonebk -> de_DE@collation=phonebook
186 char icu_locale[ULOC_FULLNAME_CAPACITY];
187 int icu_locale_length = 0;
188 uloc_forLanguageTag(*ascii_locale_id, icu_locale, ULOC_FULLNAME_CAPACITY,
189 &icu_locale_length, &error);
190 if (U_FAILURE(error) || icu_locale_length == 0) {
191 v8::ThrowException(v8::Exception::SyntaxError(
192 v8::String::New("Internal error. Failed to convert locale to ICU.")));
193 return;
194 }
195
196 // Maximize the locale.
197 // de_DE@collation=phonebook -> de_Latn_DE@collation=phonebook
198 char icu_max_locale[ULOC_FULLNAME_CAPACITY];
199 uloc_addLikelySubtags(
200 icu_locale, icu_max_locale, ULOC_FULLNAME_CAPACITY, &error);
201
202 // Remove extensions from maximized locale.
203 // de_Latn_DE@collation=phonebook -> de_Latn_DE
204 char icu_base_max_locale[ULOC_FULLNAME_CAPACITY];
205 uloc_getBaseName(
206 icu_max_locale, icu_base_max_locale, ULOC_FULLNAME_CAPACITY, &error);
207
208 // Get original name without extensions.
209 // de_DE@collation=phonebook -> de_DE
210 char icu_base_locale[ULOC_FULLNAME_CAPACITY];
211 uloc_getBaseName(
212 icu_locale, icu_base_locale, ULOC_FULLNAME_CAPACITY, &error);
213
214 // Convert from ICU locale format to BCP47 format.
215 // de_Latn_DE -> de-Latn-DE
216 char base_max_locale[ULOC_FULLNAME_CAPACITY];
217 uloc_toLanguageTag(icu_base_max_locale, base_max_locale,
218 ULOC_FULLNAME_CAPACITY, FALSE, &error);
219
220 // de_DE -> de-DE
221 char base_locale[ULOC_FULLNAME_CAPACITY];
222 uloc_toLanguageTag(
223 icu_base_locale, base_locale, ULOC_FULLNAME_CAPACITY, FALSE, &error);
224
225 if (U_FAILURE(error)) {
226 v8::ThrowException(v8::Exception::SyntaxError(
227 v8::String::New("Internal error. Couldn't generate maximized "
228 "or base locale.")));
229 return;
230 }
231
232 v8::Handle<v8::Object> result = v8::Object::New();
233 result->Set(v8::String::New("maximized"), v8::String::New(base_max_locale));
234 result->Set(v8::String::New("base"), v8::String::New(base_locale));
235 if (try_catch.HasCaught()) {
236 break;
237 }
238
239 output->Set(i, result);
240 if (try_catch.HasCaught()) {
241 break;
242 }
243 }
244
245 args.GetReturnValue().Set(output);
246 }
247
248 } // namespace v8_i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698