forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSLocale.swift
246 lines (200 loc) · 9.22 KB
/
NSLocale.swift
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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
import CoreFoundation
open class NSLocale: NSObject, NSCopying, NSSecureCoding, _CFBridgeable {
typealias CFType = CFLocale
// struct __CFLocale
private var _base = _CFInfo(typeID: CFLocaleGetTypeID())
private var _identifier: UnsafeMutableRawPointer? = nil
private var _cache: UnsafeMutableRawPointer? = nil
private var _prefs: UnsafeMutableRawPointer? = nil
private var _lock: CFLock_t = __CFLockInit()
private var _nullLocale: Bool = false
internal var _cfObject: CFType {
return unsafeBitCast(self, to: CFType.self)
}
open func object(forKey key: NSLocale.Key) -> Any? {
return __SwiftValue.fetch(CFLocaleGetValue(_cfObject, key.rawValue._cfObject))
}
open func displayName(forKey key: Key, value: String) -> String? {
return CFLocaleCopyDisplayNameForPropertyValue(_cfObject, key.rawValue._cfObject, value._cfObject)?._swiftObject
}
public init(localeIdentifier string: String) {
super.init()
_CFLocaleInit(_cfObject, string._cfObject)
}
public required convenience init?(coder aDecoder: NSCoder) {
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
guard let identifier = aDecoder.decodeObject(of: NSString.self, forKey: "NS.identifier") else {
return nil
}
self.init(localeIdentifier: String._unconditionallyBridgeFromObjectiveC(identifier))
}
deinit {
_CFDeinit(self)
}
open override func copy() -> Any {
return copy(with: nil)
}
open func copy(with zone: NSZone? = nil) -> Any {
return self
}
override open func isEqual(_ object: Any?) -> Bool {
guard let locale = object as? NSLocale else {
return false
}
return locale.localeIdentifier == localeIdentifier
}
override open var hash: Int {
return localeIdentifier.hash
}
open func encode(with aCoder: NSCoder) {
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
let identifier = CFLocaleGetIdentifier(self._cfObject)._nsObject
aCoder.encode(identifier, forKey: "NS.identifier")
}
public static var supportsSecureCoding: Bool {
return true
}
}
extension NSLocale {
open class var current: Locale {
return CFLocaleCopyCurrent()._swiftObject
}
open class var system: Locale {
return CFLocaleGetSystem()._swiftObject
}
}
extension NSLocale {
public var localeIdentifier: String {
return object(forKey: .identifier) as! String
}
open class var availableLocaleIdentifiers: [String] {
return __SwiftValue.fetch(CFLocaleCopyAvailableLocaleIdentifiers()) as? [String] ?? []
}
open class var isoLanguageCodes: [String] {
return __SwiftValue.fetch(CFLocaleCopyISOLanguageCodes()) as? [String] ?? []
}
open class var isoCountryCodes: [String] {
return __SwiftValue.fetch(CFLocaleCopyISOCountryCodes()) as? [String] ?? []
}
open class var isoCurrencyCodes: [String] {
return __SwiftValue.fetch(CFLocaleCopyISOCurrencyCodes()) as? [String] ?? []
}
open class var commonISOCurrencyCodes: [String] {
return __SwiftValue.fetch(CFLocaleCopyCommonISOCurrencyCodes()) as? [String] ?? []
}
open class var preferredLanguages: [String] {
return __SwiftValue.fetch(CFLocaleCopyPreferredLanguages()) as? [String] ?? []
}
open class func components(fromLocaleIdentifier string: String) -> [String : String] {
return __SwiftValue.fetch(CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, string._cfObject)) as? [String : String] ?? [:]
}
open class func localeIdentifier(fromComponents dict: [String : String]) -> String {
return CFLocaleCreateLocaleIdentifierFromComponents(kCFAllocatorSystemDefault, dict._cfObject)._swiftObject
}
open class func canonicalLocaleIdentifier(from string: String) -> String {
return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorSystemDefault, string._cfObject)._swiftObject
}
open class func canonicalLanguageIdentifier(from string: String) -> String {
return CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorSystemDefault, string._cfObject)._swiftObject
}
open class func localeIdentifier(fromWindowsLocaleCode lcid: UInt32) -> String? {
return CFLocaleCreateLocaleIdentifierFromWindowsLocaleCode(kCFAllocatorSystemDefault, lcid)._swiftObject
}
open class func windowsLocaleCode(fromLocaleIdentifier localeIdentifier: String) -> UInt32 {
return CFLocaleGetWindowsLocaleCodeFromLocaleIdentifier(localeIdentifier._cfObject)
}
open class func characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
let dir = CFLocaleGetLanguageCharacterDirection(isoLangCode._cfObject)
#if os(macOS) || os(iOS)
return NSLocale.LanguageDirection(rawValue: UInt(dir.rawValue))!
#else
return NSLocale.LanguageDirection(rawValue: UInt(dir))!
#endif
}
open class func lineDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
let dir = CFLocaleGetLanguageLineDirection(isoLangCode._cfObject)
#if os(macOS) || os(iOS)
return NSLocale.LanguageDirection(rawValue: UInt(dir.rawValue))!
#else
return NSLocale.LanguageDirection(rawValue: UInt(dir))!
#endif
}
}
extension NSLocale {
public struct Key : RawRepresentable, Equatable, Hashable {
public private(set) var rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
public static let identifier = NSLocale.Key(rawValue: "kCFLocaleIdentifierKey")
public static let languageCode = NSLocale.Key(rawValue: "kCFLocaleLanguageCodeKey")
public static let countryCode = NSLocale.Key(rawValue: "kCFLocaleCountryCodeKey")
public static let scriptCode = NSLocale.Key(rawValue: "kCFLocaleScriptCodeKey")
public static let variantCode = NSLocale.Key(rawValue: "kCFLocaleVariantCodeKey")
public static let exemplarCharacterSet = NSLocale.Key(rawValue: "kCFLocaleExemplarCharacterSetKey")
public static let calendar = NSLocale.Key(rawValue: "kCFLocaleCalendarKey")
public static let collationIdentifier = NSLocale.Key(rawValue: "collation")
public static let usesMetricSystem = NSLocale.Key(rawValue: "kCFLocaleUsesMetricSystemKey")
public static let measurementSystem = NSLocale.Key(rawValue: "kCFLocaleMeasurementSystemKey")
public static let decimalSeparator = NSLocale.Key(rawValue: "kCFLocaleDecimalSeparatorKey")
public static let groupingSeparator = NSLocale.Key(rawValue: "kCFLocaleGroupingSeparatorKey")
public static let currencySymbol = NSLocale.Key(rawValue: "kCFLocaleCurrencySymbolKey")
public static let currencyCode = NSLocale.Key(rawValue: "currency")
public static let collatorIdentifier = NSLocale.Key(rawValue: "kCFLocaleCollatorIdentifierKey")
public static let quotationBeginDelimiterKey = NSLocale.Key(rawValue: "kCFLocaleQuotationBeginDelimiterKey")
public static let quotationEndDelimiterKey = NSLocale.Key(rawValue: "kCFLocaleQuotationEndDelimiterKey")
public static let calendarIdentifier = NSLocale.Key(rawValue: "kCFLocaleCalendarIdentifierKey")
public static let alternateQuotationBeginDelimiterKey = NSLocale.Key(rawValue: "kCFLocaleAlternateQuotationBeginDelimiterKey")
public static let alternateQuotationEndDelimiterKey = NSLocale.Key(rawValue: "kCFLocaleAlternateQuotationEndDelimiterKey")
}
public enum LanguageDirection : UInt {
case unknown
case leftToRight
case rightToLeft
case topToBottom
case bottomToTop
}
}
extension NSLocale {
public static let currentLocaleDidChangeNotification = NSNotification.Name(rawValue: "kCFLocaleCurrentLocaleDidChangeNotification")
}
extension CFLocale : _NSBridgeable, _SwiftBridgeable {
typealias NSType = NSLocale
typealias SwiftType = Locale
internal var _nsObject: NSLocale {
return unsafeBitCast(self, to: NSType.self)
}
internal var _swiftObject: Locale {
return _nsObject._swiftObject
}
}
extension NSLocale : _SwiftBridgeable {
typealias SwiftType = Locale
internal var _swiftObject: Locale {
return Locale(reference: self)
}
}
extension Locale : _CFBridgeable {
typealias CFType = CFLocale
internal var _cfObject: CFLocale {
return _bridgeToObjectiveC()._cfObject
}
}
extension NSLocale : _StructTypeBridgeable {
public typealias _StructType = Locale
public func _bridgeToSwift() -> Locale {
return Locale._unconditionallyBridgeFromObjectiveC(self)
}
}