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