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 "platform/text/LocaleICU.h"
33
34#include <unicode/udatpg.h>
35#include <unicode/uloc.h>
36#include <limits>
37#include "wtf/DateMath.h"
38#include "wtf/PassOwnPtr.h"
39#include "wtf/text/StringBuffer.h"
40#include "wtf/text/StringBuilder.h"
41
42using namespace icu;
43
44namespace blink {
45
46PassOwnPtr<Locale> Locale::create(const String& locale)
47{
48    return LocaleICU::create(locale.utf8().data());
49}
50
51LocaleICU::LocaleICU(const char* locale)
52    : m_locale(locale)
53    , m_numberFormat(0)
54    , m_shortDateFormat(0)
55    , m_didCreateDecimalFormat(false)
56    , m_didCreateShortDateFormat(false)
57    , m_firstDayOfWeek(0)
58    , m_mediumTimeFormat(0)
59    , m_shortTimeFormat(0)
60    , m_didCreateTimeFormat(false)
61{
62}
63
64LocaleICU::~LocaleICU()
65{
66    unum_close(m_numberFormat);
67    udat_close(m_shortDateFormat);
68    udat_close(m_mediumTimeFormat);
69    udat_close(m_shortTimeFormat);
70}
71
72PassOwnPtr<LocaleICU> LocaleICU::create(const char* localeString)
73{
74    return adoptPtr(new LocaleICU(localeString));
75}
76
77String LocaleICU::decimalSymbol(UNumberFormatSymbol symbol)
78{
79    UErrorCode status = U_ZERO_ERROR;
80    int32_t bufferLength = unum_getSymbol(m_numberFormat, symbol, 0, 0, &status);
81    ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR);
82    if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
83        return String();
84    StringBuffer<UChar> buffer(bufferLength);
85    status = U_ZERO_ERROR;
86    unum_getSymbol(m_numberFormat, symbol, buffer.characters(), bufferLength, &status);
87    if (U_FAILURE(status))
88        return String();
89    return String::adopt(buffer);
90}
91
92String LocaleICU::decimalTextAttribute(UNumberFormatTextAttribute tag)
93{
94    UErrorCode status = U_ZERO_ERROR;
95    int32_t bufferLength = unum_getTextAttribute(m_numberFormat, tag, 0, 0, &status);
96    ASSERT(U_SUCCESS(status) || status == U_BUFFER_OVERFLOW_ERROR);
97    if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
98        return String();
99    StringBuffer<UChar> buffer(bufferLength);
100    status = U_ZERO_ERROR;
101    unum_getTextAttribute(m_numberFormat, tag, buffer.characters(), bufferLength, &status);
102    ASSERT(U_SUCCESS(status));
103    if (U_FAILURE(status))
104        return String();
105    return String::adopt(buffer);
106}
107
108void LocaleICU::initializeLocaleData()
109{
110    if (m_didCreateDecimalFormat)
111        return;
112    m_didCreateDecimalFormat = true;
113    UErrorCode status = U_ZERO_ERROR;
114    m_numberFormat = unum_open(UNUM_DECIMAL, 0, 0, m_locale.data(), 0, &status);
115    if (!U_SUCCESS(status))
116        return;
117
118    Vector<String, DecimalSymbolsSize> symbols;
119    symbols.append(decimalSymbol(UNUM_ZERO_DIGIT_SYMBOL));
120    symbols.append(decimalSymbol(UNUM_ONE_DIGIT_SYMBOL));
121    symbols.append(decimalSymbol(UNUM_TWO_DIGIT_SYMBOL));
122    symbols.append(decimalSymbol(UNUM_THREE_DIGIT_SYMBOL));
123    symbols.append(decimalSymbol(UNUM_FOUR_DIGIT_SYMBOL));
124    symbols.append(decimalSymbol(UNUM_FIVE_DIGIT_SYMBOL));
125    symbols.append(decimalSymbol(UNUM_SIX_DIGIT_SYMBOL));
126    symbols.append(decimalSymbol(UNUM_SEVEN_DIGIT_SYMBOL));
127    symbols.append(decimalSymbol(UNUM_EIGHT_DIGIT_SYMBOL));
128    symbols.append(decimalSymbol(UNUM_NINE_DIGIT_SYMBOL));
129    symbols.append(decimalSymbol(UNUM_DECIMAL_SEPARATOR_SYMBOL));
130    symbols.append(decimalSymbol(UNUM_GROUPING_SEPARATOR_SYMBOL));
131    ASSERT(symbols.size() == DecimalSymbolsSize);
132    setLocaleData(symbols, decimalTextAttribute(UNUM_POSITIVE_PREFIX), decimalTextAttribute(UNUM_POSITIVE_SUFFIX), decimalTextAttribute(UNUM_NEGATIVE_PREFIX), decimalTextAttribute(UNUM_NEGATIVE_SUFFIX));
133}
134
135bool LocaleICU::initializeShortDateFormat()
136{
137    if (m_didCreateShortDateFormat)
138        return m_shortDateFormat;
139    m_shortDateFormat = openDateFormat(UDAT_NONE, UDAT_SHORT);
140    m_didCreateShortDateFormat = true;
141    return m_shortDateFormat;
142}
143
144UDateFormat* LocaleICU::openDateFormat(UDateFormatStyle timeStyle, UDateFormatStyle dateStyle) const
145{
146    const UChar gmtTimezone[3] = {'G', 'M', 'T'};
147    UErrorCode status = U_ZERO_ERROR;
148    return udat_open(timeStyle, dateStyle, m_locale.data(), gmtTimezone, WTF_ARRAY_LENGTH(gmtTimezone), 0, -1, &status);
149}
150
151static String getDateFormatPattern(const UDateFormat* dateFormat)
152{
153    if (!dateFormat)
154        return emptyString();
155
156    UErrorCode status = U_ZERO_ERROR;
157    int32_t length = udat_toPattern(dateFormat, TRUE, 0, 0, &status);
158    if (status != U_BUFFER_OVERFLOW_ERROR || !length)
159        return emptyString();
160    StringBuffer<UChar> buffer(length);
161    status = U_ZERO_ERROR;
162    udat_toPattern(dateFormat, TRUE, buffer.characters(), length, &status);
163    if (U_FAILURE(status))
164        return emptyString();
165    return String::adopt(buffer);
166}
167
168PassOwnPtr<Vector<String> > LocaleICU::createLabelVector(const UDateFormat* dateFormat, UDateFormatSymbolType type, int32_t startIndex, int32_t size)
169{
170    if (!dateFormat)
171        return PassOwnPtr<Vector<String> >();
172    if (udat_countSymbols(dateFormat, type) != startIndex + size)
173        return PassOwnPtr<Vector<String> >();
174
175    OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>());
176    labels->reserveCapacity(size);
177    for (int32_t i = 0; i < size; ++i) {
178        UErrorCode status = U_ZERO_ERROR;
179        int32_t length = udat_getSymbols(dateFormat, type, startIndex + i, 0, 0, &status);
180        if (status != U_BUFFER_OVERFLOW_ERROR)
181            return PassOwnPtr<Vector<String> >();
182        StringBuffer<UChar> buffer(length);
183        status = U_ZERO_ERROR;
184        udat_getSymbols(dateFormat, type, startIndex + i, buffer.characters(), length, &status);
185        if (U_FAILURE(status))
186            return PassOwnPtr<Vector<String> >();
187        labels->append(String::adopt(buffer));
188    }
189    return labels.release();
190}
191
192static PassOwnPtr<Vector<String> > createFallbackWeekDayShortLabels()
193{
194    OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>());
195    labels->reserveCapacity(7);
196    labels->append("Sun");
197    labels->append("Mon");
198    labels->append("Tue");
199    labels->append("Wed");
200    labels->append("Thu");
201    labels->append("Fri");
202    labels->append("Sat");
203    return labels.release();
204}
205
206void LocaleICU::initializeCalendar()
207{
208    if (m_weekDayShortLabels)
209        return;
210
211    if (!initializeShortDateFormat()) {
212        m_firstDayOfWeek = 0;
213        m_weekDayShortLabels = createFallbackWeekDayShortLabels();
214        return;
215    }
216    m_firstDayOfWeek = ucal_getAttribute(udat_getCalendar(m_shortDateFormat), UCAL_FIRST_DAY_OF_WEEK) - UCAL_SUNDAY;
217
218    m_weekDayShortLabels = createLabelVector(m_shortDateFormat, UDAT_SHORT_WEEKDAYS, UCAL_SUNDAY, 7);
219    if (!m_weekDayShortLabels)
220        m_weekDayShortLabels = createFallbackWeekDayShortLabels();
221}
222
223static PassOwnPtr<Vector<String> > createFallbackMonthLabels()
224{
225    OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>());
226    labels->reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthFullName));
227    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthFullName); ++i)
228        labels->append(WTF::monthFullName[i]);
229    return labels.release();
230}
231
232const Vector<String>& LocaleICU::monthLabels()
233{
234    if (m_monthLabels)
235        return *m_monthLabels;
236    if (initializeShortDateFormat()) {
237        m_monthLabels = createLabelVector(m_shortDateFormat, UDAT_MONTHS, UCAL_JANUARY, 12);
238        if (m_monthLabels)
239            return *m_monthLabels;
240    }
241    m_monthLabels = createFallbackMonthLabels();
242    return *m_monthLabels;
243}
244
245const Vector<String>& LocaleICU::weekDayShortLabels()
246{
247    initializeCalendar();
248    return *m_weekDayShortLabels;
249}
250
251unsigned LocaleICU::firstDayOfWeek()
252{
253    initializeCalendar();
254    return m_firstDayOfWeek;
255}
256
257bool LocaleICU::isRTL()
258{
259    UErrorCode status = U_ZERO_ERROR;
260    return uloc_getCharacterOrientation(m_locale.data(), &status) == ULOC_LAYOUT_RTL;
261}
262
263static PassOwnPtr<Vector<String> > createFallbackAMPMLabels()
264{
265    OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>());
266    labels->reserveCapacity(2);
267    labels->append("AM");
268    labels->append("PM");
269    return labels.release();
270}
271
272void LocaleICU::initializeDateTimeFormat()
273{
274    if (m_didCreateTimeFormat)
275        return;
276
277    // We assume ICU medium time pattern and short time pattern are compatible
278    // with LDML, because ICU specific pattern character "V" doesn't appear
279    // in both medium and short time pattern.
280    m_mediumTimeFormat = openDateFormat(UDAT_MEDIUM, UDAT_NONE);
281    m_timeFormatWithSeconds = getDateFormatPattern(m_mediumTimeFormat);
282
283    m_shortTimeFormat = openDateFormat(UDAT_SHORT, UDAT_NONE);
284    m_timeFormatWithoutSeconds = getDateFormatPattern(m_shortTimeFormat);
285
286    UDateFormat* dateTimeFormatWithSeconds = openDateFormat(UDAT_MEDIUM, UDAT_SHORT);
287    m_dateTimeFormatWithSeconds = getDateFormatPattern(dateTimeFormatWithSeconds);
288    udat_close(dateTimeFormatWithSeconds);
289
290    UDateFormat* dateTimeFormatWithoutSeconds = openDateFormat(UDAT_SHORT, UDAT_SHORT);
291    m_dateTimeFormatWithoutSeconds = getDateFormatPattern(dateTimeFormatWithoutSeconds);
292    udat_close(dateTimeFormatWithoutSeconds);
293
294    OwnPtr<Vector<String> > timeAMPMLabels = createLabelVector(m_mediumTimeFormat, UDAT_AM_PMS, UCAL_AM, 2);
295    if (!timeAMPMLabels)
296        timeAMPMLabels = createFallbackAMPMLabels();
297    m_timeAMPMLabels = *timeAMPMLabels;
298
299    m_didCreateTimeFormat = true;
300}
301
302String LocaleICU::dateFormat()
303{
304    if (!m_dateFormat.isNull())
305        return m_dateFormat;
306    if (!initializeShortDateFormat())
307        return "yyyy-MM-dd";
308    m_dateFormat = getDateFormatPattern(m_shortDateFormat);
309    return m_dateFormat;
310}
311
312static String getFormatForSkeleton(const char* locale, const String& skeleton)
313{
314    String format = "yyyy-MM";
315    UErrorCode status = U_ZERO_ERROR;
316    UDateTimePatternGenerator* patternGenerator = udatpg_open(locale, &status);
317    if (!patternGenerator)
318        return format;
319    status = U_ZERO_ERROR;
320    Vector<UChar> skeletonCharacters;
321    skeleton.appendTo(skeletonCharacters);
322    int32_t length = udatpg_getBestPattern(patternGenerator, skeletonCharacters.data(), skeletonCharacters.size(), 0, 0, &status);
323    if (status == U_BUFFER_OVERFLOW_ERROR && length) {
324        StringBuffer<UChar> buffer(length);
325        status = U_ZERO_ERROR;
326        udatpg_getBestPattern(patternGenerator, skeletonCharacters.data(), skeletonCharacters.size(), buffer.characters(), length, &status);
327        if (U_SUCCESS(status))
328            format = String::adopt(buffer);
329    }
330    udatpg_close(patternGenerator);
331    return format;
332}
333
334String LocaleICU::monthFormat()
335{
336    if (!m_monthFormat.isNull())
337        return m_monthFormat;
338    // Gets a format for "MMMM" because Windows API always provides formats for
339    // "MMMM" in some locales.
340    m_monthFormat = getFormatForSkeleton(m_locale.data(), "yyyyMMMM");
341    return m_monthFormat;
342}
343
344String LocaleICU::shortMonthFormat()
345{
346    if (!m_shortMonthFormat.isNull())
347        return m_shortMonthFormat;
348    m_shortMonthFormat = getFormatForSkeleton(m_locale.data(), "yyyyMMM");
349    return m_shortMonthFormat;
350}
351
352String LocaleICU::timeFormat()
353{
354    initializeDateTimeFormat();
355    return m_timeFormatWithSeconds;
356}
357
358String LocaleICU::shortTimeFormat()
359{
360    initializeDateTimeFormat();
361    return m_timeFormatWithoutSeconds;
362}
363
364String LocaleICU::dateTimeFormatWithSeconds()
365{
366    initializeDateTimeFormat();
367    return m_dateTimeFormatWithSeconds;
368}
369
370String LocaleICU::dateTimeFormatWithoutSeconds()
371{
372    initializeDateTimeFormat();
373    return m_dateTimeFormatWithoutSeconds;
374}
375
376const Vector<String>& LocaleICU::shortMonthLabels()
377{
378    if (!m_shortMonthLabels.isEmpty())
379        return m_shortMonthLabels;
380    if (initializeShortDateFormat()) {
381        if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat, UDAT_SHORT_MONTHS, UCAL_JANUARY, 12)) {
382            m_shortMonthLabels = *labels;
383            return m_shortMonthLabels;
384        }
385    }
386    m_shortMonthLabels.reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthName));
387    for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthName); ++i)
388        m_shortMonthLabels.append(WTF::monthName[i]);
389    return m_shortMonthLabels;
390}
391
392const Vector<String>& LocaleICU::standAloneMonthLabels()
393{
394    if (!m_standAloneMonthLabels.isEmpty())
395        return m_standAloneMonthLabels;
396    if (initializeShortDateFormat()) {
397        if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat, UDAT_STANDALONE_MONTHS, UCAL_JANUARY, 12)) {
398            m_standAloneMonthLabels = *labels;
399            return m_standAloneMonthLabels;
400        }
401    }
402    m_standAloneMonthLabels = monthLabels();
403    return m_standAloneMonthLabels;
404}
405
406const Vector<String>& LocaleICU::shortStandAloneMonthLabels()
407{
408    if (!m_shortStandAloneMonthLabels.isEmpty())
409        return m_shortStandAloneMonthLabels;
410    if (initializeShortDateFormat()) {
411        if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat, UDAT_STANDALONE_SHORT_MONTHS, UCAL_JANUARY, 12)) {
412            m_shortStandAloneMonthLabels = *labels;
413            return m_shortStandAloneMonthLabels;
414        }
415    }
416    m_shortStandAloneMonthLabels = shortMonthLabels();
417    return m_shortStandAloneMonthLabels;
418}
419
420const Vector<String>& LocaleICU::timeAMPMLabels()
421{
422    initializeDateTimeFormat();
423    return m_timeAMPMLabels;
424}
425
426} // namespace blink
427
428