| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "platform/text/LocaleMac.h" | 31 #include "platform/text/LocaleMac.h" |
| 32 | 32 |
| 33 #import <Foundation/NSDateFormatter.h> | 33 #import <Foundation/NSDateFormatter.h> |
| 34 #import <Foundation/NSLocale.h> | 34 #import <Foundation/NSLocale.h> |
| 35 #include "platform/Language.h" | 35 #include "platform/Language.h" |
| 36 #include "platform/LayoutTestSupport.h" | 36 #include "platform/LayoutTestSupport.h" |
| 37 #include "wtf/DateMath.h" | 37 #include "wtf/DateMath.h" |
| 38 #include "wtf/PassOwnPtr.h" | 38 #include "wtf/PtrUtil.h" |
| 39 #include "wtf/RetainPtr.h" | 39 #include "wtf/RetainPtr.h" |
| 40 #include "wtf/text/StringBuilder.h" | 40 #include "wtf/text/StringBuilder.h" |
| 41 #include <memory> |
| 41 | 42 |
| 42 namespace blink { | 43 namespace blink { |
| 43 | 44 |
| 44 static inline String languageFromLocale(const String& locale) | 45 static inline String languageFromLocale(const String& locale) |
| 45 { | 46 { |
| 46 String normalizedLocale = locale; | 47 String normalizedLocale = locale; |
| 47 normalizedLocale.replace('-', '_'); | 48 normalizedLocale.replace('-', '_'); |
| 48 size_t separatorPosition = normalizedLocale.find('_'); | 49 size_t separatorPosition = normalizedLocale.find('_'); |
| 49 if (separatorPosition == kNotFound) | 50 if (separatorPosition == kNotFound) |
| 50 return normalizedLocale; | 51 return normalizedLocale; |
| 51 return normalizedLocale.left(separatorPosition); | 52 return normalizedLocale.left(separatorPosition); |
| 52 } | 53 } |
| 53 | 54 |
| 54 static RetainPtr<NSLocale> determineLocale(const String& locale) | 55 static RetainPtr<NSLocale> determineLocale(const String& locale) |
| 55 { | 56 { |
| 56 if (!LayoutTestSupport::isRunningLayoutTest()) { | 57 if (!LayoutTestSupport::isRunningLayoutTest()) { |
| 57 RetainPtr<NSLocale> currentLocale = [NSLocale currentLocale]; | 58 RetainPtr<NSLocale> currentLocale = [NSLocale currentLocale]; |
| 58 String currentLocaleLanguage = languageFromLocale(String([currentLocale.
get() localeIdentifier])); | 59 String currentLocaleLanguage = languageFromLocale(String([currentLocale.
get() localeIdentifier])); |
| 59 String localeLanguage = languageFromLocale(locale); | 60 String localeLanguage = languageFromLocale(locale); |
| 60 if (equalIgnoringCase(currentLocaleLanguage, localeLanguage)) | 61 if (equalIgnoringCase(currentLocaleLanguage, localeLanguage)) |
| 61 return currentLocale; | 62 return currentLocale; |
| 62 } | 63 } |
| 63 // It seems initWithLocaleIdentifier accepts dash-separated locale identifie
r. | 64 // It seems initWithLocaleIdentifier accepts dash-separated locale identifie
r. |
| 64 return RetainPtr<NSLocale>(AdoptNS, [[NSLocale alloc] initWithLocaleIdentifi
er:locale]); | 65 return RetainPtr<NSLocale>(AdoptNS, [[NSLocale alloc] initWithLocaleIdentifi
er:locale]); |
| 65 } | 66 } |
| 66 | 67 |
| 67 PassOwnPtr<Locale> Locale::create(const String& locale) | 68 std::unique_ptr<Locale> Locale::create(const String& locale) |
| 68 { | 69 { |
| 69 return LocaleMac::create(determineLocale(locale).get()); | 70 return LocaleMac::create(determineLocale(locale).get()); |
| 70 } | 71 } |
| 71 | 72 |
| 72 static RetainPtr<NSDateFormatter> createDateTimeFormatter(NSLocale* locale, NSCa
lendar* calendar, NSDateFormatterStyle dateStyle, NSDateFormatterStyle timeStyle
) | 73 static RetainPtr<NSDateFormatter> createDateTimeFormatter(NSLocale* locale, NSCa
lendar* calendar, NSDateFormatterStyle dateStyle, NSDateFormatterStyle timeStyle
) |
| 73 { | 74 { |
| 74 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; | 75 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; |
| 75 [formatter setLocale:locale]; | 76 [formatter setLocale:locale]; |
| 76 [formatter setDateStyle:dateStyle]; | 77 [formatter setDateStyle:dateStyle]; |
| 77 [formatter setTimeStyle:timeStyle]; | 78 [formatter setTimeStyle:timeStyle]; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 90 NSString* language = [m_locale.get() objectForKey:NSLocaleLanguageCode]; | 91 NSString* language = [m_locale.get() objectForKey:NSLocaleLanguageCode]; |
| 91 if ([availableLanguages indexOfObject:language] == NSNotFound) | 92 if ([availableLanguages indexOfObject:language] == NSNotFound) |
| 92 m_locale.adoptNS([[NSLocale alloc] initWithLocaleIdentifier:defaultLangu
age()]); | 93 m_locale.adoptNS([[NSLocale alloc] initWithLocaleIdentifier:defaultLangu
age()]); |
| 93 [m_gregorianCalendar.get() setLocale:m_locale.get()]; | 94 [m_gregorianCalendar.get() setLocale:m_locale.get()]; |
| 94 } | 95 } |
| 95 | 96 |
| 96 LocaleMac::~LocaleMac() | 97 LocaleMac::~LocaleMac() |
| 97 { | 98 { |
| 98 } | 99 } |
| 99 | 100 |
| 100 PassOwnPtr<LocaleMac> LocaleMac::create(const String& localeIdentifier) | 101 std::unique_ptr<LocaleMac> LocaleMac::create(const String& localeIdentifier) |
| 101 { | 102 { |
| 102 RetainPtr<NSLocale> locale = [[NSLocale alloc] initWithLocaleIdentifier:loca
leIdentifier]; | 103 RetainPtr<NSLocale> locale = [[NSLocale alloc] initWithLocaleIdentifier:loca
leIdentifier]; |
| 103 return adoptPtr(new LocaleMac(locale.get())); | 104 return wrapUnique(new LocaleMac(locale.get())); |
| 104 } | 105 } |
| 105 | 106 |
| 106 PassOwnPtr<LocaleMac> LocaleMac::create(NSLocale* locale) | 107 std::unique_ptr<LocaleMac> LocaleMac::create(NSLocale* locale) |
| 107 { | 108 { |
| 108 return adoptPtr(new LocaleMac(locale)); | 109 return wrapUnique(new LocaleMac(locale)); |
| 109 } | 110 } |
| 110 | 111 |
| 111 RetainPtr<NSDateFormatter> LocaleMac::shortDateFormatter() | 112 RetainPtr<NSDateFormatter> LocaleMac::shortDateFormatter() |
| 112 { | 113 { |
| 113 return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NS
DateFormatterShortStyle, NSDateFormatterNoStyle); | 114 return createDateTimeFormatter(m_locale.get(), m_gregorianCalendar.get(), NS
DateFormatterShortStyle, NSDateFormatterNoStyle); |
| 114 } | 115 } |
| 115 | 116 |
| 116 const Vector<String>& LocaleMac::monthLabels() | 117 const Vector<String>& LocaleMac::monthLabels() |
| 117 { | 118 { |
| 118 if (!m_monthLabels.isEmpty()) | 119 if (!m_monthLabels.isEmpty()) |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 ASSERT(symbols.size() == DecimalSymbolsSize); | 321 ASSERT(symbols.size() == DecimalSymbolsSize); |
| 321 | 322 |
| 322 String positivePrefix([formatter.get() positivePrefix]); | 323 String positivePrefix([formatter.get() positivePrefix]); |
| 323 String positiveSuffix([formatter.get() positiveSuffix]); | 324 String positiveSuffix([formatter.get() positiveSuffix]); |
| 324 String negativePrefix([formatter.get() negativePrefix]); | 325 String negativePrefix([formatter.get() negativePrefix]); |
| 325 String negativeSuffix([formatter.get() negativeSuffix]); | 326 String negativeSuffix([formatter.get() negativeSuffix]); |
| 326 setLocaleData(symbols, positivePrefix, positiveSuffix, negativePrefix, negat
iveSuffix); | 327 setLocaleData(symbols, positivePrefix, positiveSuffix, negativePrefix, negat
iveSuffix); |
| 327 } | 328 } |
| 328 | 329 |
| 329 } | 330 } |
| OLD | NEW |