1/*
2 * Copyright (C) 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
6 * are met:
7 * 1.  Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *     notice, this list of conditions and the following disclaimer in the
11 *     documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23 * DAMAGE.
24 */
25
26#ifndef PlatformLocale_h
27#define PlatformLocale_h
28
29#include "DateComponents.h"
30#include "Language.h"
31#include <wtf/PassOwnPtr.h>
32#include <wtf/text/WTFString.h>
33
34namespace WebCore {
35
36#if PLATFORM(IOS)
37class Font;
38#endif
39
40class Locale {
41    WTF_MAKE_NONCOPYABLE(Locale);
42
43public:
44    static PassOwnPtr<Locale> create(const AtomicString& localeIdentifier);
45    static PassOwnPtr<Locale> createDefault();
46
47    // Converts the specified number string to another number string localized
48    // for this Locale locale. The input string must conform to HTML
49    // floating-point numbers, and is not empty.
50    String convertToLocalizedNumber(const String&);
51
52    // Converts the specified localized number string to a number string in the
53    // HTML floating-point number format. The input string is provided by a end
54    // user, and might not be a number string. It's ok that the function returns
55    // a string which is not conforms to the HTML floating-point number format,
56    // callers of this function are responsible to check the format of the
57    // resultant string.
58    String convertFromLocalizedNumber(const String&);
59
60#if ENABLE(DATE_AND_TIME_INPUT_TYPES)
61    // Returns date format in Unicode TR35 LDML[1] containing day of month,
62    // month, and year, e.g. "dd/mm/yyyy"
63    // [1] LDML http://unicode.org/reports/tr35/#Date_Format_Patterns
64    virtual String dateFormat() = 0;
65
66    // Returns a year-month format in Unicode TR35 LDML.
67    virtual String monthFormat() = 0;
68
69    // Returns a year-month format using short month lanel in Unicode TR35 LDML.
70    virtual String shortMonthFormat() = 0;
71
72    // Returns time format in Unicode TR35 LDML[1] containing hour, minute, and
73    // second with optional period(AM/PM), e.g. "h:mm:ss a"
74    // [1] LDML http://unicode.org/reports/tr35/#Date_Format_Patterns
75    virtual String timeFormat() = 0;
76
77    // Returns time format in Unicode TR35 LDML containing hour, and minute
78    // with optional period(AM/PM), e.g. "h:mm a"
79    // Note: Some platforms return same value as timeFormat().
80    virtual String shortTimeFormat() = 0;
81
82    // Returns a date-time format in Unicode TR35 LDML. It should have a seconds
83    // field.
84    virtual String dateTimeFormatWithSeconds() = 0;
85
86    // Returns a date-time format in Unicode TR35 LDML. It should have no seconds
87    // field.
88    virtual String dateTimeFormatWithoutSeconds() = 0;
89
90    // Returns a vector of string of which size is 12. The first item is a
91    // localized string of Jan and the last item is a localized string of
92    // Dec. These strings should be short.
93    virtual const Vector<String>& shortMonthLabels() = 0;
94
95    // Returns a vector of string of which size is 12. The first item is a
96    // stand-alone localized string of January and the last item is a
97    // stand-alone localized string of December. These strings should not be
98    // abbreviations.
99    virtual const Vector<String>& standAloneMonthLabels() = 0;
100
101    // Stand-alone month version of shortMonthLabels.
102    virtual const Vector<String>& shortStandAloneMonthLabels() = 0;
103
104    // Returns localized period field(AM/PM) strings.
105    virtual const Vector<String>& timeAMPMLabels() = 0;
106
107    // Returns a vector of string of which size is 12. The first item is a
108    // localized string of January, and the last item is a localized string of
109    // December. These strings should not be abbreviations.
110    virtual const Vector<String>& monthLabels() = 0;
111#endif
112
113#if ENABLE(DATE_AND_TIME_INPUT_TYPES)
114    enum FormatType { FormatTypeUnspecified, FormatTypeShort, FormatTypeMedium };
115
116    // Serializes the specified date into a formatted date string to
117    // display to the user. If an implementation doesn't support
118    // localized dates the function should return an empty string.
119    // FormatType can be used to specify if you want the short format.
120#if !PLATFORM(IOS)
121    String formatDateTime(const DateComponents&, FormatType = FormatTypeUnspecified);
122#else
123    virtual String formatDateTime(const DateComponents&, FormatType = FormatTypeUnspecified) = 0;
124#endif // !PLATFORM(IOS)
125#endif
126
127    virtual ~Locale();
128
129protected:
130    enum {
131        // 0-9 for digits.
132        DecimalSeparatorIndex = 10,
133        GroupSeparatorIndex = 11,
134        DecimalSymbolsSize
135    };
136
137    Locale() : m_hasLocaleData(false) { }
138    virtual void initializeLocaleData() = 0;
139    void setLocaleData(const Vector<String, DecimalSymbolsSize>&, const String& positivePrefix, const String& positiveSuffix, const String& negativePrefix, const String& negativeSuffix);
140
141private:
142    bool detectSignAndGetDigitRange(const String& input, bool& isNegative, unsigned& startIndex, unsigned& endIndex);
143    unsigned matchedDecimalSymbolIndex(const String& input, unsigned& position);
144
145    String m_decimalSymbols[DecimalSymbolsSize];
146    String m_positivePrefix;
147    String m_positiveSuffix;
148    String m_negativePrefix;
149    String m_negativeSuffix;
150    bool m_hasLocaleData;
151};
152
153inline PassOwnPtr<Locale> Locale::createDefault()
154{
155    return Locale::create(defaultLanguage());
156}
157
158}
159#endif
160