1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 // limitations under the License.
8 #include "unicode/brkiter.h"
9 #include "unicode/calendar.h"
10 #include "unicode/coll.h"
11 #include "unicode/curramt.h"
12 #include "unicode/dcfmtsym.h"
13 #include "unicode/decimfmt.h"
14 #include "unicode/dtfmtsym.h"
15 #include "unicode/dtptngen.h"
16 #include "unicode/locid.h"
17 #include "unicode/numfmt.h"
18 #include "unicode/numsys.h"
19 #include "unicode/rbbi.h"
20 #include "unicode/smpdtfmt.h"
21 #include "unicode/timezone.h"
22 #include "unicode/uchar.h"
23 #include "unicode/ucol.h"
24 #include "unicode/ucurr.h"
25 #include "unicode/unum.h"
26 #include "unicode/uversion.h"
33 bool ExtractStringSetting(Isolate* isolate,
34 Handle<JSObject> options,
36 icu::UnicodeString* setting) {
37 Handle<String> str = isolate->factory()->NewStringFromAsciiChecked(key);
38 Handle<Object> object = Object::GetProperty(options, str).ToHandleChecked();
39 if (object->IsString()) {
40 v8::String::Utf8Value utf8_string(
41 v8::Utils::ToLocal(Handle<String>::cast(object)));
42 *setting = icu::UnicodeString::fromUTF8(*utf8_string);
49 bool ExtractIntegerSetting(Isolate* isolate,
50 Handle<JSObject> options,
53 Handle<String> str = isolate->factory()->NewStringFromAsciiChecked(key);
54 Handle<Object> object = Object::GetProperty(options, str).ToHandleChecked();
55 if (object->IsNumber()) {
56 object->ToInt32(value);
63 bool ExtractBooleanSetting(Isolate* isolate,
64 Handle<JSObject> options,
67 Handle<String> str = isolate->factory()->NewStringFromAsciiChecked(key);
68 Handle<Object> object = Object::GetProperty(options, str).ToHandleChecked();
69 if (object->IsBoolean()) {
70 *value = object->BooleanValue();
77 icu::SimpleDateFormat* CreateICUDateFormat(
79 const icu::Locale& icu_locale,
80 Handle<JSObject> options) {
81 // Create time zone as specified by the user. We have to re-create time zone
82 // since calendar takes ownership.
83 icu::TimeZone* tz = NULL;
84 icu::UnicodeString timezone;
85 if (ExtractStringSetting(isolate, options, "timeZone", &timezone)) {
86 tz = icu::TimeZone::createTimeZone(timezone);
88 tz = icu::TimeZone::createDefault();
91 // Create a calendar using locale, and apply time zone to it.
92 UErrorCode status = U_ZERO_ERROR;
93 icu::Calendar* calendar =
94 icu::Calendar::createInstance(tz, icu_locale, status);
96 // Make formatter from skeleton. Calendar and numbering system are added
97 // to the locale as Unicode extension (if they were specified at all).
98 icu::SimpleDateFormat* date_format = NULL;
99 icu::UnicodeString skeleton;
100 if (ExtractStringSetting(isolate, options, "skeleton", &skeleton)) {
101 icu::DateTimePatternGenerator* generator =
102 icu::DateTimePatternGenerator::createInstance(icu_locale, status);
103 icu::UnicodeString pattern;
104 if (U_SUCCESS(status)) {
105 pattern = generator->getBestPattern(skeleton, status);
109 date_format = new icu::SimpleDateFormat(pattern, icu_locale, status);
110 if (U_SUCCESS(status)) {
111 date_format->adoptCalendar(calendar);
115 if (U_FAILURE(status)) {
125 void SetResolvedDateSettings(Isolate* isolate,
126 const icu::Locale& icu_locale,
127 icu::SimpleDateFormat* date_format,
128 Handle<JSObject> resolved) {
129 Factory* factory = isolate->factory();
130 UErrorCode status = U_ZERO_ERROR;
131 icu::UnicodeString pattern;
132 date_format->toPattern(pattern);
133 JSObject::SetProperty(
135 factory->NewStringFromStaticAscii("pattern"),
136 factory->NewStringFromTwoByte(
137 Vector<const uint16_t>(
138 reinterpret_cast<const uint16_t*>(pattern.getBuffer()),
139 pattern.length())).ToHandleChecked(),
143 // Set time zone and calendar.
144 const icu::Calendar* calendar = date_format->getCalendar();
145 const char* calendar_name = calendar->getType();
146 JSObject::SetProperty(
148 factory->NewStringFromStaticAscii("calendar"),
149 factory->NewStringFromAsciiChecked(calendar_name),
153 const icu::TimeZone& tz = calendar->getTimeZone();
154 icu::UnicodeString time_zone;
157 icu::UnicodeString canonical_time_zone;
158 icu::TimeZone::getCanonicalID(time_zone, canonical_time_zone, status);
159 if (U_SUCCESS(status)) {
160 if (canonical_time_zone == UNICODE_STRING_SIMPLE("Etc/GMT")) {
161 JSObject::SetProperty(
163 factory->NewStringFromStaticAscii("timeZone"),
164 factory->NewStringFromStaticAscii("UTC"),
168 JSObject::SetProperty(
170 factory->NewStringFromStaticAscii("timeZone"),
171 factory->NewStringFromTwoByte(
172 Vector<const uint16_t>(
173 reinterpret_cast<const uint16_t*>(
174 canonical_time_zone.getBuffer()),
175 canonical_time_zone.length())).ToHandleChecked(),
181 // Ugly hack. ICU doesn't expose numbering system in any way, so we have
182 // to assume that for given locale NumberingSystem constructor produces the
183 // same digits as NumberFormat/Calendar would.
184 status = U_ZERO_ERROR;
185 icu::NumberingSystem* numbering_system =
186 icu::NumberingSystem::createInstance(icu_locale, status);
187 if (U_SUCCESS(status)) {
188 const char* ns = numbering_system->getName();
189 JSObject::SetProperty(
191 factory->NewStringFromStaticAscii("numberingSystem"),
192 factory->NewStringFromAsciiChecked(ns),
196 JSObject::SetProperty(
198 factory->NewStringFromStaticAscii("numberingSystem"),
199 factory->undefined_value(),
203 delete numbering_system;
206 char result[ULOC_FULLNAME_CAPACITY];
207 status = U_ZERO_ERROR;
209 icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status);
210 if (U_SUCCESS(status)) {
211 JSObject::SetProperty(
213 factory->NewStringFromStaticAscii("locale"),
214 factory->NewStringFromAsciiChecked(result),
218 // This would never happen, since we got the locale from ICU.
219 JSObject::SetProperty(
221 factory->NewStringFromStaticAscii("locale"),
222 factory->NewStringFromStaticAscii("und"),
229 template<int internal_fields, EternalHandles::SingletonHandle field>
230 Handle<ObjectTemplateInfo> GetEternal(Isolate* isolate) {
231 if (isolate->eternal_handles()->Exists(field)) {
232 return Handle<ObjectTemplateInfo>::cast(
233 isolate->eternal_handles()->GetSingleton(field));
235 v8::Local<v8::ObjectTemplate> raw_template =
236 v8::ObjectTemplate::New(reinterpret_cast<v8::Isolate*>(isolate));
237 raw_template->SetInternalFieldCount(internal_fields);
238 return Handle<ObjectTemplateInfo>::cast(
239 isolate->eternal_handles()->CreateSingleton(
241 *v8::Utils::OpenHandle(*raw_template),
246 icu::DecimalFormat* CreateICUNumberFormat(
248 const icu::Locale& icu_locale,
249 Handle<JSObject> options) {
250 // Make formatter from options. Numbering system is added
251 // to the locale as Unicode extension (if it was specified at all).
252 UErrorCode status = U_ZERO_ERROR;
253 icu::DecimalFormat* number_format = NULL;
254 icu::UnicodeString style;
255 icu::UnicodeString currency;
256 if (ExtractStringSetting(isolate, options, "style", &style)) {
257 if (style == UNICODE_STRING_SIMPLE("currency")) {
258 icu::UnicodeString display;
259 ExtractStringSetting(isolate, options, "currency", ¤cy);
260 ExtractStringSetting(isolate, options, "currencyDisplay", &display);
262 #if (U_ICU_VERSION_MAJOR_NUM == 4) && (U_ICU_VERSION_MINOR_NUM <= 6)
263 icu::NumberFormat::EStyles format_style;
264 if (display == UNICODE_STRING_SIMPLE("code")) {
265 format_style = icu::NumberFormat::kIsoCurrencyStyle;
266 } else if (display == UNICODE_STRING_SIMPLE("name")) {
267 format_style = icu::NumberFormat::kPluralCurrencyStyle;
269 format_style = icu::NumberFormat::kCurrencyStyle;
271 #else // ICU version is 4.8 or above (we ignore versions below 4.0).
272 UNumberFormatStyle format_style;
273 if (display == UNICODE_STRING_SIMPLE("code")) {
274 format_style = UNUM_CURRENCY_ISO;
275 } else if (display == UNICODE_STRING_SIMPLE("name")) {
276 format_style = UNUM_CURRENCY_PLURAL;
278 format_style = UNUM_CURRENCY;
282 number_format = static_cast<icu::DecimalFormat*>(
283 icu::NumberFormat::createInstance(icu_locale, format_style, status));
284 } else if (style == UNICODE_STRING_SIMPLE("percent")) {
285 number_format = static_cast<icu::DecimalFormat*>(
286 icu::NumberFormat::createPercentInstance(icu_locale, status));
287 if (U_FAILURE(status)) {
288 delete number_format;
291 // Make sure 1.1% doesn't go into 2%.
292 number_format->setMinimumFractionDigits(1);
294 // Make a decimal instance by default.
295 number_format = static_cast<icu::DecimalFormat*>(
296 icu::NumberFormat::createInstance(icu_locale, status));
300 if (U_FAILURE(status)) {
301 delete number_format;
306 if (!currency.isEmpty()) {
307 number_format->setCurrency(currency.getBuffer(), status);
311 if (ExtractIntegerSetting(
312 isolate, options, "minimumIntegerDigits", &digits)) {
313 number_format->setMinimumIntegerDigits(digits);
316 if (ExtractIntegerSetting(
317 isolate, options, "minimumFractionDigits", &digits)) {
318 number_format->setMinimumFractionDigits(digits);
321 if (ExtractIntegerSetting(
322 isolate, options, "maximumFractionDigits", &digits)) {
323 number_format->setMaximumFractionDigits(digits);
326 bool significant_digits_used = false;
327 if (ExtractIntegerSetting(
328 isolate, options, "minimumSignificantDigits", &digits)) {
329 number_format->setMinimumSignificantDigits(digits);
330 significant_digits_used = true;
333 if (ExtractIntegerSetting(
334 isolate, options, "maximumSignificantDigits", &digits)) {
335 number_format->setMaximumSignificantDigits(digits);
336 significant_digits_used = true;
339 number_format->setSignificantDigitsUsed(significant_digits_used);
342 if (ExtractBooleanSetting(isolate, options, "useGrouping", &grouping)) {
343 number_format->setGroupingUsed(grouping);
346 // Set rounding mode.
347 number_format->setRoundingMode(icu::DecimalFormat::kRoundHalfUp);
349 return number_format;
353 void SetResolvedNumberSettings(Isolate* isolate,
354 const icu::Locale& icu_locale,
355 icu::DecimalFormat* number_format,
356 Handle<JSObject> resolved) {
357 Factory* factory = isolate->factory();
358 icu::UnicodeString pattern;
359 number_format->toPattern(pattern);
360 JSObject::SetProperty(
362 factory->NewStringFromStaticAscii("pattern"),
363 factory->NewStringFromTwoByte(
364 Vector<const uint16_t>(
365 reinterpret_cast<const uint16_t*>(pattern.getBuffer()),
366 pattern.length())).ToHandleChecked(),
370 // Set resolved currency code in options.currency if not empty.
371 icu::UnicodeString currency(number_format->getCurrency());
372 if (!currency.isEmpty()) {
373 JSObject::SetProperty(
375 factory->NewStringFromStaticAscii("currency"),
376 factory->NewStringFromTwoByte(
377 Vector<const uint16_t>(
378 reinterpret_cast<const uint16_t*>(currency.getBuffer()),
379 currency.length())).ToHandleChecked(),
384 // Ugly hack. ICU doesn't expose numbering system in any way, so we have
385 // to assume that for given locale NumberingSystem constructor produces the
386 // same digits as NumberFormat/Calendar would.
387 UErrorCode status = U_ZERO_ERROR;
388 icu::NumberingSystem* numbering_system =
389 icu::NumberingSystem::createInstance(icu_locale, status);
390 if (U_SUCCESS(status)) {
391 const char* ns = numbering_system->getName();
392 JSObject::SetProperty(
394 factory->NewStringFromStaticAscii("numberingSystem"),
395 factory->NewStringFromAsciiChecked(ns),
399 JSObject::SetProperty(
401 factory->NewStringFromStaticAscii("numberingSystem"),
402 factory->undefined_value(),
406 delete numbering_system;
408 JSObject::SetProperty(
410 factory->NewStringFromStaticAscii("useGrouping"),
411 factory->ToBoolean(number_format->isGroupingUsed()),
415 JSObject::SetProperty(
417 factory->NewStringFromStaticAscii("minimumIntegerDigits"),
418 factory->NewNumberFromInt(number_format->getMinimumIntegerDigits()),
422 JSObject::SetProperty(
424 factory->NewStringFromStaticAscii("minimumFractionDigits"),
425 factory->NewNumberFromInt(number_format->getMinimumFractionDigits()),
429 JSObject::SetProperty(
431 factory->NewStringFromStaticAscii("maximumFractionDigits"),
432 factory->NewNumberFromInt(number_format->getMaximumFractionDigits()),
437 factory->NewStringFromStaticAscii("minimumSignificantDigits");
438 if (JSReceiver::HasLocalProperty(resolved, key)) {
439 JSObject::SetProperty(
441 factory->NewStringFromStaticAscii("minimumSignificantDigits"),
442 factory->NewNumberFromInt(number_format->getMinimumSignificantDigits()),
447 key = factory->NewStringFromStaticAscii("maximumSignificantDigits");
448 if (JSReceiver::HasLocalProperty(resolved, key)) {
449 JSObject::SetProperty(
451 factory->NewStringFromStaticAscii("maximumSignificantDigits"),
452 factory->NewNumberFromInt(number_format->getMaximumSignificantDigits()),
458 char result[ULOC_FULLNAME_CAPACITY];
459 status = U_ZERO_ERROR;
461 icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status);
462 if (U_SUCCESS(status)) {
463 JSObject::SetProperty(
465 factory->NewStringFromStaticAscii("locale"),
466 factory->NewStringFromAsciiChecked(result),
470 // This would never happen, since we got the locale from ICU.
471 JSObject::SetProperty(
473 factory->NewStringFromStaticAscii("locale"),
474 factory->NewStringFromStaticAscii("und"),
481 icu::Collator* CreateICUCollator(
483 const icu::Locale& icu_locale,
484 Handle<JSObject> options) {
485 // Make collator from options.
486 icu::Collator* collator = NULL;
487 UErrorCode status = U_ZERO_ERROR;
488 collator = icu::Collator::createInstance(icu_locale, status);
490 if (U_FAILURE(status)) {
495 // Set flags first, and then override them with sensitivity if necessary.
497 if (ExtractBooleanSetting(isolate, options, "numeric", &numeric)) {
498 collator->setAttribute(
499 UCOL_NUMERIC_COLLATION, numeric ? UCOL_ON : UCOL_OFF, status);
502 // Normalization is always on, by the spec. We are free to optimize
503 // if the strings are already normalized (but we don't have a way to tell
505 collator->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
507 icu::UnicodeString case_first;
508 if (ExtractStringSetting(isolate, options, "caseFirst", &case_first)) {
509 if (case_first == UNICODE_STRING_SIMPLE("upper")) {
510 collator->setAttribute(UCOL_CASE_FIRST, UCOL_UPPER_FIRST, status);
511 } else if (case_first == UNICODE_STRING_SIMPLE("lower")) {
512 collator->setAttribute(UCOL_CASE_FIRST, UCOL_LOWER_FIRST, status);
514 // Default (false/off).
515 collator->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status);
519 icu::UnicodeString sensitivity;
520 if (ExtractStringSetting(isolate, options, "sensitivity", &sensitivity)) {
521 if (sensitivity == UNICODE_STRING_SIMPLE("base")) {
522 collator->setStrength(icu::Collator::PRIMARY);
523 } else if (sensitivity == UNICODE_STRING_SIMPLE("accent")) {
524 collator->setStrength(icu::Collator::SECONDARY);
525 } else if (sensitivity == UNICODE_STRING_SIMPLE("case")) {
526 collator->setStrength(icu::Collator::PRIMARY);
527 collator->setAttribute(UCOL_CASE_LEVEL, UCOL_ON, status);
530 collator->setStrength(icu::Collator::TERTIARY);
535 if (ExtractBooleanSetting(isolate, options, "ignorePunctuation", &ignore)) {
537 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status);
545 void SetResolvedCollatorSettings(Isolate* isolate,
546 const icu::Locale& icu_locale,
547 icu::Collator* collator,
548 Handle<JSObject> resolved) {
549 Factory* factory = isolate->factory();
550 UErrorCode status = U_ZERO_ERROR;
552 JSObject::SetProperty(
554 factory->NewStringFromStaticAscii("numeric"),
556 collator->getAttribute(UCOL_NUMERIC_COLLATION, status) == UCOL_ON),
560 switch (collator->getAttribute(UCOL_CASE_FIRST, status)) {
561 case UCOL_LOWER_FIRST:
562 JSObject::SetProperty(
564 factory->NewStringFromStaticAscii("caseFirst"),
565 factory->NewStringFromStaticAscii("lower"),
569 case UCOL_UPPER_FIRST:
570 JSObject::SetProperty(
572 factory->NewStringFromStaticAscii("caseFirst"),
573 factory->NewStringFromStaticAscii("upper"),
578 JSObject::SetProperty(
580 factory->NewStringFromStaticAscii("caseFirst"),
581 factory->NewStringFromStaticAscii("false"),
586 switch (collator->getAttribute(UCOL_STRENGTH, status)) {
588 JSObject::SetProperty(
590 factory->NewStringFromStaticAscii("strength"),
591 factory->NewStringFromStaticAscii("primary"),
595 // case level: true + s1 -> case, s1 -> base.
596 if (UCOL_ON == collator->getAttribute(UCOL_CASE_LEVEL, status)) {
597 JSObject::SetProperty(
599 factory->NewStringFromStaticAscii("sensitivity"),
600 factory->NewStringFromStaticAscii("case"),
604 JSObject::SetProperty(
606 factory->NewStringFromStaticAscii("sensitivity"),
607 factory->NewStringFromStaticAscii("base"),
614 JSObject::SetProperty(
616 factory->NewStringFromStaticAscii("strength"),
617 factory->NewStringFromStaticAscii("secondary"),
620 JSObject::SetProperty(
622 factory->NewStringFromStaticAscii("sensitivity"),
623 factory->NewStringFromStaticAscii("accent"),
628 JSObject::SetProperty(
630 factory->NewStringFromStaticAscii("strength"),
631 factory->NewStringFromStaticAscii("tertiary"),
634 JSObject::SetProperty(
636 factory->NewStringFromStaticAscii("sensitivity"),
637 factory->NewStringFromStaticAscii("variant"),
641 case UCOL_QUATERNARY:
642 // We shouldn't get quaternary and identical from ICU, but if we do
643 // put them into variant.
644 JSObject::SetProperty(
646 factory->NewStringFromStaticAscii("strength"),
647 factory->NewStringFromStaticAscii("quaternary"),
650 JSObject::SetProperty(
652 factory->NewStringFromStaticAscii("sensitivity"),
653 factory->NewStringFromStaticAscii("variant"),
658 JSObject::SetProperty(
660 factory->NewStringFromStaticAscii("strength"),
661 factory->NewStringFromStaticAscii("identical"),
664 JSObject::SetProperty(
666 factory->NewStringFromStaticAscii("sensitivity"),
667 factory->NewStringFromStaticAscii("variant"),
672 JSObject::SetProperty(
674 factory->NewStringFromStaticAscii("ignorePunctuation"),
675 factory->ToBoolean(collator->getAttribute(
676 UCOL_ALTERNATE_HANDLING, status) == UCOL_SHIFTED),
681 char result[ULOC_FULLNAME_CAPACITY];
682 status = U_ZERO_ERROR;
684 icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status);
685 if (U_SUCCESS(status)) {
686 JSObject::SetProperty(
688 factory->NewStringFromStaticAscii("locale"),
689 factory->NewStringFromAsciiChecked(result),
693 // This would never happen, since we got the locale from ICU.
694 JSObject::SetProperty(
696 factory->NewStringFromStaticAscii("locale"),
697 factory->NewStringFromStaticAscii("und"),
704 icu::BreakIterator* CreateICUBreakIterator(
706 const icu::Locale& icu_locale,
707 Handle<JSObject> options) {
708 UErrorCode status = U_ZERO_ERROR;
709 icu::BreakIterator* break_iterator = NULL;
710 icu::UnicodeString type;
711 if (!ExtractStringSetting(isolate, options, "type", &type)) return NULL;
713 if (type == UNICODE_STRING_SIMPLE("character")) {
715 icu::BreakIterator::createCharacterInstance(icu_locale, status);
716 } else if (type == UNICODE_STRING_SIMPLE("sentence")) {
718 icu::BreakIterator::createSentenceInstance(icu_locale, status);
719 } else if (type == UNICODE_STRING_SIMPLE("line")) {
721 icu::BreakIterator::createLineInstance(icu_locale, status);
723 // Defualt is word iterator.
725 icu::BreakIterator::createWordInstance(icu_locale, status);
728 if (U_FAILURE(status)) {
729 delete break_iterator;
733 return break_iterator;
737 void SetResolvedBreakIteratorSettings(Isolate* isolate,
738 const icu::Locale& icu_locale,
739 icu::BreakIterator* break_iterator,
740 Handle<JSObject> resolved) {
741 Factory* factory = isolate->factory();
742 UErrorCode status = U_ZERO_ERROR;
745 char result[ULOC_FULLNAME_CAPACITY];
746 status = U_ZERO_ERROR;
748 icu_locale.getName(), result, ULOC_FULLNAME_CAPACITY, FALSE, &status);
749 if (U_SUCCESS(status)) {
750 JSObject::SetProperty(
752 factory->NewStringFromStaticAscii("locale"),
753 factory->NewStringFromAsciiChecked(result),
757 // This would never happen, since we got the locale from ICU.
758 JSObject::SetProperty(
760 factory->NewStringFromStaticAscii("locale"),
761 factory->NewStringFromStaticAscii("und"),
771 Handle<ObjectTemplateInfo> I18N::GetTemplate(Isolate* isolate) {
772 return GetEternal<1, i::EternalHandles::I18N_TEMPLATE_ONE>(isolate);
777 Handle<ObjectTemplateInfo> I18N::GetTemplate2(Isolate* isolate) {
778 return GetEternal<2, i::EternalHandles::I18N_TEMPLATE_TWO>(isolate);
783 icu::SimpleDateFormat* DateFormat::InitializeDateTimeFormat(
785 Handle<String> locale,
786 Handle<JSObject> options,
787 Handle<JSObject> resolved) {
788 // Convert BCP47 into ICU locale format.
789 UErrorCode status = U_ZERO_ERROR;
790 icu::Locale icu_locale;
791 char icu_result[ULOC_FULLNAME_CAPACITY];
793 v8::String::Utf8Value bcp47_locale(v8::Utils::ToLocal(locale));
794 if (bcp47_locale.length() != 0) {
795 uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY,
796 &icu_length, &status);
797 if (U_FAILURE(status) || icu_length == 0) {
800 icu_locale = icu::Locale(icu_result);
803 icu::SimpleDateFormat* date_format = CreateICUDateFormat(
804 isolate, icu_locale, options);
806 // Remove extensions and try again.
807 icu::Locale no_extension_locale(icu_locale.getBaseName());
808 date_format = CreateICUDateFormat(isolate, no_extension_locale, options);
810 // Set resolved settings (pattern, numbering system, calendar).
811 SetResolvedDateSettings(
812 isolate, no_extension_locale, date_format, resolved);
814 SetResolvedDateSettings(isolate, icu_locale, date_format, resolved);
821 icu::SimpleDateFormat* DateFormat::UnpackDateFormat(
823 Handle<JSObject> obj) {
825 isolate->factory()->NewStringFromStaticAscii("dateFormat");
826 if (JSReceiver::HasLocalProperty(obj, key)) {
827 return reinterpret_cast<icu::SimpleDateFormat*>(
828 obj->GetInternalField(0));
836 void DeleteNativeObjectAt(const v8::WeakCallbackData<v8::Value, void>& data,
838 v8::Local<v8::Object> obj = v8::Handle<v8::Object>::Cast(data.GetValue());
839 delete reinterpret_cast<T*>(obj->GetAlignedPointerFromInternalField(index));
843 static void DestroyGlobalHandle(
844 const v8::WeakCallbackData<v8::Value, void>& data) {
845 GlobalHandles::Destroy(reinterpret_cast<Object**>(data.GetParameter()));
849 void DateFormat::DeleteDateFormat(
850 const v8::WeakCallbackData<v8::Value, void>& data) {
851 DeleteNativeObjectAt<icu::SimpleDateFormat>(data, 0);
852 DestroyGlobalHandle(data);
856 icu::DecimalFormat* NumberFormat::InitializeNumberFormat(
858 Handle<String> locale,
859 Handle<JSObject> options,
860 Handle<JSObject> resolved) {
861 // Convert BCP47 into ICU locale format.
862 UErrorCode status = U_ZERO_ERROR;
863 icu::Locale icu_locale;
864 char icu_result[ULOC_FULLNAME_CAPACITY];
866 v8::String::Utf8Value bcp47_locale(v8::Utils::ToLocal(locale));
867 if (bcp47_locale.length() != 0) {
868 uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY,
869 &icu_length, &status);
870 if (U_FAILURE(status) || icu_length == 0) {
873 icu_locale = icu::Locale(icu_result);
876 icu::DecimalFormat* number_format =
877 CreateICUNumberFormat(isolate, icu_locale, options);
878 if (!number_format) {
879 // Remove extensions and try again.
880 icu::Locale no_extension_locale(icu_locale.getBaseName());
881 number_format = CreateICUNumberFormat(
882 isolate, no_extension_locale, options);
884 // Set resolved settings (pattern, numbering system).
885 SetResolvedNumberSettings(
886 isolate, no_extension_locale, number_format, resolved);
888 SetResolvedNumberSettings(isolate, icu_locale, number_format, resolved);
891 return number_format;
895 icu::DecimalFormat* NumberFormat::UnpackNumberFormat(
897 Handle<JSObject> obj) {
899 isolate->factory()->NewStringFromStaticAscii("numberFormat");
900 if (JSReceiver::HasLocalProperty(obj, key)) {
901 return reinterpret_cast<icu::DecimalFormat*>(obj->GetInternalField(0));
908 void NumberFormat::DeleteNumberFormat(
909 const v8::WeakCallbackData<v8::Value, void>& data) {
910 DeleteNativeObjectAt<icu::DecimalFormat>(data, 0);
911 DestroyGlobalHandle(data);
915 icu::Collator* Collator::InitializeCollator(
917 Handle<String> locale,
918 Handle<JSObject> options,
919 Handle<JSObject> resolved) {
920 // Convert BCP47 into ICU locale format.
921 UErrorCode status = U_ZERO_ERROR;
922 icu::Locale icu_locale;
923 char icu_result[ULOC_FULLNAME_CAPACITY];
925 v8::String::Utf8Value bcp47_locale(v8::Utils::ToLocal(locale));
926 if (bcp47_locale.length() != 0) {
927 uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY,
928 &icu_length, &status);
929 if (U_FAILURE(status) || icu_length == 0) {
932 icu_locale = icu::Locale(icu_result);
935 icu::Collator* collator = CreateICUCollator(isolate, icu_locale, options);
937 // Remove extensions and try again.
938 icu::Locale no_extension_locale(icu_locale.getBaseName());
939 collator = CreateICUCollator(isolate, no_extension_locale, options);
941 // Set resolved settings (pattern, numbering system).
942 SetResolvedCollatorSettings(
943 isolate, no_extension_locale, collator, resolved);
945 SetResolvedCollatorSettings(isolate, icu_locale, collator, resolved);
952 icu::Collator* Collator::UnpackCollator(Isolate* isolate,
953 Handle<JSObject> obj) {
954 Handle<String> key = isolate->factory()->NewStringFromStaticAscii("collator");
955 if (JSReceiver::HasLocalProperty(obj, key)) {
956 return reinterpret_cast<icu::Collator*>(obj->GetInternalField(0));
963 void Collator::DeleteCollator(
964 const v8::WeakCallbackData<v8::Value, void>& data) {
965 DeleteNativeObjectAt<icu::Collator>(data, 0);
966 DestroyGlobalHandle(data);
970 icu::BreakIterator* BreakIterator::InitializeBreakIterator(
972 Handle<String> locale,
973 Handle<JSObject> options,
974 Handle<JSObject> resolved) {
975 // Convert BCP47 into ICU locale format.
976 UErrorCode status = U_ZERO_ERROR;
977 icu::Locale icu_locale;
978 char icu_result[ULOC_FULLNAME_CAPACITY];
980 v8::String::Utf8Value bcp47_locale(v8::Utils::ToLocal(locale));
981 if (bcp47_locale.length() != 0) {
982 uloc_forLanguageTag(*bcp47_locale, icu_result, ULOC_FULLNAME_CAPACITY,
983 &icu_length, &status);
984 if (U_FAILURE(status) || icu_length == 0) {
987 icu_locale = icu::Locale(icu_result);
990 icu::BreakIterator* break_iterator = CreateICUBreakIterator(
991 isolate, icu_locale, options);
992 if (!break_iterator) {
993 // Remove extensions and try again.
994 icu::Locale no_extension_locale(icu_locale.getBaseName());
995 break_iterator = CreateICUBreakIterator(
996 isolate, no_extension_locale, options);
998 // Set resolved settings (locale).
999 SetResolvedBreakIteratorSettings(
1000 isolate, no_extension_locale, break_iterator, resolved);
1002 SetResolvedBreakIteratorSettings(
1003 isolate, icu_locale, break_iterator, resolved);
1006 return break_iterator;
1010 icu::BreakIterator* BreakIterator::UnpackBreakIterator(Isolate* isolate,
1011 Handle<JSObject> obj) {
1012 Handle<String> key =
1013 isolate->factory()->NewStringFromStaticAscii("breakIterator");
1014 if (JSReceiver::HasLocalProperty(obj, key)) {
1015 return reinterpret_cast<icu::BreakIterator*>(obj->GetInternalField(0));
1022 void BreakIterator::DeleteBreakIterator(
1023 const v8::WeakCallbackData<v8::Value, void>& data) {
1024 DeleteNativeObjectAt<icu::BreakIterator>(data, 0);
1025 DeleteNativeObjectAt<icu::UnicodeString>(data, 1);
1026 DestroyGlobalHandle(data);
1029 } } // namespace v8::internal