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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/**
`Locale` encapsulates information about linguistic, cultural, and technological conventions and standards. Examples of information encapsulated by a locale include the symbol used for the decimal separator in numbers and the way dates are formatted.
Locales are typically used to provide, format, and interpret information about and according to the user's customs and preferences. They are frequently used in conjunction with formatters. Although you can use many locales, you usually use the one associated with the current user.
*/
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
public struct Locale : Hashable, Equatable, Sendable {
#if FOUNDATION_FRAMEWORK
public typealias LanguageDirection = NSLocale.LanguageDirection
#else
public enum LanguageDirection : UInt, Sendable {
/// The direction of the language is unknown.
case unknown
/// The language direction is from left to right.
case leftToRight
/// The language direction is from right to left.
case rightToLeft
/// The language direction is from top to bottom.
case topToBottom
/// The language direction is from bottom to top.
case bottomToTop
}
#endif
// Identifier canonicalization
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public enum IdentifierType : Sendable {
/* This canonicalizes the identifier */
/// Identifier following ICU (International Components for Unicode) convention. Unlike the `posix` type, does not include character set
/// For example: "th_TH@calendar=gregorian;numbers=thai"
case icu
/* This would be a canonicalized "Unicode BCP 47 locale identifier", not a "BCP 47 language tag", per https://www.unicode.org/reports/tr35/#BCP_47_Language_Tag_Conversion */
/// The identifier is also a valid BCP47 language tag
/// For example: "th-TH-u-ca-gregory-nu-thai"
case bcp47
/// The components are the same as `icu`, but does not use the key-value type keyword list
/// For example: "th_TH_u_ca_gregory_nu_thai"
case cldr
}
internal var _locale: any _LocaleProtocol
/// Returns a locale which tracks the user's current preferences.
///
/// If mutated, this Locale will no longer track the user's preferences.
///
/// - note: The autoupdating Locale will only compare equal to another autoupdating Locale.
public static var autoupdatingCurrent : Locale {
Locale(inner: LocaleCache.cache.autoupdatingCurrent)
}
/// Returns the user's current locale.
public static var current : Locale {
Locale(inner: LocaleCache.cache.current)
}
/// System locale.
internal static var system : Locale {
Locale(inner: LocaleCache.cache.system)
}
/// Unlocalized locale (`en_001`).
internal static var unlocalized : Locale {
Locale(inner: LocaleCache.cache.unlocalized)
}
#if FOUNDATION_FRAMEWORK && canImport(_FoundationICU)
/// This returns an instance of `Locale` that's set up exactly like it would be if the user changed the current locale to that identifier, set the preferences keys in the overrides dictionary, then called `current`.
internal static func localeAsIfCurrent(name: String?, cfOverrides: CFDictionary? = nil, disableBundleMatching: Bool = false) -> Locale {
return LocaleCache.cache.localeAsIfCurrent(name: name, cfOverrides: cfOverrides, disableBundleMatching: disableBundleMatching)
}
#endif
/// This returns an instance of `Locale` that's set up exactly like it would be if the user changed the current locale to that identifier, set the preferences keys in the overrides dictionary, then called `current`.
internal static func localeAsIfCurrent(name: String?, overrides: LocalePreferences? = nil, disableBundleMatching: Bool = false) -> Locale {
// On Darwin, this overrides are applied on top of CFPreferences.
return LocaleCache.cache.localeAsIfCurrent(name: name, overrides: overrides, disableBundleMatching: disableBundleMatching)
}
internal static func localeAsIfCurrentWithBundleLocalizations(_ availableLocalizations: [String], allowsMixedLocalizations: Bool) -> Locale? {
return LocaleCache.cache.localeAsIfCurrentWithBundleLocalizations(availableLocalizations, allowsMixedLocalizations: allowsMixedLocalizations)
}
// MARK: -
//
/// Return a locale with the specified identifier.
public init(identifier: String) {
_locale = LocaleCache.cache.fixed(identifier)
}
/// Creates a `Locale` with the specified locale components
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public init(components: Locale.Components) {
_locale = LocaleCache.cache.fixedComponents(components)
}
/// Creates a `Locale` with the specified language components
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public init(languageComponents: Locale.Language.Components) {
self = Locale(identifier: languageComponents.identifier)
}
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public init(languageCode: LanguageCode? = nil, script: Script? = nil, languageRegion: Region? = nil) {
let comps = Components(languageCode: languageCode, script: script, languageRegion: languageRegion)
self = .init(components: comps)
}
#if FOUNDATION_FRAMEWORK
internal init(reference: __shared NSLocale) {
if let swift = reference as? _NSSwiftLocale {
_locale = swift.locale._locale
} else {
// This is a custom NSLocale subclass
_locale = _LocaleBridged(adoptingReference: reference)
}
}
#endif
/// Used by `Locale_Cache` for creating fixed `NSLocale` instances.
internal init(inner: any _LocaleProtocol) {
_locale = inner
}
/// Used by `Calendar` to get a `Locale` with a specific identifier and preferences.
package init(identifier: String, preferences: LocalePreferences?) {
self = LocaleCache.cache.localeWithPreferences(identifier: identifier, prefs: preferences)
}
/// Produce a copy of the `Locale` (including `LocalePreferences`, if present), but with a different `Calendar.Identifier`. Date formatting uses this.
internal func copy(newCalendarIdentifier identifier: Calendar.Identifier) -> Locale {
Locale(inner: _locale.copy(newCalendarIdentifier: identifier))
}
// MARK: -
//
/// Returns a localized string for a specified identifier.
///
/// For example, in the "en" locale, the result for `"es"` is `"Spanish"`.
public func localizedString(forIdentifier identifier: String) -> String? {
_locale.identifierDisplayName(for: identifier)
}
/// Returns a localized string for a specified language code.
///
/// For example, in the "en" locale, the result for `"es"` is `"Spanish"`.
public func localizedString(forLanguageCode languageCode: String) -> String? {
_locale.languageCodeDisplayName(for: languageCode)
}
/// Returns a localized string for a specified region code.
///
/// For example, in the "en" locale, the result for `"fr"` is `"France"`.
public func localizedString(forRegionCode regionCode: String) -> String? {
_locale.countryCodeDisplayName(for: regionCode)
}
/// Returns a localized string for a specified script code.
///
/// For example, in the "en" locale, the result for `"Hans"` is `"Simplified Han"`.
public func localizedString(forScriptCode scriptCode: String) -> String? {
_locale.scriptCodeDisplayName(for: scriptCode)
}
/// Returns a localized string for a specified variant code.
///
/// For example, in the "en" locale, the result for `"POSIX"` is `"Computer"`.
public func localizedString(forVariantCode variantCode: String) -> String? {
_locale.variantCodeDisplayName(for: variantCode)
}
/// Returns a localized string for a specified `Calendar.Identifier`.
///
/// For example, in the "en" locale, the result for `.buddhist` is `"Buddhist Calendar"`.
public func localizedString(for calendarIdentifier: Calendar.Identifier) -> String? {
_locale.calendarIdentifierDisplayName(for: calendarIdentifier)
}
/// Returns a localized string for a specified ISO 4217 currency code.
///
/// For example, in the "en" locale, the result for `"USD"` is `"US Dollar"`.
/// - seealso: `Locale.isoCurrencyCodes`
public func localizedString(forCurrencyCode currencyCode: String) -> String? {
_locale.currencyCodeDisplayName(for: currencyCode)
}
/// This exists in `NSLocale` via the `displayName` API, using the currency *symbol* key instead of *code*.
internal func localizedString(forCurrencySymbol currencySymbol: String) -> String? {
_locale.currencySymbolDisplayName(for: currencySymbol)
}
#if !FOUNDATION_FRAMEWORK
@_spi(SwiftCorelibsFoundation) public func _localizedString(forCurrencySymbol currencySymbol: String) -> String? {
localizedString(forCurrencySymbol: currencySymbol)
}
#endif
/// Returns a localized string for a specified ICU collation identifier.
///
/// For example, in the "en" locale, the result for `"phonebook"` is `"Phonebook Sort Order"`.
public func localizedString(forCollationIdentifier collationIdentifier: String) -> String? {
_locale.collationIdentifierDisplayName(for: collationIdentifier)
}
/// Returns a localized string for a specified ICU collator identifier.
public func localizedString(forCollatorIdentifier collatorIdentifier: String) -> String? {
_locale.collatorIdentifierDisplayName(for: collatorIdentifier)
}
// MARK: -
//
/// Returns the identifier of the locale.
public var identifier: String {
_locale.identifier
}
/// Returns the language code of the locale, or nil if has none.
///
/// For example, for the locale "zh-Hant-HK", returns "zh".
@available(macOS, deprecated: 13, renamed: "language.languageCode.identifier")
@available(iOS, deprecated: 16, renamed: "language.languageCode.identifier")
@available(tvOS, deprecated: 16, renamed: "language.languageCode.identifier")
@available(watchOS, deprecated: 9, renamed: "language.languageCode.identifier")
public var languageCode: String? {
_locale.languageCode
}
/// Returns the region code of the locale, or nil if it has none.
///
/// For example, for the locale "zh-Hant-HK", returns "HK".
@available(macOS, deprecated: 13, renamed: "region.identifier")
@available(iOS, deprecated: 16, renamed: "region.identifier")
@available(tvOS, deprecated: 16, renamed: "region.identifier")
@available(watchOS, deprecated: 9, renamed: "region.identifier")
public var regionCode: String? {
// n.b. this is called countryCode in ObjC
let result = _locale.regionCode
if let result, result.isEmpty {
return nil
}
return result
}
/// Returns the script code of the locale, or nil if has none.
///
/// For example, for the locale "zh-Hant-HK", returns "Hant".
@available(macOS, deprecated: 13, renamed: "language.script.identifier")
@available(iOS, deprecated: 16, renamed: "language.script.identifier")
@available(tvOS, deprecated: 16, renamed: "language.script.identifier")
@available(watchOS, deprecated: 9, renamed: "language.script.identifier")
public var scriptCode: String? {
_locale.scriptCode
}
/// Returns the variant code for the locale, or nil if it has none.
///
/// For example, for the locale "en_POSIX", returns "POSIX".
@available(macOS, deprecated: 13, renamed: "variant.identifier")
@available(iOS, deprecated: 16, renamed: "variant.identifier")
@available(tvOS, deprecated: 16, renamed: "variant.identifier")
@available(watchOS, deprecated: 9, renamed: "variant.identifier")
public var variantCode: String? {
let result = _locale.variantCode
if let result, result.isEmpty {
return nil
}
return result
}
#if FOUNDATION_FRAMEWORK
/// Returns the exemplar character set for the locale, or nil if has none.
public var exemplarCharacterSet: CharacterSet? {
_locale.exemplarCharacterSet
}
#endif
/// Returns the calendar for the locale, or the Gregorian calendar as a fallback.
public var calendar: Calendar {
var cal = _locale.calendar
// This is a fairly expensive operation, because it recreates the Calendar's backing ICU object.
// However, we can't cache `struct Calendar` because it would create a retain cycle between _Calendar/_Locale:
// struct Calendar -> inner _Calendar: any _CalendarProtocol -> struct Locale -> inner _Locale: any _LocaleProtocol -> struct Calendar...
// _Calendar holds a Locale for performance reasons
cal.locale = self
return cal
}
/// Returns the calendar identifier for the locale, or the Gregorian identifier as a fallback.
/// Useful if you need the identifier but not a full instance of the `Calendar`.
package var _calendarIdentifier: Calendar.Identifier {
_locale.calendarIdentifier
}
#if !FOUNDATION_FRAMEWORK
@_spi(SwiftCorelibsFoundation) public var __calendarIdentifier: Calendar.Identifier {
_calendarIdentifier
}
#endif
/// Returns the collation identifier for the locale, or nil if it has none.
///
/// For example, for the locale "en_US@collation=phonebook", returns "phonebook".
@available(macOS, deprecated: 13, renamed: "collation.identifier")
@available(iOS, deprecated: 16, renamed: "collation.identifier")
@available(tvOS, deprecated: 16, renamed: "collation.identifier")
@available(watchOS, deprecated: 9, renamed: "collation.identifier")
public var collationIdentifier: String? {
_locale.collationIdentifier
}
/// Returns true if the locale uses the metric system.
///
/// -seealso: MeasurementFormatter
@available(macOS, deprecated: 13, message: "Use `measurementSystem` instead")
@available(iOS, deprecated: 16, message: "Use `measurementSystem` instead")
@available(tvOS, deprecated: 16, message: "Use `measurementSystem` instead")
@available(watchOS, deprecated: 9, message: "Use `measurementSystem` instead")
public var usesMetricSystem: Bool {
_locale.usesMetricSystem
}
/// Returns the decimal separator of the locale.
///
/// For example, for "en_US", returns ".".
public var decimalSeparator: String? {
_locale.decimalSeparator
}
/// Returns the grouping separator of the locale.
///
/// For example, for "en_US", returns ",".
public var groupingSeparator: String? {
_locale.groupingSeparator
}
/// Returns the currency symbol of the locale.
///
/// For example, for "zh-Hant-HK", returns "HK$".
public var currencySymbol: String? {
_locale.currencySymbol
}
/// Returns the currency code of the locale.
///
/// For example, for "zh-Hant-HK", returns "HKD".
@available(macOS, deprecated: 13, renamed: "currency.identifier")
@available(iOS, deprecated: 16, renamed: "currency.identifier")
@available(tvOS, deprecated: 16, renamed: "currency.identifier")
@available(watchOS, deprecated: 9, renamed: "currency.identifier")
public var currencyCode: String? {
_locale.currencyCode
}
/// Returns the collator identifier of the locale.
public var collatorIdentifier: String? {
_locale.collatorIdentifier
}
/// Returns the quotation begin delimiter of the locale.
///
/// For example, returns `“` for "en_US", and `「` for "zh-Hant-HK".
public var quotationBeginDelimiter: String? {
_locale.quotationBeginDelimiter
}
/// Returns the quotation end delimiter of the locale.
///
/// For example, returns `”` for "en_US", and `」` for "zh-Hant-HK".
public var quotationEndDelimiter: String? {
_locale.quotationEndDelimiter
}
/// Returns the alternate quotation begin delimiter of the locale.
///
/// For example, returns `‘` for "en_US", and `『` for "zh-Hant-HK".
public var alternateQuotationBeginDelimiter: String? {
_locale.alternateQuotationBeginDelimiter
}
/// Returns the alternate quotation end delimiter of the locale.
///
/// For example, returns `’` for "en_US", and `』` for "zh-Hant-HK".
public var alternateQuotationEndDelimiter: String? {
_locale.alternateQuotationEndDelimiter
}
// MARK: - Components
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
/// Returns the measurement system of the locale. Returns `metric` as the default value if the data isn't available.
public var measurementSystem: MeasurementSystem {
_locale.measurementSystem
}
/// Returns the currency of the locale. Returns nil if the data isn't available.
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var currency: Currency? {
_locale.currency
}
/// Returns the numbering system of the locale. If the locale has an explicitly specified numbering system in the identifier (e.g. `bn_BD@numbers=latn`) or in the associated `Locale.Components`, that numbering system is returned. Otherwise, returns the default numbering system of the locale. Returns `"latn"` as the default value if the data isn't available.
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var numberingSystem: NumberingSystem {
_locale.numberingSystem
}
/// Returns all the valid numbering systems for the locale. For example, `"ar-AE (Arabic (United Arab Emirates)"` has both `"latn" (Latin digits)` and `"arab" (Arabic-Indic digits)` numbering system.
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var availableNumberingSystems: [NumberingSystem] {
_locale.availableNumberingSystems
}
/// Returns the first day of the week of the locale. Returns `.sunday` as the default value if the data isn't available to the requested locale.
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var firstDayOfWeek: Weekday {
_locale.firstDayOfWeek
}
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var language: Language {
_locale.language
}
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
/// Returns the identifier conforming to the specified standard
public func identifier(_ type: IdentifierType) -> String {
_locale.identifier(type)
}
/// Returns the hour cycle such as whether it uses 12-hour clock or 24-hour clock. Default is `.zeroToTwentyThree` if the data isn't available.
/// Calling this on `.current` or `.autoupdatingCurrent` returns user's preference values as set in the system settings if available, overriding the default value of the user's locale.
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var hourCycle: HourCycle {
_locale.hourCycle
}
/// Returns the default collation used by the locale. Default is `.standard`.
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var collation: Collation {
_locale.collation
}
/// Returns the region of the locale. For example, "US" for "en_US", "GB" for "en_GB", "PT" for "pt_PT".
///
///
/// note: Typically this is equivalent to the language region, unless there's an `rg` override in the locale identifier. For example, for "en_GB@rg=USzzzz", the language region is "GB", while the locale region is "US". `Language.region` represents the region variant of the language, such as "British English" in this example, while `Locale.region` controls the region-specific default values, such as measuring system and first day of the week.
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var region: Region? {
_locale.region
}
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var timeZone: TimeZone? {
_locale.timeZone
}
/// Returns the regional subdivision for the locale, or nil if there is none.
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var subdivision: Subdivision? {
_locale.subdivision
}
/// Returns the variant for the locale, or nil if it has none. For example, for the locale "en_POSIX", returns "POSIX".
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public var variant: Variant? {
_locale.variant
}
internal var weekendRange: WeekendRange? {
_locale.weekendRange
}
internal var minimumDaysInFirstWeek: Int {
_locale.minimumDaysInFirstWeek
}
// MARK: - Preferences support (Internal)
package var forceHourCycle: HourCycle? {
_locale.forceHourCycle
}
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
package func forceFirstWeekday(_ calendar: Calendar.Identifier) -> Weekday? {
_locale.forceFirstWeekday(calendar)
}
package func forceMinDaysInFirstWeek(_ calendar: Calendar.Identifier) -> Int? {
_locale.forceMinDaysInFirstWeek(calendar)
}
#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS
// This is framework-only because Date.FormatStyle.DateStyle is Internationalization-only.
package func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? {
_locale.customDateFormat(style)
}
#endif
@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
package var forceMeasurementSystem: Locale.MeasurementSystem? {
_locale.forceMeasurementSystem
}
package var forceTemperatureUnit: LocalePreferences.TemperatureUnit? {
_locale.forceTemperatureUnit
}
package var temperatureUnit: LocalePreferences.TemperatureUnit {
_locale.temperatureUnit
}
/// The whole bucket of preferences.
/// For use by `Calendar`, which wants to keep these values without a circular retain cycle with `Locale`. Only `current` locales and current-alikes have prefs.
package var prefs: LocalePreferences? {
_locale.prefs
}
#if FOUNDATION_FRAMEWORK
internal func pref(for key: String) -> Any? {
_locale.pref(for: key)
}
#endif
// Returns an identifier that includes preferences as keywords, such as "en_US@measure=metric" or "en_GB@hours=h12"
package var identifierCapturingPreferences: String {
_locale.identifierCapturingPreferences
}
// MARK: -
//
/// Returns a list of the user's preferred languages.
///
/// - note: `Bundle` is responsible for determining the language that your application will run in, based on the result of this API and combined with the languages your application supports.
/// - seealso: `Bundle.preferredLocalizations(from:)`
/// - seealso: `Bundle.preferredLocalizations(from:forPreferences:)`
public static var preferredLanguages: [String] {
LocaleCache.cache.preferredLanguages(forCurrentUser: false)
}
private static let languageCodeKey = "kCFLocaleLanguageCodeKey"
private static let scriptCodeKey = "kCFLocaleScriptCodeKey"
private static let countryCodeKey = "kCFLocaleCountryCodeKey"
private static let variantCodeKey = "kCFLocaleVariantCodeKey"
private static let calendarKey = "kCFLocaleCalendarKey"
/// Constructs an identifier from a dictionary of components.
public static func identifier(fromComponents components: [String : String]) -> String {
// Holds remaining keywords after we remove the CF-specific ones
var keywords = components
var result = ""
if let language = components[Self.languageCodeKey] {
result += language
keywords.removeValue(forKey: Self.languageCodeKey)
}
if let script = components[Self.scriptCodeKey] {
result += "_" + script
keywords.removeValue(forKey: Self.scriptCodeKey)
}
let country = components[Self.countryCodeKey]
let variant = components[Self.variantCodeKey]
if country != nil || variant != nil {
result += "_"
}
if let country {
result += country
keywords.removeValue(forKey: Self.countryCodeKey)
}
if let variant {
result += "_" + variant
keywords.removeValue(forKey: Self.variantCodeKey)
}
let corrected = Dictionary<String, String>(uniqueKeysWithValues: keywords.compactMap { key, value -> (String, String)? in
// Keys must be non-empty
guard !key.isEmpty else { return nil }
// Identifier keywords must be ASCII a-z, A-Z, 0-9
// They are normalized to all-lowercase
var correctedKey : [CChar] = []
for char in key.utf8 {
if (0x41...0x5a).contains(char) {
// A-Z
// Convert to lowercase by adding 0x20
correctedKey.append(CChar(char + 0x20))
} else if (0x61...0x7a).contains(char) {
// a-z
correctedKey.append(CChar(char))
} else if (0x30...0x39).contains(char) {
// 0-9
correctedKey.append(CChar(char))
} else {
// Skip this key
return nil
}
}
// null-terminate
correctedKey.append(CChar(0))
let correctedKeyString = String(cString: correctedKey)
// Values must be non-empty
guard !value.isEmpty else { return nil }
// Values must be ASCII a-z, A-Z, 0-9, _, -, +, /
let validValue = value.utf8.allSatisfy { char in
/* A-Z */ (0x41...0x5a).contains(char) ||
/* a-z */ (0x61...0x7a).contains(char) ||
/* 0-9 */ (0x30...0x39).contains(char) ||
/* _ */ char == 0x5f ||
/* - */ char == 0x2d ||
/* + */ char == 0x2b ||
/* / */ char == 0x2f
}
guard validValue else { return nil }
return (correctedKeyString, value)
})
guard !corrected.isEmpty else {
// Stop here
return result
}
result += "@"
let sortedKeys = corrected.keys.sorted(by: <)
for key in sortedKeys {
result += key + "=" + corrected[key]! + ";"
}
// Remove last ;
let _ = result.popLast()
return result
}
#if FOUNDATION_FRAMEWORK
/// Constructs an identifier from a dictionary of components, allowing a `Calendar` value. Compatibility only.
internal static func identifier(fromAnyComponents components: [String : Any]) -> String {
// n.b. the CFLocaleCreateLocaleIdentifierFromComponents API is normally [String: String], but for 'convenience' allows a `Calendar` value for "kCFLocaleCalendarKey"/"calendar". This version for framework use allows Calendar.
// Handle a special case of having both "kCFLocaleCalendarKey" and "calendar"
var uniqued = components
if let calendar = uniqued[Self.calendarKey] as? Calendar {
// Overwrite any value for "calendar" - the CF key takes precedence
uniqued["calendar"] = calendar.identifier.cfCalendarIdentifier
}
// Always remove this key
uniqued.removeValue(forKey: Self.calendarKey)
// Map the remaining values to strings, or remove them
let converted = Dictionary<String, String>(uniqueKeysWithValues: uniqued.compactMap { key, value in
if let value = value as? String {
return (key, value)
} else {
// Remove this, bad value type
return nil
}
})
return identifier(fromComponents: converted)
}
#endif // FOUNDATION_FRAMEWORK
/// Returns a canonical identifier from the given string.
@available(macOS, deprecated: 13, renamed: "identifier(_:from:)")
@available(iOS, deprecated: 16, renamed: "identifier(_:from:)")
@available(tvOS, deprecated: 16, renamed: "identifier(_:from:)")
@available(watchOS, deprecated: 9, renamed: "identifier(_:from:)")
public static func canonicalIdentifier(from string: String) -> String {
#if FOUNDATION_FRAMEWORK
return _canonicalLocaleIdentifier(from: string)
#else
// TODO: (Locale.canonicalIdentifier) implement in Swift: https://github.com/apple/swift-foundation/issues/45
return string
#endif // FOUNDATION_FRAMEWORK
}
/// Same as `canonicalIdentifier` but not deprecated, for internal usage. Also, it handles a nil result (e.g. non-ASCII identifier input) correctly.
package static func _canonicalLocaleIdentifier(from string: String) -> String {
#if FOUNDATION_FRAMEWORK
if let id = CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorSystemDefault, string as CFString) {
return id.rawValue as String
} else {
return ""
}
#else
// TODO: (Locale.canonicalIdentifier) implement in Swift: https://github.com/apple/swift-foundation/issues/45
return string
#endif // FOUNDATION_FRAMEWORK
}
/// Returns a canonical language identifier from the given string.
public static func canonicalLanguageIdentifier(from string: String) -> String {
#if FOUNDATION_FRAMEWORK
if let id = CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorSystemDefault, string as CFString) {
return id.rawValue as String
} else {
return ""
}
#else
// TODO: (Locale.canonicalLanguageIdentifier) Implement in Swift: https://github.com/apple/swift-foundation/issues/45
return string
#endif // FOUNDATION_FRAMEWORK
}
// MARK: -
/// Returns `true` if the locale is one of the "special" languages that requires special handling during case mapping.
/// - "az": Azerbaijani
/// - "lt": Lithuanian
/// - "tr": Turkish
/// - "nl": Dutch
/// - "el": Greek
/// For all other locales such as en_US, this is `false`.
internal var doesNotRequireSpecialCaseHandling: Bool {
// We call into the _LocaleBase because this value is cached in many cases. They will defer the initial calculation to the below helper function.
_locale.doesNotRequireSpecialCaseHandling
}
package static func identifierDoesNotRequireSpecialCaseHandling(_ identifier: String) -> Bool {
guard identifier.count >= 2 else { return true }
let first = identifier.prefix(2)
switch first {
case "az", "lt", "tr", "nl", "el":
return false // Does require special handling
default:
return true // Does not require special handling
}
}
// MARK: -
//
public func hash(into hasher: inout Hasher) {
if _locale.isAutoupdating {
hasher.combine(true)
} else {
hasher.combine(false)
hasher.combine(_locale.identifier)
hasher.combine(prefs)
}
}
public static func ==(lhs: Locale, rhs: Locale) -> Bool {
if lhs._locale.isAutoupdating || rhs._locale.isAutoupdating {
return lhs._locale.isAutoupdating && rhs._locale.isAutoupdating
} else {
return lhs.identifier == rhs.identifier && lhs.prefs == rhs.prefs
}
}
}
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension Locale : CustomDebugStringConvertible, CustomStringConvertible, CustomReflectable {
public var customMirror : Mirror {
var c: [(label: String?, value: Any)] = []
c.append((label: "identifier", value: identifier))
c.append((label: "locale", value: _locale.debugDescription))
return Mirror(self, children: c, displayStyle: Mirror.DisplayStyle.struct)
}
public var description: String {
return "\(identifier) (\(_locale.debugDescription))"
}
public var debugDescription : String {
description
}
}
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
extension Locale : Codable {
private enum CodingKeys : Int, CodingKey {
case identifier
case current
}
// CFLocale enforces a rule that fixed/current/autoupdatingCurrent can never be equal even if their values seem like they are the same
private enum Current : Int, Codable {
case fixed
case current
case autoupdatingCurrent
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let current = try container.decodeIfPresent(Current.self, forKey: .current) {
switch current {
case .autoupdatingCurrent:
self = Locale.autoupdatingCurrent
return
case .current:
self = Locale.current
return
case .fixed:
// Fall through to identifier-based
break
}
}
let identifier = try container.decode(String.self, forKey: .identifier)
self.init(identifier: identifier)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
// Even if we are current/autoupdatingCurrent, encode the identifier for backward compatibility
try container.encode(self.identifier, forKey: .identifier)
if self == Locale.autoupdatingCurrent {
try container.encode(Current.autoupdatingCurrent, forKey: .current)
} else if self == Locale.current {
try container.encode(Current.current, forKey: .current)
} else {
try container.encode(Current.fixed, forKey: .current)
}
}
}
|