1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "LocaleMac.h"
#import <Foundation/NSDateFormatter.h>
#import <Foundation/NSLocale.h>
#include "Language.h"
#include "LocalizedStrings.h"
#include <wtf/DateMath.h>
#include <wtf/PassOwnPtr.h>
#include <wtf/RetainPtr.h>
#include <wtf/text/StringBuilder.h>
using namespace std;
namespace WebCore {
static inline String languageFromLocale(const String& locale)
{
String normalizedLocale = locale;
normalizedLocale.replace('-', '_');
size_t separatorPosition = normalizedLocale.find('_');
if (separatorPosition == notFound)
return normalizedLocale;
return normalizedLocale.left(separatorPosition);
}
static RetainPtr<NSLocale> determineLocale(const String& locale)
{
RetainPtr<NSLocale> currentLocale = [NSLocale currentLocale];
String currentLocaleLanguage = languageFromLocale(String([currentLocale.get() localeIdentifier]));
String localeLanguage = languageFromLocale(locale);
if (equalIgnoringCase(currentLocaleLanguage, localeLanguage))
return currentLocale;
// It seems initWithLocaleIdentifier accepts dash-separated locale identifier.
return adoptNS([[NSLocale alloc] initWithLocaleIdentifier:locale]);
}
PassOwnPtr<Locale> Locale::create(const AtomicString& locale)
{
return LocaleMac::create(determineLocale(locale.string()).get());
}
static RetainPtr<NSDateFormatter> createDateTimeFormatter(NSLocale* locale, NSCalendar* calendar, NSDateFormatterStyle dateStyle, NSDateFormatterStyle timeStyle)
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:locale];
[formatter setDateStyle:dateStyle];
[formatter setTimeStyle:timeStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[formatter setCalendar:calendar];
return adoptNS(formatter);
}
LocaleMac::LocaleMac(NSLocale* locale)
: m_locale(locale)
, m_gregorianCalendar(adoptNS([[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]))
, m_didInitializeNumberData(false)
{
NSArray* availableLanguages = [NSLocale ISOLanguageCodes];
// NSLocale returns a lower case NSLocaleLanguageCode so we don't have care about case.
NSString* language = [m_locale.get() objectForKey:NSLocaleLanguageCode];
if ([availableLanguages indexOfObject:language] == NSNotFound)
m_locale = adoptNS([[NSLocale alloc] initWithLocaleIdentifier:defaultLanguage()]);
[m_gregorianCalendar.get() setLocale:m_locale.get()];
}
LocaleMac::~LocaleMac()
{
}
PassOwnPtr<LocaleMac> LocaleMac::create(const String& localeIdentifier)
{
RetainPtr<NSLocale> locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
return adoptPtr(new LocaleMac(locale.get()));
}
PassOwnPtr<LocaleMac> LocaleMac::create(NSLocale* locale)
{
return adoptPtr(new LocaleMac(locale));
}
RetainPtr<NSDateFormatter> LocaleMac::shortDateFormatter()
{
return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NSDateFormatterShortStyle, NSDateFormatterNoStyle);
}
#if ENABLE(DATE_AND_TIME_INPUT_TYPES)
const Vector<String>& LocaleMac::monthLabels()
{
if (!m_monthLabels.isEmpty())
return m_monthLabels;
m_monthLabels.reserveCapacity(12);
NSArray *array = [shortDateFormatter().get() monthSymbols];
if ([array count] == 12) {
for (unsigned i = 0; i < 12; ++i)
m_monthLabels.append(String([array objectAtIndex:i]));
return m_monthLabels;
}
for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthFullName); ++i)
m_monthLabels.append(WTF::monthFullName[i]);
return m_monthLabels;
}
RetainPtr<NSDateFormatter> LocaleMac::timeFormatter()
{
return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NSDateFormatterNoStyle, NSDateFormatterMediumStyle);
}
RetainPtr<NSDateFormatter> LocaleMac::shortTimeFormatter()
{
return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NSDateFormatterNoStyle, NSDateFormatterShortStyle);
}
RetainPtr<NSDateFormatter> LocaleMac::dateTimeFormatterWithSeconds()
{
return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NSDateFormatterShortStyle, NSDateFormatterMediumStyle);
}
RetainPtr<NSDateFormatter> LocaleMac::dateTimeFormatterWithoutSeconds()
{
return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NSDateFormatterShortStyle, NSDateFormatterShortStyle);
}
String LocaleMac::dateFormat()
{
if (!m_dateFormat.isNull())
return m_dateFormat;
m_dateFormat = [shortDateFormatter().get() dateFormat];
return m_dateFormat;
}
String LocaleMac::monthFormat()
{
if (!m_monthFormat.isNull())
return m_monthFormat;
// Gets a format for "MMMM" because Windows API always provides formats for
// "MMMM" in some locales.
m_monthFormat = [NSDateFormatter dateFormatFromTemplate:@"yyyyMMMM" options:0 locale:m_locale.get()];
return m_monthFormat;
}
String LocaleMac::shortMonthFormat()
{
if (!m_shortMonthFormat.isNull())
return m_shortMonthFormat;
m_shortMonthFormat = [NSDateFormatter dateFormatFromTemplate:@"yyyyMMM" options:0 locale:m_locale.get()];
return m_shortMonthFormat;
}
String LocaleMac::timeFormat()
{
if (!m_timeFormatWithSeconds.isNull())
return m_timeFormatWithSeconds;
m_timeFormatWithSeconds = [timeFormatter().get() dateFormat];
return m_timeFormatWithSeconds;
}
String LocaleMac::shortTimeFormat()
{
if (!m_timeFormatWithoutSeconds.isNull())
return m_timeFormatWithoutSeconds;
m_timeFormatWithoutSeconds = [shortTimeFormatter().get() dateFormat];
return m_timeFormatWithoutSeconds;
}
String LocaleMac::dateTimeFormatWithSeconds()
{
if (!m_dateTimeFormatWithSeconds.isNull())
return m_dateTimeFormatWithSeconds;
m_dateTimeFormatWithSeconds = [dateTimeFormatterWithSeconds().get() dateFormat];
return m_dateTimeFormatWithSeconds;
}
String LocaleMac::dateTimeFormatWithoutSeconds()
{
if (!m_dateTimeFormatWithoutSeconds.isNull())
return m_dateTimeFormatWithoutSeconds;
m_dateTimeFormatWithoutSeconds = [dateTimeFormatterWithoutSeconds().get() dateFormat];
return m_dateTimeFormatWithoutSeconds;
}
const Vector<String>& LocaleMac::shortMonthLabels()
{
if (!m_shortMonthLabels.isEmpty())
return m_shortMonthLabels;
m_shortMonthLabels.reserveCapacity(12);
NSArray *array = [shortDateFormatter().get() shortMonthSymbols];
if ([array count] == 12) {
for (unsigned i = 0; i < 12; ++i)
m_shortMonthLabels.append([array objectAtIndex:i]);
return m_shortMonthLabels;
}
for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthName); ++i)
m_shortMonthLabels.append(WTF::monthName[i]);
return m_shortMonthLabels;
}
const Vector<String>& LocaleMac::standAloneMonthLabels()
{
if (!m_standAloneMonthLabels.isEmpty())
return m_standAloneMonthLabels;
NSArray *array = [shortDateFormatter().get() standaloneMonthSymbols];
if ([array count] == 12) {
m_standAloneMonthLabels.reserveCapacity(12);
for (unsigned i = 0; i < 12; ++i)
m_standAloneMonthLabels.append([array objectAtIndex:i]);
return m_standAloneMonthLabels;
}
m_standAloneMonthLabels = shortMonthLabels();
return m_standAloneMonthLabels;
}
const Vector<String>& LocaleMac::shortStandAloneMonthLabels()
{
if (!m_shortStandAloneMonthLabels.isEmpty())
return m_shortStandAloneMonthLabels;
NSArray *array = [shortDateFormatter().get() shortStandaloneMonthSymbols];
if ([array count] == 12) {
m_shortStandAloneMonthLabels.reserveCapacity(12);
for (unsigned i = 0; i < 12; ++i)
m_shortStandAloneMonthLabels.append([array objectAtIndex:i]);
return m_shortStandAloneMonthLabels;
}
m_shortStandAloneMonthLabels = shortMonthLabels();
return m_shortStandAloneMonthLabels;
}
const Vector<String>& LocaleMac::timeAMPMLabels()
{
if (!m_timeAMPMLabels.isEmpty())
return m_timeAMPMLabels;
m_timeAMPMLabels.reserveCapacity(2);
RetainPtr<NSDateFormatter> formatter = shortTimeFormatter();
m_timeAMPMLabels.append([formatter.get() AMSymbol]);
m_timeAMPMLabels.append([formatter.get() PMSymbol]);
return m_timeAMPMLabels;
}
#endif
void LocaleMac::initializeLocaleData()
{
if (m_didInitializeNumberData)
return;
m_didInitializeNumberData = true;
RetainPtr<NSNumberFormatter> formatter = adoptNS([[NSNumberFormatter alloc] init]);
[formatter.get() setLocale:m_locale.get()];
[formatter.get() setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter.get() setUsesGroupingSeparator:NO];
RetainPtr<NSNumber> sampleNumber = adoptNS([[NSNumber alloc] initWithDouble:9876543210]);
String nineToZero([formatter.get() stringFromNumber:sampleNumber.get()]);
if (nineToZero.length() != 10)
return;
Vector<String, DecimalSymbolsSize> symbols;
for (unsigned i = 0; i < 10; ++i)
symbols.append(nineToZero.substring(9 - i, 1));
ASSERT(symbols.size() == DecimalSeparatorIndex);
symbols.append([formatter.get() decimalSeparator]);
ASSERT(symbols.size() == GroupSeparatorIndex);
symbols.append([formatter.get() groupingSeparator]);
ASSERT(symbols.size() == DecimalSymbolsSize);
String positivePrefix([formatter.get() positivePrefix]);
String positiveSuffix([formatter.get() positiveSuffix]);
String negativePrefix([formatter.get() negativePrefix]);
String negativeSuffix([formatter.get() negativeSuffix]);
setLocaleData(symbols, positivePrefix, positiveSuffix, negativePrefix, negativeSuffix);
}
}
|