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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
// 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
//
@_implementationOnly import CoreFoundation
@_spi(SwiftCorelibsFoundation) @_exported import FoundationEssentials
@_exported import FoundationInternationalization
open class NSLocale: NSObject, NSCopying, NSSecureCoding, @unchecked Sendable {
// Our own data
let _locale: Locale
internal init(locale: Locale) {
_locale = locale
}
open func object(forKey key: NSLocale.Key) -> Any? {
switch key {
case .identifier: return self.localeIdentifier
case .languageCode: return self.languageCode
case .countryCode: return self.countryCode
case .scriptCode: return self.scriptCode
case .variantCode: return self.variantCode
#if FOUNDATION_FRAMEWORK
case .exemplarCharacterSet: return self.exemplarCharacterSet
#endif
case .calendarIdentifier: return self.calendarIdentifier
case .calendar: return _locale.calendar
case .collationIdentifier: return self.collationIdentifier
case .usesMetricSystem: return self.usesMetricSystem
#if FOUNDATION_FRAMEWORK
case .measurementSystem: return self.measurementSystem
#endif
case .decimalSeparator: return self.decimalSeparator
case .groupingSeparator: return self.groupingSeparator
case .currencySymbol: return self.currencySymbol
case .currencyCode: return self.currencyCode
// Foundation framework only
/*
case .collatorIdentifier, .cfLocaleCollatorID: return self.collatorIdentifier
*/
case .quotationBeginDelimiterKey: return self.quotationBeginDelimiter
case .quotationEndDelimiterKey: return self.quotationEndDelimiter
case .alternateQuotationBeginDelimiterKey: return self.alternateQuotationBeginDelimiter
case .alternateQuotationEndDelimiterKey: return self.alternateQuotationEndDelimiter
// Foundation framework only
/*
case .languageIdentifier: return self.languageIdentifier
*/
default:
return nil
}
}
open func displayName(forKey key: Key, value: String) -> String? {
switch key {
case .identifier: return self._nullableLocalizedString(forLocaleIdentifier: value)
case .languageCode: return self.localizedString(forLanguageCode: value)
case .countryCode: return self.localizedString(forCountryCode: value)
case .scriptCode: return self.localizedString(forScriptCode: value)
case .variantCode: return self.localizedString(forVariantCode: value)
#if FOUNDATION_FRAMEWORK
case .exemplarCharacterSet: return nil
#endif
case .calendarIdentifier, .calendar: return self.localizedString(forCalendarIdentifier: value)
case .collationIdentifier: return self.localizedString(forCollationIdentifier: value)
case .usesMetricSystem: return nil
case .measurementSystem: return nil
case .decimalSeparator: return nil
case .groupingSeparator: return nil
case .currencySymbol: return self.localizedString(forCurrencySymbol: value)
case .currencyCode: return self.localizedString(forCurrencyCode: value)
case .collatorIdentifier: return self.localizedString(forCollatorIdentifier: value)
case .quotationBeginDelimiterKey: return nil
case .quotationEndDelimiterKey: return nil
case .alternateQuotationBeginDelimiterKey: return nil
case .alternateQuotationEndDelimiterKey: return nil
default:
return nil
}
}
var localeIdentifier: String {
_locale.identifier
}
var languageCode: String {
_locale.languageCode ?? ""
}
var languageIdentifier: String {
let langIdentifier = _locale.language._components._identifier
let localeWithOnlyLanguage = Locale(identifier: langIdentifier)
return localeWithOnlyLanguage.identifier(.bcp47)
}
var countryCode: String? {
_locale.region?.identifier
}
var regionCode: String? {
_locale.region?.identifier
}
var scriptCode: String? {
_locale.scriptCode
}
var variantCode: String? {
_locale.variantCode
}
var calendarIdentifier: String {
_locale.__calendarIdentifier._cfCalendarIdentifier
}
var collationIdentifier: String? {
_locale.collationIdentifier
}
var decimalSeparator: String {
_locale.decimalSeparator ?? ""
}
var groupingSeparator: String {
_locale.groupingSeparator ?? ""
}
var currencySymbol: String {
_locale.currencySymbol ?? ""
}
var currencyCode: String? {
_locale.currencyCode
}
var collatorIdentifier: String {
_locale.collatorIdentifier ?? ""
}
var quotationBeginDelimiter: String {
_locale.quotationBeginDelimiter ?? ""
}
var quotationEndDelimiter: String {
_locale.quotationEndDelimiter ?? ""
}
var alternateQuotationBeginDelimiter: String {
_locale.alternateQuotationBeginDelimiter ?? ""
}
var alternateQuotationEndDelimiter: String {
_locale.alternateQuotationEndDelimiter ?? ""
}
#if FOUNDATION_FRAMEWORK
var exemplarCharacterSet: CharacterSet {
_locale.exemplarCharacterSet ?? CharacterSet()
}
#endif
var usesMetricSystem: Bool {
_locale.usesMetricSystem
}
func localizedString(forLocaleIdentifier localeIdentifier: String) -> String {
_nullableLocalizedString(forLocaleIdentifier: localeIdentifier) ?? ""
}
/// Some CFLocale APIs require the result to remain `nullable`. They can call this directly, where the `localizedString(forLocaleIdentifier:)` entry point can remain (correctly) non-nullable.
private func _nullableLocalizedString(forLocaleIdentifier localeIdentifier: String) -> String? {
_locale.localizedString(forIdentifier: localeIdentifier)
}
func localizedString(forLanguageCode languageCode: String) -> String? {
_locale.localizedString(forLanguageCode: languageCode)
}
func localizedString(forCountryCode countryCode: String) -> String? {
_locale.localizedString(forRegionCode: countryCode)
}
func localizedString(forScriptCode scriptCode: String) -> String? {
_locale.localizedString(forScriptCode: scriptCode)
}
func localizedString(forVariantCode variantCode: String) -> String? {
_locale.localizedString(forVariantCode: variantCode)
}
func localizedString(forCalendarIdentifier calendarIdentifier: String) -> String? {
guard let id = NSCalendar.Identifier(string: calendarIdentifier), let id = id._calendarIdentifier else {
return nil
}
return _locale.localizedString(for: id)
}
func localizedString(forCollationIdentifier collationIdentifier: String) -> String? {
_locale.localizedString(forCollationIdentifier: collationIdentifier)
}
func localizedString(forCurrencyCode currencyCode: String) -> String? {
_locale.localizedString(forCurrencyCode: currencyCode)
}
func localizedString(forCurrencySymbol currencySymbol: String) -> String? {
// internal access for SCL-F only
_locale._localizedString(forCurrencySymbol: currencySymbol)
}
func localizedString(forCollatorIdentifier collatorIdentifier: String) -> String? {
_locale.localizedString(forCollatorIdentifier: collatorIdentifier)
}
public init(localeIdentifier string: String) {
_locale = Locale(identifier: string)
super.init()
}
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))
}
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.")
}
aCoder.encode(_locale.identifier as NSString, forKey: "NS.identifier")
}
public static var supportsSecureCoding: Bool {
return true
}
}
extension NSLocale {
public class var current: Locale {
Locale.current
}
public class var system: Locale {
Locale(identifier: "")
}
}
extension NSLocale {
public class var availableLocaleIdentifiers: [String] {
Locale.availableIdentifiers
}
public class var isoLanguageCodes: [String] {
// Map back from the type to strings
Locale.LanguageCode.isoLanguageCodes.map { $0.identifier }
}
public class var isoCountryCodes: [String] {
Locale.Region.isoRegions.map { $0.identifier }
}
public class var isoCurrencyCodes: [String] {
Locale.Currency.isoCurrencies.map { $0.identifier }
}
public class var commonISOCurrencyCodes: [String] {
Locale.commonISOCurrencyCodes
}
public class var preferredLanguages: [String] {
Locale.preferredLanguages
}
public class func components(fromLocaleIdentifier string: String) -> [String : String] {
__SwiftValue.fetch(CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorSystemDefault, string._cfObject)) as? [String : String] ?? [:]
}
public class func localeIdentifier(fromComponents dict: [String : String]) -> String {
Locale.identifier(fromComponents: dict)
}
public class func canonicalLocaleIdentifier(from string: String) -> String {
Locale.canonicalLanguageIdentifier(from: string)
}
public class func canonicalLanguageIdentifier(from string: String) -> String {
Locale.canonicalLanguageIdentifier(from: string)
}
public class func localeIdentifier(fromWindowsLocaleCode lcid: UInt32) -> String? {
Locale.identifier(fromWindowsLocaleCode: Int(lcid))
}
public class func windowsLocaleCode(fromLocaleIdentifier localeIdentifier: String) -> UInt32 {
if let code = Locale.windowsLocaleCode(fromIdentifier: localeIdentifier) {
return UInt32(code)
} else {
return UInt32.max
}
}
public class func characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
let language = Locale.Language(components: .init(identifier: isoLangCode))
return language.characterDirection
}
public class func lineDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
let language = Locale.Language(components: .init(identifier: isoLangCode))
return language.lineLayoutDirection
}
}
extension NSLocale {
public struct Key : RawRepresentable, Equatable, Hashable, Sendable {
public let 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 typealias LanguageDirection = Locale.LanguageDirection
}
#if !os(WASI)
extension NSLocale {
public static let currentLocaleDidChangeNotification = NSNotification.Name(rawValue: "kCFLocaleCurrentLocaleDidChangeNotification")
}
#endif
// MARK: - Deprecated Locale API
extension Locale {
/// Returns a list of available `Locale` language codes.
@available(*, deprecated, message: "Use `Locale.LanguageCode.isoLanguageCodes` instead")
public static var isoLanguageCodes: [String] {
NSLocale.isoLanguageCodes
}
/// Returns a dictionary that splits an identifier into its component pieces.
@available(*, deprecated, message: "Use `Locale.Components(identifier:)` to access components")
public static func components(fromIdentifier string: String) -> [String : String] {
NSLocale.components(fromLocaleIdentifier: string)
}
/// Returns a list of available `Locale` region codes.
@available(*, deprecated, message: "Use `Locale.Region.isoRegions` instead")
public static var isoRegionCodes: [String] {
NSLocale.isoCountryCodes
}
/// Returns a list of available `Locale` currency codes.
@available(*, deprecated, message: "Use `Locale.Currency.isoCurrencies` instead")
public static var isoCurrencyCodes: [String] {
NSLocale.isoCurrencyCodes
}
/// Returns the character direction for a specified language code.
@available(*, deprecated, message: "Use `Locale.Language(identifier:).characterDirection`")
public static func characterDirection(forLanguage isoLangCode: String) -> Locale.LanguageDirection {
NSLocale.characterDirection(forLanguage: isoLangCode)
}
/// Returns the line direction for a specified language code.
@available(*, deprecated, message: "Use `Locale.Language(identifier:).lineLayoutDirection`")
public static func lineDirection(forLanguage isoLangCode: String) -> Locale.LanguageDirection {
NSLocale.lineDirection(forLanguage: isoLangCode)
}
}
// MARK: - CF Conversions
extension CFLocale : _NSBridgeable, _SwiftBridgeable {
typealias NSType = NSLocale
typealias SwiftType = Locale
internal var _nsObject: NSLocale {
return NSLocale(localeIdentifier: CFLocaleGetIdentifier(self)._swiftObject)
}
internal var _swiftObject: Locale {
return _nsObject._swiftObject
}
}
extension Locale {
typealias CFType = CFLocale
internal var _cfObject: CFLocale {
return _bridgeToObjectiveC()._cfObject
}
}
extension NSLocale {
internal var _cfObject: CFLocale {
CFLocaleCreate(nil, self.localeIdentifier._cfObject)
}
}
// MARK: - Swift Conversions
extension NSLocale : _SwiftBridgeable {
typealias SwiftType = Locale
internal var _swiftObject: Locale {
return _locale
}
}
extension NSLocale : _StructTypeBridgeable {
public typealias _StructType = Locale
public func _bridgeToSwift() -> Locale {
return Locale._unconditionallyBridgeFromObjectiveC(self)
}
}
extension Locale : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSLocale {
return NSLocale(locale: self)
}
public static func _forceBridgeFromObjectiveC(_ input: NSLocale, result: inout Locale?) {
if !_conditionallyBridgeFromObjectiveC(input, result: &result) {
fatalError("Unable to bridge \(NSLocale.self) to \(self)")
}
}
public static func _conditionallyBridgeFromObjectiveC(_ input: NSLocale, result: inout Locale?) -> Bool {
result = input._locale
return true
}
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSLocale?) -> Locale {
var result: Locale? = nil
_forceBridgeFromObjectiveC(source!, result: &result)
return result!
}
}
|