OLD | NEW |
| (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 | |
86 void JSAvailableLocalesOf(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
87 // Expect service name which is a string. | |
88 if (args.Length() != 1 || !args[0]->IsString()) { | |
89 v8::ThrowException(v8::Exception::SyntaxError( | |
90 v8::String::New("Service identifier, as a string, is required."))); | |
91 return; | |
92 } | |
93 | |
94 const icu::Locale* available_locales = NULL; | |
95 | |
96 int32_t count = 0; | |
97 v8::String::AsciiValue service(args[0]->ToString()); | |
98 if (strcmp(*service, "collator") == 0) { | |
99 available_locales = icu::Collator::getAvailableLocales(count); | |
100 } else if (strcmp(*service, "numberformat") == 0) { | |
101 available_locales = icu::NumberFormat::getAvailableLocales(count); | |
102 } else if (strcmp(*service, "dateformat") == 0) { | |
103 available_locales = icu::DateFormat::getAvailableLocales(count); | |
104 } else if (strcmp(*service, "breakiterator") == 0) { | |
105 available_locales = icu::BreakIterator::getAvailableLocales(count); | |
106 } | |
107 | |
108 v8::TryCatch try_catch; | |
109 UErrorCode error = U_ZERO_ERROR; | |
110 char result[ULOC_FULLNAME_CAPACITY]; | |
111 v8::Handle<v8::Object> locales = v8::Object::New(); | |
112 | |
113 for (int32_t i = 0; i < count; ++i) { | |
114 const char* icu_name = available_locales[i].getName(); | |
115 | |
116 error = U_ZERO_ERROR; | |
117 // No need to force strict BCP47 rules. | |
118 uloc_toLanguageTag(icu_name, result, ULOC_FULLNAME_CAPACITY, FALSE, &error); | |
119 if (U_FAILURE(error)) { | |
120 // This shouldn't happen, but lets not break the user. | |
121 continue; | |
122 } | |
123 | |
124 // Index is just a dummy value for the property value. | |
125 locales->Set(v8::String::New(result), v8::Integer::New(i)); | |
126 if (try_catch.HasCaught()) { | |
127 // Ignore error, but stop processing and return. | |
128 break; | |
129 } | |
130 } | |
131 | |
132 args.GetReturnValue().Set(locales); | |
133 } | |
134 | |
135 | |
136 void JSGetDefaultICULocale(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
137 icu::Locale default_locale; | |
138 | |
139 // Set the locale | |
140 char result[ULOC_FULLNAME_CAPACITY]; | |
141 UErrorCode status = U_ZERO_ERROR; | |
142 uloc_toLanguageTag( | |
143 default_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status); | |
144 if (U_SUCCESS(status)) { | |
145 args.GetReturnValue().Set(v8::String::New(result)); | |
146 return; | |
147 } | |
148 | |
149 args.GetReturnValue().Set(v8::String::New("und")); | |
150 } | |
151 | |
152 | |
153 void JSGetLanguageTagVariants(const v8::FunctionCallbackInfo<v8::Value>& args) { | |
154 v8::TryCatch try_catch; | |
155 | |
156 // Expect an array of strings. | |
157 if (args.Length() != 1 || !args[0]->IsArray()) { | |
158 v8::ThrowException(v8::Exception::SyntaxError( | |
159 v8::String::New("Internal error. Expected Array<String>."))); | |
160 return; | |
161 } | |
162 | |
163 v8::Local<v8::Array> input = v8::Local<v8::Array>::Cast(args[0]); | |
164 v8::Handle<v8::Array> output = v8::Array::New(input->Length()); | |
165 for (unsigned int i = 0; i < input->Length(); ++i) { | |
166 v8::Local<v8::Value> locale_id = input->Get(i); | |
167 if (try_catch.HasCaught()) { | |
168 break; | |
169 } | |
170 | |
171 if (!locale_id->IsString()) { | |
172 v8::ThrowException(v8::Exception::SyntaxError( | |
173 v8::String::New("Internal error. Array element is missing " | |
174 "or it isn't a string."))); | |
175 return; | |
176 } | |
177 | |
178 v8::String::AsciiValue ascii_locale_id(locale_id); | |
179 if (*ascii_locale_id == NULL) { | |
180 v8::ThrowException(v8::Exception::SyntaxError( | |
181 v8::String::New("Internal error. Non-ASCII locale identifier."))); | |
182 return; | |
183 } | |
184 | |
185 UErrorCode error = U_ZERO_ERROR; | |
186 | |
187 // Convert from BCP47 to ICU format. | |
188 // de-DE-u-co-phonebk -> de_DE@collation=phonebook | |
189 char icu_locale[ULOC_FULLNAME_CAPACITY]; | |
190 int icu_locale_length = 0; | |
191 uloc_forLanguageTag(*ascii_locale_id, icu_locale, ULOC_FULLNAME_CAPACITY, | |
192 &icu_locale_length, &error); | |
193 if (U_FAILURE(error) || icu_locale_length == 0) { | |
194 v8::ThrowException(v8::Exception::SyntaxError( | |
195 v8::String::New("Internal error. Failed to convert locale to ICU."))); | |
196 return; | |
197 } | |
198 | |
199 // Maximize the locale. | |
200 // de_DE@collation=phonebook -> de_Latn_DE@collation=phonebook | |
201 char icu_max_locale[ULOC_FULLNAME_CAPACITY]; | |
202 uloc_addLikelySubtags( | |
203 icu_locale, icu_max_locale, ULOC_FULLNAME_CAPACITY, &error); | |
204 | |
205 // Remove extensions from maximized locale. | |
206 // de_Latn_DE@collation=phonebook -> de_Latn_DE | |
207 char icu_base_max_locale[ULOC_FULLNAME_CAPACITY]; | |
208 uloc_getBaseName( | |
209 icu_max_locale, icu_base_max_locale, ULOC_FULLNAME_CAPACITY, &error); | |
210 | |
211 // Get original name without extensions. | |
212 // de_DE@collation=phonebook -> de_DE | |
213 char icu_base_locale[ULOC_FULLNAME_CAPACITY]; | |
214 uloc_getBaseName( | |
215 icu_locale, icu_base_locale, ULOC_FULLNAME_CAPACITY, &error); | |
216 | |
217 // Convert from ICU locale format to BCP47 format. | |
218 // de_Latn_DE -> de-Latn-DE | |
219 char base_max_locale[ULOC_FULLNAME_CAPACITY]; | |
220 uloc_toLanguageTag(icu_base_max_locale, base_max_locale, | |
221 ULOC_FULLNAME_CAPACITY, FALSE, &error); | |
222 | |
223 // de_DE -> de-DE | |
224 char base_locale[ULOC_FULLNAME_CAPACITY]; | |
225 uloc_toLanguageTag( | |
226 icu_base_locale, base_locale, ULOC_FULLNAME_CAPACITY, FALSE, &error); | |
227 | |
228 if (U_FAILURE(error)) { | |
229 v8::ThrowException(v8::Exception::SyntaxError( | |
230 v8::String::New("Internal error. Couldn't generate maximized " | |
231 "or base locale."))); | |
232 return; | |
233 } | |
234 | |
235 v8::Handle<v8::Object> result = v8::Object::New(); | |
236 result->Set(v8::String::New("maximized"), v8::String::New(base_max_locale)); | |
237 result->Set(v8::String::New("base"), v8::String::New(base_locale)); | |
238 if (try_catch.HasCaught()) { | |
239 break; | |
240 } | |
241 | |
242 output->Set(i, result); | |
243 if (try_catch.HasCaught()) { | |
244 break; | |
245 } | |
246 } | |
247 | |
248 args.GetReturnValue().Set(output); | |
249 } | |
250 | |
251 } // namespace v8_i18n | |
OLD | NEW |