| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2003, 2005, 2006, 2010, 2011 Apple 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'' | |
| 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | |
| 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | |
| 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | |
| 26 #import "config.h" | |
| 27 #import "Language.h" | |
| 28 | |
| 29 #import "BlockExceptions.h" | |
| 30 #import "WebCoreSystemInterface.h" | |
| 31 #import <wtf/Assertions.h> | |
| 32 #import <wtf/MainThread.h> | |
| 33 #import <wtf/RetainPtr.h> | |
| 34 #import <wtf/text/WTFString.h> | |
| 35 | |
| 36 using namespace WebCore; | |
| 37 | |
| 38 static BOOL useCachedPreferredLanguages; | |
| 39 | |
| 40 @interface WebLanguageChangeObserver : NSObject { | |
| 41 } | |
| 42 @end | |
| 43 | |
| 44 @implementation WebLanguageChangeObserver | |
| 45 | |
| 46 + (void)_webkit_languagePreferencesDidChange | |
| 47 { | |
| 48 ASSERT(isMainThread()); | |
| 49 | |
| 50 useCachedPreferredLanguages = NO; | |
| 51 | |
| 52 languageDidChange(); | |
| 53 } | |
| 54 | |
| 55 @end | |
| 56 | |
| 57 namespace WebCore { | |
| 58 | |
| 59 static String httpStyleLanguageCode(NSString *languageCode) | |
| 60 { | |
| 61 ASSERT(isMainThread()); | |
| 62 | |
| 63 // Look up the language code using CFBundle. | |
| 64 RetainPtr<CFStringRef> preferredLanguageCode(AdoptCF, wkCopyCFLocalizationPr
eferredName((CFStringRef)languageCode)); | |
| 65 | |
| 66 if (preferredLanguageCode) | |
| 67 languageCode = (NSString *)preferredLanguageCode.get(); | |
| 68 | |
| 69 // Make the string lowercase. | |
| 70 NSString *lowercaseLanguageCode = [languageCode lowercaseString]; | |
| 71 | |
| 72 // Turn a '_' into a '-' if it appears after a 2-letter language code. | |
| 73 if ([lowercaseLanguageCode length] >= 3 && [lowercaseLanguageCode characterA
tIndex:2] == '_') { | |
| 74 RetainPtr<NSMutableString> mutableLanguageCode(AdoptNS, [lowercaseLangua
geCode mutableCopy]); | |
| 75 [mutableLanguageCode.get() replaceCharactersInRange:NSMakeRange(2, 1) wi
thString:@"-"]; | |
| 76 return mutableLanguageCode.get(); | |
| 77 } | |
| 78 | |
| 79 return lowercaseLanguageCode; | |
| 80 } | |
| 81 | |
| 82 Vector<String> platformUserPreferredLanguages() | |
| 83 { | |
| 84 DEFINE_STATIC_LOCAL(Vector<String>, userPreferredLanguages, ()); | |
| 85 | |
| 86 ASSERT(isMainThread()); | |
| 87 | |
| 88 BEGIN_BLOCK_OBJC_EXCEPTIONS; | |
| 89 | |
| 90 if (!useCachedPreferredLanguages) { | |
| 91 useCachedPreferredLanguages = YES; | |
| 92 userPreferredLanguages.clear(); | |
| 93 | |
| 94 RetainPtr<CFArrayRef> languages(AdoptCF, CFLocaleCopyPreferredLanguages(
)); | |
| 95 CFIndex languageCount = CFArrayGetCount(languages.get()); | |
| 96 if (!languageCount) | |
| 97 userPreferredLanguages.append("en"); | |
| 98 else { | |
| 99 for (CFIndex i = 0; i < languageCount; i++) | |
| 100 userPreferredLanguages.append(httpStyleLanguageCode((NSString *)
CFArrayGetValueAtIndex(languages.get(), i))); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 #if !PLATFORM(IOS) | |
| 105 static bool languageChangeObserverAdded; | |
| 106 if (!languageChangeObserverAdded) { | |
| 107 [[NSDistributedNotificationCenter defaultCenter] addObserver:[WebLanguag
eChangeObserver self] | |
| 108 selector:@selector(_
webkit_languagePreferencesDidChange) | |
| 109 name:@"AppleLang
uagePreferencesChangedNotification" | |
| 110 object:nil]; | |
| 111 languageChangeObserverAdded = true; | |
| 112 } | |
| 113 #endif // !PLATFORM(IOS) | |
| 114 | |
| 115 return userPreferredLanguages; | |
| 116 | |
| 117 END_BLOCK_OBJC_EXCEPTIONS; | |
| 118 | |
| 119 return Vector<String>(); | |
| 120 } | |
| 121 | |
| 122 } | |
| OLD | NEW |