1/* 2 * Copyright (C) 2011,2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32#include "LocaleICU.h" 33 34#include "LocalizedStrings.h" 35#include <limits> 36#include <unicode/udatpg.h> 37#include <unicode/uloc.h> 38#include <wtf/DateMath.h> 39#include <wtf/PassOwnPtr.h> 40#include <wtf/text/StringBuilder.h> 41 42using namespace icu; 43using namespace std; 44 45namespace WebCore { 46 47PassOwnPtr<Locale> Locale::create(const AtomicString& locale) 48{ 49 return LocaleICU::create(locale.string().utf8().data()); 50} 51 52LocaleICU::LocaleICU(const char* locale) 53 : m_locale(locale) 54 , m_numberFormat(0) 55 , m_shortDateFormat(0) 56 , m_didCreateDecimalFormat(false) 57 , m_didCreateShortDateFormat(false) 58#if ENABLE(DATE_AND_TIME_INPUT_TYPES) 59 , m_mediumTimeFormat(0) 60 , m_shortTimeFormat(0) 61 , m_didCreateTimeFormat(false) 62#endif 63{ 64} 65 66LocaleICU::~LocaleICU() 67{ 68 unum_close(m_numberFormat); 69 udat_close(m_shortDateFormat); 70#if ENABLE(DATE_AND_TIME_INPUT_TYPES) 71 udat_close(m_mediumTimeFormat); 72 udat_close(m_shortTimeFormat); 73#endif 74} 75 76PassOwnPtr<LocaleICU> LocaleICU::create(const char* localeString) 77{ 78 return adoptPtr(new LocaleICU(localeString)); 79} 80 81String LocaleICU::decimalSymbol(UNumberFormatSymbol symbol) 82{ 83 UErrorCode status = U_ZERO_ERROR; 84 int32_t bufferLength = unum_getSymbol(m_numberFormat, symbol, 0, 0, &status); 85 ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR); 86 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) 87 return String(); 88 Vector<UChar> buffer(bufferLength); 89 status = U_ZERO_ERROR; 90 unum_getSymbol(m_numberFormat, symbol, buffer.data(), bufferLength, &status); 91 if (U_FAILURE(status)) 92 return String(); 93 return String::adopt(buffer); 94} 95 96String LocaleICU::decimalTextAttribute(UNumberFormatTextAttribute tag) 97{ 98 UErrorCode status = U_ZERO_ERROR; 99 int32_t bufferLength = unum_getTextAttribute(m_numberFormat, tag, 0, 0, &status); 100 ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR); 101 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR) 102 return String(); 103 Vector<UChar> buffer(bufferLength); 104 status = U_ZERO_ERROR; 105 unum_getTextAttribute(m_numberFormat, tag, buffer.data(), bufferLength, &status); 106 ASSERT(U_SUCCESS(status)); 107 if (U_FAILURE(status)) 108 return String(); 109 return String::adopt(buffer); 110} 111 112void LocaleICU::initializeLocaleData() 113{ 114 if (m_didCreateDecimalFormat) 115 return; 116 m_didCreateDecimalFormat = true; 117 UErrorCode status = U_ZERO_ERROR; 118 m_numberFormat = unum_open(UNUM_DECIMAL, 0, 0, m_locale.data(), 0, &status); 119 if (!U_SUCCESS(status)) 120 return; 121 122 Vector<String, DecimalSymbolsSize> symbols; 123 symbols.append(decimalSymbol(UNUM_ZERO_DIGIT_SYMBOL)); 124 symbols.append(decimalSymbol(UNUM_ONE_DIGIT_SYMBOL)); 125 symbols.append(decimalSymbol(UNUM_TWO_DIGIT_SYMBOL)); 126 symbols.append(decimalSymbol(UNUM_THREE_DIGIT_SYMBOL)); 127 symbols.append(decimalSymbol(UNUM_FOUR_DIGIT_SYMBOL)); 128 symbols.append(decimalSymbol(UNUM_FIVE_DIGIT_SYMBOL)); 129 symbols.append(decimalSymbol(UNUM_SIX_DIGIT_SYMBOL)); 130 symbols.append(decimalSymbol(UNUM_SEVEN_DIGIT_SYMBOL)); 131 symbols.append(decimalSymbol(UNUM_EIGHT_DIGIT_SYMBOL)); 132 symbols.append(decimalSymbol(UNUM_NINE_DIGIT_SYMBOL)); 133 symbols.append(decimalSymbol(UNUM_DECIMAL_SEPARATOR_SYMBOL)); 134 symbols.append(decimalSymbol(UNUM_GROUPING_SEPARATOR_SYMBOL)); 135 ASSERT(symbols.size() == DecimalSymbolsSize); 136 setLocaleData(symbols, decimalTextAttribute(UNUM_POSITIVE_PREFIX), decimalTextAttribute(UNUM_POSITIVE_SUFFIX), decimalTextAttribute(UNUM_NEGATIVE_PREFIX), decimalTextAttribute(UNUM_NEGATIVE_SUFFIX)); 137} 138 139bool LocaleICU::initializeShortDateFormat() 140{ 141 if (m_didCreateShortDateFormat) 142 return m_shortDateFormat; 143 m_shortDateFormat = openDateFormat(UDAT_NONE, UDAT_SHORT); 144 m_didCreateShortDateFormat = true; 145 return m_shortDateFormat; 146} 147 148UDateFormat* LocaleICU::openDateFormat(UDateFormatStyle timeStyle, UDateFormatStyle dateStyle) const 149{ 150 const UChar gmtTimezone[3] = {'G', 'M', 'T'}; 151 UErrorCode status = U_ZERO_ERROR; 152 return udat_open(timeStyle, dateStyle, m_locale.data(), gmtTimezone, WTF_ARRAY_LENGTH(gmtTimezone), 0, -1, &status); 153} 154 155#if ENABLE(DATE_AND_TIME_INPUT_TYPES) 156static String getDateFormatPattern(const UDateFormat* dateFormat) 157{ 158 if (!dateFormat) 159 return emptyString(); 160 161 UErrorCode status = U_ZERO_ERROR; 162 int32_t length = udat_toPattern(dateFormat, TRUE, 0, 0, &status); 163 if (status != U_BUFFER_OVERFLOW_ERROR || !length) 164 return emptyString(); 165 Vector<UChar> buffer(length); 166 status = U_ZERO_ERROR; 167 udat_toPattern(dateFormat, TRUE, buffer.data(), length, &status); 168 if (U_FAILURE(status)) 169 return emptyString(); 170 return String::adopt(buffer); 171} 172 173PassOwnPtr<Vector<String> > LocaleICU::createLabelVector(const UDateFormat* dateFormat, UDateFormatSymbolType type, int32_t startIndex, int32_t size) 174{ 175 if (!dateFormat) 176 return PassOwnPtr<Vector<String> >(); 177 if (udat_countSymbols(dateFormat, type) != startIndex + size) 178 return PassOwnPtr<Vector<String> >(); 179 180 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); 181 labels->reserveCapacity(size); 182 for (int32_t i = 0; i < size; ++i) { 183 UErrorCode status = U_ZERO_ERROR; 184 int32_t length = udat_getSymbols(dateFormat, type, startIndex + i, 0, 0, &status); 185 if (status != U_BUFFER_OVERFLOW_ERROR) 186 return PassOwnPtr<Vector<String> >(); 187 Vector<UChar> buffer(length); 188 status = U_ZERO_ERROR; 189 udat_getSymbols(dateFormat, type, startIndex + i, buffer.data(), length, &status); 190 if (U_FAILURE(status)) 191 return PassOwnPtr<Vector<String> >(); 192 labels->append(String::adopt(buffer)); 193 } 194 return labels.release(); 195} 196#endif 197 198#if ENABLE(DATE_AND_TIME_INPUT_TYPES) 199static PassOwnPtr<Vector<String> > createFallbackMonthLabels() 200{ 201 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); 202 labels->reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthFullName)); 203 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthFullName); ++i) 204 labels->append(WTF::monthFullName[i]); 205 return labels.release(); 206} 207 208const Vector<String>& LocaleICU::monthLabels() 209{ 210 if (m_monthLabels) 211 return *m_monthLabels; 212 if (initializeShortDateFormat()) { 213 m_monthLabels = createLabelVector(m_shortDateFormat, UDAT_MONTHS, UCAL_JANUARY, 12); 214 if (m_monthLabels) 215 return *m_monthLabels; 216 } 217 m_monthLabels = createFallbackMonthLabels(); 218 return *m_monthLabels; 219} 220#endif 221 222#if ENABLE(DATE_AND_TIME_INPUT_TYPES) 223static PassOwnPtr<Vector<String> > createFallbackAMPMLabels() 224{ 225 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); 226 labels->reserveCapacity(2); 227 labels->append("AM"); 228 labels->append("PM"); 229 return labels.release(); 230} 231 232void LocaleICU::initializeDateTimeFormat() 233{ 234 if (m_didCreateTimeFormat) 235 return; 236 237 // We assume ICU medium time pattern and short time pattern are compatible 238 // with LDML, because ICU specific pattern character "V" doesn't appear 239 // in both medium and short time pattern. 240 m_mediumTimeFormat = openDateFormat(UDAT_MEDIUM, UDAT_NONE); 241 m_timeFormatWithSeconds = getDateFormatPattern(m_mediumTimeFormat); 242 243 m_shortTimeFormat = openDateFormat(UDAT_SHORT, UDAT_NONE); 244 m_timeFormatWithoutSeconds = getDateFormatPattern(m_shortTimeFormat); 245 246 UDateFormat* dateTimeFormatWithSeconds = openDateFormat(UDAT_MEDIUM, UDAT_SHORT); 247 m_dateTimeFormatWithSeconds = getDateFormatPattern(dateTimeFormatWithSeconds); 248 udat_close(dateTimeFormatWithSeconds); 249 250 UDateFormat* dateTimeFormatWithoutSeconds = openDateFormat(UDAT_SHORT, UDAT_SHORT); 251 m_dateTimeFormatWithoutSeconds = getDateFormatPattern(dateTimeFormatWithoutSeconds); 252 udat_close(dateTimeFormatWithoutSeconds); 253 254 OwnPtr<Vector<String> > timeAMPMLabels = createLabelVector(m_mediumTimeFormat, UDAT_AM_PMS, UCAL_AM, 2); 255 if (!timeAMPMLabels) 256 timeAMPMLabels = createFallbackAMPMLabels(); 257 m_timeAMPMLabels = *timeAMPMLabels; 258 259 m_didCreateTimeFormat = true; 260} 261 262String LocaleICU::dateFormat() 263{ 264 if (!m_dateFormat.isNull()) 265 return m_dateFormat; 266 if (!initializeShortDateFormat()) 267 return ASCIILiteral("yyyy-MM-dd"); 268 m_dateFormat = getDateFormatPattern(m_shortDateFormat); 269 return m_dateFormat; 270} 271 272static String getFormatForSkeleton(const char* locale, const String& skeleton) 273{ 274 String format = ASCIILiteral("yyyy-MM"); 275 UErrorCode status = U_ZERO_ERROR; 276 UDateTimePatternGenerator* patternGenerator = udatpg_open(locale, &status); 277 if (!patternGenerator) 278 return format; 279 status = U_ZERO_ERROR; 280 int32_t length = udatpg_getBestPattern(patternGenerator, skeleton.characters(), skeleton.length(), 0, 0, &status); 281 if (status == U_BUFFER_OVERFLOW_ERROR && length) { 282 Vector<UChar> buffer(length); 283 status = U_ZERO_ERROR; 284 udatpg_getBestPattern(patternGenerator, skeleton.characters(), skeleton.length(), buffer.data(), length, &status); 285 if (U_SUCCESS(status)) 286 format = String::adopt(buffer); 287 } 288 udatpg_close(patternGenerator); 289 return format; 290} 291 292String LocaleICU::monthFormat() 293{ 294 if (!m_monthFormat.isNull()) 295 return m_monthFormat; 296 // Gets a format for "MMMM" because Windows API always provides formats for 297 // "MMMM" in some locales. 298 m_monthFormat = getFormatForSkeleton(m_locale.data(), ASCIILiteral("yyyyMMMM")); 299 return m_monthFormat; 300} 301 302String LocaleICU::shortMonthFormat() 303{ 304 if (!m_shortMonthFormat.isNull()) 305 return m_shortMonthFormat; 306 m_shortMonthFormat = getFormatForSkeleton(m_locale.data(), ASCIILiteral("yyyyMMM")); 307 return m_shortMonthFormat; 308} 309 310String LocaleICU::timeFormat() 311{ 312 initializeDateTimeFormat(); 313 return m_timeFormatWithSeconds; 314} 315 316String LocaleICU::shortTimeFormat() 317{ 318 initializeDateTimeFormat(); 319 return m_timeFormatWithoutSeconds; 320} 321 322String LocaleICU::dateTimeFormatWithSeconds() 323{ 324 initializeDateTimeFormat(); 325 return m_dateTimeFormatWithSeconds; 326} 327 328String LocaleICU::dateTimeFormatWithoutSeconds() 329{ 330 initializeDateTimeFormat(); 331 return m_dateTimeFormatWithoutSeconds; 332} 333 334const Vector<String>& LocaleICU::shortMonthLabels() 335{ 336 if (!m_shortMonthLabels.isEmpty()) 337 return m_shortMonthLabels; 338 if (initializeShortDateFormat()) { 339 if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat, UDAT_SHORT_MONTHS, UCAL_JANUARY, 12)) { 340 m_shortMonthLabels = *labels; 341 return m_shortMonthLabels; 342 } 343 } 344 m_shortMonthLabels.reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthName)); 345 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthName); ++i) 346 m_shortMonthLabels.append(WTF::monthName[i]); 347 return m_shortMonthLabels; 348} 349 350const Vector<String>& LocaleICU::standAloneMonthLabels() 351{ 352 if (!m_standAloneMonthLabels.isEmpty()) 353 return m_standAloneMonthLabels; 354 if (initializeShortDateFormat()) { 355 if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat, UDAT_STANDALONE_MONTHS, UCAL_JANUARY, 12)) { 356 m_standAloneMonthLabels = *labels; 357 return m_standAloneMonthLabels; 358 } 359 } 360 m_standAloneMonthLabels = monthLabels(); 361 return m_standAloneMonthLabels; 362} 363 364const Vector<String>& LocaleICU::shortStandAloneMonthLabels() 365{ 366 if (!m_shortStandAloneMonthLabels.isEmpty()) 367 return m_shortStandAloneMonthLabels; 368 if (initializeShortDateFormat()) { 369 if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat, UDAT_STANDALONE_SHORT_MONTHS, UCAL_JANUARY, 12)) { 370 m_shortStandAloneMonthLabels = *labels; 371 return m_shortStandAloneMonthLabels; 372 } 373 } 374 m_shortStandAloneMonthLabels = shortMonthLabels(); 375 return m_shortStandAloneMonthLabels; 376} 377 378const Vector<String>& LocaleICU::timeAMPMLabels() 379{ 380 initializeDateTimeFormat(); 381 return m_timeAMPMLabels; 382} 383 384#endif 385 386} // namespace WebCore 387 388