Skip to content

Fixed NSString/Swift.String Bridging in Locale
 #1474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CoreFoundation/Locale.subproj/CFLocale.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ _CFLocaleCalendarDirection _CFLocaleGetCalendarDirection(void) {
#endif
}

#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesArray) {
CFMutableArrayRef newArray = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
if (languagesArray && (CFArrayGetTypeID() == CFGetTypeID(languagesArray))) {
Expand All @@ -1100,7 +1100,7 @@ static CFArrayRef _CFLocaleCopyPreferredLanguagesFromPrefs(CFArrayRef languagesA
#endif

CFArrayRef CFLocaleCopyPreferredLanguages(void) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
CFArrayRef languagesArray = (CFArrayRef)CFPreferencesCopyAppValue(CFSTR("AppleLanguages"), kCFPreferencesCurrentApplication);
CFArrayRef result = _CFLocaleCopyPreferredLanguagesFromPrefs(languagesArray);
if (languagesArray) CFRelease(languagesArray);
Expand Down
45 changes: 7 additions & 38 deletions Foundation/NSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,62 +99,31 @@ extension NSLocale {
}

open class var availableLocaleIdentifiers: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyAvailableLocaleIdentifiers()._nsObject {
identifiers.append(obj as! String)
}
return identifiers
return _SwiftValue.fetch(CFLocaleCopyAvailableLocaleIdentifiers()) as? [String] ?? []
}

open class var isoLanguageCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOLanguageCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
return _SwiftValue.fetch(CFLocaleCopyISOLanguageCodes()) as? [String] ?? []
}

open class var isoCountryCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOCountryCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
return _SwiftValue.fetch(CFLocaleCopyISOCountryCodes()) as? [String] ?? []
}

open class var isoCurrencyCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOCurrencyCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
return _SwiftValue.fetch(CFLocaleCopyISOCurrencyCodes()) as? [String] ?? []
}

open class var commonISOCurrencyCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyCommonISOCurrencyCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
return _SwiftValue.fetch(CFLocaleCopyCommonISOCurrencyCodes()) as? [String] ?? []
}

open class var preferredLanguages: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyPreferredLanguages()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
return _SwiftValue.fetch(CFLocaleCopyPreferredLanguages()) as? [String] ?? []
}

open class func components(fromLocaleIdentifier string: String) -> [String : String] {
var comps = Dictionary<String, String>()
let values = CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, string._cfObject)._nsObject
values.enumerateKeysAndObjects(options: []) { (k, v, stop) in
let key = (k as! NSString)._swiftObject
let value = (v as! NSString)._swiftObject
comps[key] = value
}
return comps
return _SwiftValue.fetch(CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, string._cfObject)) as? [String : String] ?? [:]
}

open class func localeIdentifier(fromComponents dict: [String : String]) -> String {
Expand Down
27 changes: 23 additions & 4 deletions TestFoundation/TestNSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestNSLocale : XCTestCase {
("test_constants", test_constants),
("test_Identifier", test_Identifier),
("test_copy", test_copy),
("test_availableIdentifiers", test_availableIdentifiers),
("test_staticProperties", test_staticProperties),
("test_localeProperties", test_localeProperties),
]
}
Expand Down Expand Up @@ -110,10 +110,29 @@ class TestNSLocale : XCTestCase {
XCTAssertTrue(locale == localeCopy)
}

func test_availableIdentifiers() {
XCTAssertNoThrow(Locale.availableIdentifiers)
func test_staticProperties() {
let euroCurrencyCode = "EUR"
let spainRegionCode = "ES"
let galicianLanguageCode = "gl"
let galicianLocaleIdentifier = Locale.identifier(fromComponents: [NSLocale.Key.languageCode.rawValue: galicianLanguageCode,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the hoop jump through languageCode.rawValue here? The argument type is [String:String]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestions? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the types are all consistent with Darwin :( so, not an issue that concerns this patch specifically.

NSLocale.Key.countryCode.rawValue: spainRegionCode])

XCTAssertTrue(galicianLocaleIdentifier == "\(galicianLanguageCode)_\(spainRegionCode)")

let components = Locale.components(fromIdentifier: galicianLocaleIdentifier)

XCTAssertTrue(components[NSLocale.Key.languageCode.rawValue] == galicianLanguageCode)
XCTAssertTrue(components[NSLocale.Key.countryCode.rawValue] == spainRegionCode)

XCTAssertTrue(Locale.availableIdentifiers.contains(galicianLocaleIdentifier))
XCTAssertTrue(Locale.commonISOCurrencyCodes.contains(euroCurrencyCode))
XCTAssertTrue(Locale.isoCurrencyCodes.contains(euroCurrencyCode))
XCTAssertTrue(Locale.isoRegionCodes.contains(spainRegionCode))
XCTAssertTrue(Locale.isoLanguageCodes.contains(galicianLanguageCode))

XCTAssertTrue(Locale.preferredLanguages.count == UserDefaults.standard.array(forKey: "AppleLanguages")?.count ?? 0)
}

func test_localeProperties(){
#if os(Android)
XCTFail("Locale lookup unavailable on Android")
Expand Down