-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathNSLocale.swift
285 lines (237 loc) · 10.3 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
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
// 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 {
typealias CFType = CFLocale
private var _base = _CFInfo(typeID: CFLocaleGetTypeID())
private var _identifier: UnsafeMutableRawPointer? = nil
private var _cache: UnsafeMutableRawPointer? = nil
private var _prefs: UnsafeMutableRawPointer? = nil
#if os(OSX) || os(iOS)
private var _lock = pthread_mutex_t()
#elseif os(Linux)
private var _lock = Int32(0)
#endif
private var _nullLocale = false
internal var _cfObject: CFType {
return unsafeBitCast(self, to: CFType.self)
}
open func object(forKey key: NSLocale.Key) -> AnyObject? {
return 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) {
if aDecoder.allowsKeyedCoding {
guard let identifier = aDecoder.decodeObject(of: NSString.self, forKey: "NS.identifier") else {
return nil
}
self.init(localeIdentifier: String._unconditionallyBridgeFromObjectiveC(identifier))
} else {
NSUnimplemented()
}
}
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
}
open func encode(with aCoder: NSCoder) {
if aCoder.allowsKeyedCoding {
let identifier = CFLocaleGetIdentifier(self._cfObject)._nsObject
aCoder.encode(identifier, forKey: "NS.identifier")
} else {
NSUnimplemented()
}
}
public static var supportsSecureCoding: Bool {
return true
}
}
extension NSLocale {
open class var current: Locale {
return CFLocaleCopyCurrent()._swiftObject
}
open class func systemLocale() -> Locale {
return CFLocaleGetSystem()._swiftObject
}
}
extension NSLocale {
public var localeIdentifier: String {
return (object(forKey: .identifier) as! NSString)._swiftObject
}
open class var availableLocaleIdentifiers: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyAvailableLocaleIdentifiers()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
}
open class var isoLanguageCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOLanguageCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
}
open class var isoCountryCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOCountryCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
}
open class var isoCurrencyCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyISOCurrencyCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
}
open class var commonISOCurrencyCodes: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyCommonISOCurrencyCodes()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
}
open class var preferredLanguages: [String] {
var identifiers = Array<String>()
for obj in CFLocaleCopyPreferredLanguages()._nsObject {
identifiers.append((obj as! NSString)._swiftObject)
}
return identifiers
}
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
}
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(OSX) || 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(OSX) || 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, Comparable {
public private(set) var rawValue: String
public init(rawValue: String) {
self.rawValue = rawValue
}
public var hashValue: Int {
return rawValue.hashValue
}
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
}
}
public func ==(_ lhs: NSLocale.Key, _ rhs: NSLocale.Key) -> Bool {
return lhs.rawValue == rhs.rawValue
}
public func <(_ lhs: NSLocale.Key, _ rhs: NSLocale.Key) -> Bool {
return lhs.rawValue < rhs.rawValue
}
public let NSCurrentLocaleDidChangeNotification: String = "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)
}
}