| 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 <wtf/Language.h> |
| 31 | #include <wtf/text/WTFString.h> |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | #if PLATFORM(IOS_FAMILY) |
| 36 | class FontCascade; |
| 37 | #endif |
| 38 | |
| 39 | class Locale { |
| 40 | WTF_MAKE_NONCOPYABLE(Locale); WTF_MAKE_FAST_ALLOCATED; |
| 41 | |
| 42 | public: |
| 43 | static std::unique_ptr<Locale> create(const AtomicString& localeIdentifier); |
| 44 | static std::unique_ptr<Locale> createDefault(); |
| 45 | |
| 46 | // Converts the specified number string to another number string localized |
| 47 | // for this Locale locale. The input string must conform to HTML |
| 48 | // floating-point numbers, and is not empty. |
| 49 | String convertToLocalizedNumber(const String&); |
| 50 | |
| 51 | // Converts the specified localized number string to a number string in the |
| 52 | // HTML floating-point number format. The input string is provided by a end |
| 53 | // user, and might not be a number string. It's ok that the function returns |
| 54 | // a string which is not conforms to the HTML floating-point number format, |
| 55 | // callers of this function are responsible to check the format of the |
| 56 | // resultant string. |
| 57 | String convertFromLocalizedNumber(const String&); |
| 58 | |
| 59 | #if ENABLE(DATE_AND_TIME_INPUT_TYPES) |
| 60 | // Returns date format in Unicode TR35 LDML[1] containing day of month, |
| 61 | // month, and year, e.g. "dd/mm/yyyy" |
| 62 | // [1] LDML http://unicode.org/reports/tr35/#Date_Format_Patterns |
| 63 | virtual String dateFormat() = 0; |
| 64 | |
| 65 | // Returns a year-month format in Unicode TR35 LDML. |
| 66 | virtual String monthFormat() = 0; |
| 67 | |
| 68 | // Returns a year-month format using short month lanel in Unicode TR35 LDML. |
| 69 | virtual String shortMonthFormat() = 0; |
| 70 | |
| 71 | // Returns time format in Unicode TR35 LDML[1] containing hour, minute, and |
| 72 | // second with optional period(AM/PM), e.g. "h:mm:ss a" |
| 73 | // [1] LDML http://unicode.org/reports/tr35/#Date_Format_Patterns |
| 74 | virtual String timeFormat() = 0; |
| 75 | |
| 76 | // Returns time format in Unicode TR35 LDML containing hour, and minute |
| 77 | // with optional period(AM/PM), e.g. "h:mm a" |
| 78 | // Note: Some platforms return same value as timeFormat(). |
| 79 | virtual String shortTimeFormat() = 0; |
| 80 | |
| 81 | // Returns a date-time format in Unicode TR35 LDML. It should have a seconds |
| 82 | // field. |
| 83 | virtual String dateTimeFormatWithSeconds() = 0; |
| 84 | |
| 85 | // Returns a date-time format in Unicode TR35 LDML. It should have no seconds |
| 86 | // field. |
| 87 | virtual String dateTimeFormatWithoutSeconds() = 0; |
| 88 | |
| 89 | // Returns a vector of string of which size is 12. The first item is a |
| 90 | // localized string of Jan and the last item is a localized string of |
| 91 | // Dec. These strings should be short. |
| 92 | virtual const Vector<String>& shortMonthLabels() = 0; |
| 93 | |
| 94 | // Returns a vector of string of which size is 12. The first item is a |
| 95 | // stand-alone localized string of January and the last item is a |
| 96 | // stand-alone localized string of December. These strings should not be |
| 97 | // abbreviations. |
| 98 | virtual const Vector<String>& standAloneMonthLabels() = 0; |
| 99 | |
| 100 | // Stand-alone month version of shortMonthLabels. |
| 101 | virtual const Vector<String>& shortStandAloneMonthLabels() = 0; |
| 102 | |
| 103 | // Returns localized period field(AM/PM) strings. |
| 104 | virtual const Vector<String>& timeAMPMLabels() = 0; |
| 105 | |
| 106 | // Returns a vector of string of which size is 12. The first item is a |
| 107 | // localized string of January, and the last item is a localized string of |
| 108 | // December. These strings should not be abbreviations. |
| 109 | virtual const Vector<String>& monthLabels() = 0; |
| 110 | #endif |
| 111 | |
| 112 | #if ENABLE(DATE_AND_TIME_INPUT_TYPES) |
| 113 | enum FormatType { FormatTypeUnspecified, FormatTypeShort, FormatTypeMedium }; |
| 114 | |
| 115 | // Serializes the specified date into a formatted date string to |
| 116 | // display to the user. If an implementation doesn't support |
| 117 | // localized dates the function should return an empty string. |
| 118 | // FormatType can be used to specify if you want the short format. |
| 119 | #if !PLATFORM(IOS_FAMILY) |
| 120 | String formatDateTime(const DateComponents&, FormatType = FormatTypeUnspecified); |
| 121 | #else |
| 122 | virtual String formatDateTime(const DateComponents&, FormatType = FormatTypeUnspecified) = 0; |
| 123 | #endif // !PLATFORM(IOS_FAMILY) |
| 124 | #endif |
| 125 | |
| 126 | virtual ~Locale(); |
| 127 | |
| 128 | protected: |
| 129 | enum { |
| 130 | // 0-9 for digits. |
| 131 | DecimalSeparatorIndex = 10, |
| 132 | GroupSeparatorIndex = 11, |
| 133 | DecimalSymbolsSize |
| 134 | }; |
| 135 | |
| 136 | Locale() : m_hasLocaleData(false) { } |
| 137 | virtual void initializeLocaleData() = 0; |
| 138 | void setLocaleData(const Vector<String, DecimalSymbolsSize>&, const String& positivePrefix, const String& positiveSuffix, const String& negativePrefix, const String& negativeSuffix); |
| 139 | |
| 140 | private: |
| 141 | bool detectSignAndGetDigitRange(const String& input, bool& isNegative, unsigned& startIndex, unsigned& endIndex); |
| 142 | unsigned matchedDecimalSymbolIndex(const String& input, unsigned& position); |
| 143 | |
| 144 | String m_decimalSymbols[DecimalSymbolsSize]; |
| 145 | String m_positivePrefix; |
| 146 | String m_positiveSuffix; |
| 147 | String m_negativePrefix; |
| 148 | String m_negativeSuffix; |
| 149 | bool m_hasLocaleData; |
| 150 | }; |
| 151 | |
| 152 | inline std::unique_ptr<Locale> Locale::createDefault() |
| 153 | { |
| 154 | return Locale::create(defaultLanguage()); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | #endif |
| 159 | |