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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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
//
//===----------------------------------------------------------------------===//
#if FOUNDATION_FRAMEWORK
// for CFXPreferences call
internal import _ForSwiftFoundation
#endif
/// Holds user preferences about `Locale`, retrieved from user defaults. It is only used when creating the `current` Locale. Fixed-identifier locales never have preferences.
package struct LocalePreferences: Hashable, Sendable {
package enum MeasurementUnit {
case centimeters
case inches
/// Init with the value of a user defaults string
init?(_ string: String?) {
guard let string else { return nil }
if string == "Centimeters" { self = .centimeters }
else if string == "Inches" { self = .inches }
else { return nil }
}
/// Get the value as a user defaults string
var userDefaultString: String {
switch self {
case .centimeters: return "Centimeters"
case .inches: return "Inches"
}
}
}
package enum TemperatureUnit {
case fahrenheit
case celsius
/// Init with the value of a user defaults string
init?(_ string: String?) {
guard let string else { return nil }
if string == "Celsius" { self = .celsius }
else if string == "Fahrenheit" { self = .fahrenheit }
else { return nil }
}
/// Get the value as a user defaults string
var userDefaultString: String {
switch self {
case .celsius: return "Celsius"
case .fahrenheit: return "Fahrenheit"
}
}
}
package var metricUnits: Bool?
package var languages: [String]?
package var locale: String?
package var collationOrder: String?
package var firstWeekday: [Calendar.Identifier : Int]?
package var minDaysInFirstWeek: [Calendar.Identifier : Int]?
#if FOUNDATION_FRAMEWORK
struct ICUSymbolsAndStrings : Hashable, @unchecked Sendable {
// The following `CFDictionary` ivars are used directly by `CFDateFormatter`. Keep them as `CFDictionary` to avoid bridging them into and out of Swift. We don't need to access them from Swift at all.
package var icuDateTimeSymbols: CFDictionary?
package var icuDateFormatStrings: CFDictionary?
package var icuTimeFormatStrings: CFDictionary?
// The OS no longer writes out this preference, but we keep it here for compatibility with CFDateFormatter behavior.
package var icuNumberFormatStrings: CFDictionary?
package var icuNumberSymbols: CFDictionary?
}
var icuSymbolsAndStrings = ICUSymbolsAndStrings()
#if !NO_FORMATTERS
package var dateFormats: [Date.FormatStyle.DateStyle: String]? // Bridged version of `icuDateFormatStrings`
#endif
#endif
package var numberSymbols: [UInt32 : String]? // Bridged version of `icuNumberSymbols`
package var country: String?
package var measurementUnits: MeasurementUnit?
package var temperatureUnit: TemperatureUnit?
package var force24Hour: Bool?
package var force12Hour: Bool?
package init() { }
#if FOUNDATION_FRAMEWORK && canImport(_FoundationICU)
// The framework init supports customized dateFormats
package init(metricUnits: Bool? = nil,
languages: [String]? = nil,
locale: String? = nil,
collationOrder: String? = nil,
firstWeekday: [Calendar.Identifier : Int]? = nil,
minDaysInFirstWeek: [Calendar.Identifier : Int]? = nil,
country: String? = nil,
measurementUnits: MeasurementUnit? = nil,
temperatureUnit: TemperatureUnit? = nil,
force24Hour: Bool? = nil,
force12Hour: Bool? = nil,
numberSymbols: [UInt32 : String]? = nil,
dateFormats: [Date.FormatStyle.DateStyle: String]? = nil) {
self.metricUnits = metricUnits
self.languages = languages
self.locale = locale
self.collationOrder = collationOrder
self.firstWeekday = firstWeekday
self.minDaysInFirstWeek = minDaysInFirstWeek
self.country = country
self.measurementUnits = measurementUnits
self.temperatureUnit = temperatureUnit
self.force24Hour = force24Hour
self.force12Hour = force12Hour
self.numberSymbols = numberSymbols
self.dateFormats = dateFormats
}
#else
package init(metricUnits: Bool? = nil,
languages: [String]? = nil,
locale: String? = nil,
collationOrder: String? = nil,
firstWeekday: [Calendar.Identifier : Int]? = nil,
minDaysInFirstWeek: [Calendar.Identifier : Int]? = nil,
country: String? = nil,
measurementUnits: MeasurementUnit? = nil,
temperatureUnit: TemperatureUnit? = nil,
force24Hour: Bool? = nil,
force12Hour: Bool? = nil,
numberSymbols: [UInt32 : String]? = nil) {
self.metricUnits = metricUnits
self.languages = languages
self.locale = locale
self.collationOrder = collationOrder
self.firstWeekday = firstWeekday
self.minDaysInFirstWeek = minDaysInFirstWeek
self.country = country
self.measurementUnits = measurementUnits
self.temperatureUnit = temperatureUnit
self.force24Hour = force24Hour
self.force12Hour = force12Hour
self.numberSymbols = numberSymbols
}
#endif
#if FOUNDATION_FRAMEWORK && !NO_CFPREFERENCES
/// Interpret a dictionary (from user defaults) according to a predefined set of strings and convert it into the more strongly-typed `LocalePreferences` values.
/// Several dictionaries may need to be applied to the same instance, which is why this is structured as a mutating setter rather than an initializer.
/// Why use a `CFDictionary` instead of a Swift dictionary here? The input prefs may be a complete copy of the user's prefs, and we don't want to bridge a ton of unrelated data into Swift just to extract a few keys. Keeping it as a `CFDictionary` avoids that overhead, and we call into small CF helper functions to get the data we need, if it is there.
package mutating func apply(_ prefs: CFDictionary) {
var exists: DarwinBoolean = false
guard CFDictionaryGetCount(prefs) > 0 else { return }
if let langs = __CFLocalePrefsCopyAppleLanguages(prefs)?.takeRetainedValue() as? [String] {
self.languages = langs
}
if let locale = __CFLocalePrefsCopyAppleLocale(prefs)?.takeRetainedValue() as? String {
self.locale = locale
}
let isMetric = __CFLocalePrefsAppleMetricUnitsIsMetric(prefs, &exists)
if exists.boolValue {
self.metricUnits = isMetric
}
let isCentimeters = __CFLocalePrefsAppleMeasurementUnitsIsCm(prefs, &exists)
if exists.boolValue {
self.measurementUnits = isCentimeters ? .centimeters : .inches
}
let isCelsius = __CFLocalePrefsAppleTemperatureUnitIsC(prefs, &exists)
if exists.boolValue {
self.temperatureUnit = isCelsius ? .celsius : .fahrenheit
}
let is24Hour = __CFLocalePrefsAppleForce24HourTime(prefs, &exists)
if exists.boolValue {
self.force24Hour = is24Hour
}
let is12Hour = __CFLocalePrefsAppleForce12HourTime(prefs, &exists)
if exists.boolValue {
self.force12Hour = is12Hour
}
if let collationOrder = __CFLocalePrefsCopyAppleCollationOrder(prefs)?.takeRetainedValue() as? String {
self.collationOrder = collationOrder
}
if let country = __CFLocalePrefsCopyCountry(prefs)?.takeRetainedValue() as? String {
self.country = country
}
if let icuDateTimeSymbols = __CFLocalePrefsCopyAppleICUDateTimeSymbols(prefs)?.takeRetainedValue() {
self.icuSymbolsAndStrings.icuDateTimeSymbols = icuDateTimeSymbols
}
if let icuDateFormatStrings = __CFLocalePrefsCopyAppleICUDateFormatStrings(prefs)?.takeRetainedValue() {
self.icuSymbolsAndStrings.icuDateFormatStrings = icuDateFormatStrings
// Bridge the mapping for Locale's usage
if let dateFormatPrefs = icuDateFormatStrings as? [String: String] {
var mapped: [Date.FormatStyle.DateStyle : String] = [:]
for (key, value) in dateFormatPrefs {
if let k = UInt(key) {
mapped[Date.FormatStyle.DateStyle(rawValue: k)] = value
}
}
self.dateFormats = mapped
}
}
if let icuTimeFormatStrings = __CFLocalePrefsCopyAppleICUTimeFormatStrings(prefs)?.takeRetainedValue() {
self.icuSymbolsAndStrings.icuTimeFormatStrings = icuTimeFormatStrings
}
if let icuNumberFormatStrings = __CFLocalePrefsCopyAppleICUNumberFormatStrings(prefs)?.takeRetainedValue() {
self.icuSymbolsAndStrings.icuNumberFormatStrings = icuNumberFormatStrings
}
if let icuNumberSymbols = __CFLocalePrefsCopyAppleICUNumberSymbols(prefs)?.takeRetainedValue() {
// Store the CFDictionary for passing back to CFDateFormatter
self.icuSymbolsAndStrings.icuNumberSymbols = icuNumberSymbols
// And bridge the mapping for our own usage in Locale
if let numberSymbolsPrefs = icuNumberSymbols as? [String: String] {
var mapped: [UInt32 : String] = [:]
for (key, value) in numberSymbolsPrefs {
if let symbol = UInt32(key) {
mapped[symbol] = value
}
}
if !mapped.isEmpty {
self.numberSymbols = mapped
}
}
}
if let firstWeekdaysPrefs = __CFLocalePrefsCopyAppleFirstWeekday(prefs)?.takeRetainedValue() as? [String: Int] {
var mapped: [Calendar.Identifier : Int] = [:]
for (key, value) in firstWeekdaysPrefs {
if let id = Calendar.Identifier(identifierString: key) {
mapped[id] = value
}
}
if !mapped.isEmpty {
self.firstWeekday = mapped
}
}
if let minDaysPrefs = __CFLocalePrefsCopyAppleMinDaysInFirstWeek(prefs)?.takeRetainedValue() as? [String: Int] {
var mapped: [Calendar.Identifier : Int] = [:]
for (key, value) in minDaysPrefs {
if let id = Calendar.Identifier(identifierString: key) {
mapped[id] = value
}
}
if !mapped.isEmpty {
self.minDaysInFirstWeek = mapped
}
}
}
#endif // FOUNDATION_FRAMEWORK
/// For testing purposes, merge a set of override prefs into this one.
package mutating func apply(_ prefs: LocalePreferences) {
if let other = prefs.metricUnits { self.metricUnits = other }
if let other = prefs.languages { self.languages = other }
if let other = prefs.locale { self.locale = other }
if let other = prefs.collationOrder { self.collationOrder = other }
if let other = prefs.firstWeekday { self.firstWeekday = other }
if let other = prefs.minDaysInFirstWeek { self.minDaysInFirstWeek = other }
if let other = prefs.numberSymbols { self.numberSymbols = other }
#if FOUNDATION_FRAMEWORK
if let other = prefs.icuSymbolsAndStrings.icuDateTimeSymbols { self.icuSymbolsAndStrings.icuDateTimeSymbols = other }
if let other = prefs.icuSymbolsAndStrings.icuDateFormatStrings { self.icuSymbolsAndStrings.icuDateFormatStrings = other }
if let other = prefs.icuSymbolsAndStrings.icuTimeFormatStrings { self.icuSymbolsAndStrings.icuTimeFormatStrings = other }
if let other = prefs.icuSymbolsAndStrings.icuNumberFormatStrings { self.icuSymbolsAndStrings.icuNumberFormatStrings = other }
if let other = prefs.icuSymbolsAndStrings.icuNumberSymbols { self.icuSymbolsAndStrings.icuNumberSymbols = other }
#if !NO_FORMATTERS
if let other = prefs.dateFormats { self.dateFormats = other }
#endif // !NO_FORMATTERS
#endif // FOUNDATION_FRAMEWORK
if let other = prefs.country { self.country = other }
if let other = prefs.measurementUnits { self.measurementUnits = other }
if let other = prefs.temperatureUnit { self.temperatureUnit = other }
if let other = prefs.force24Hour { self.force24Hour = other }
if let other = prefs.force12Hour { self.force12Hour = other }
}
package var measurementSystem: Locale.MeasurementSystem? {
let metricPref = metricUnits
let measurementPref = measurementUnits
if metricPref == nil && measurementPref == nil {
return nil
} else if let metricPref, metricPref == true, let measurementPref, measurementPref == .inches {
return Locale.MeasurementSystem.uk
} else if let metricPref, metricPref == false {
return Locale.MeasurementSystem.us
} else if let measurementPref, measurementPref == .centimeters {
return Locale.MeasurementSystem.metric
} else {
// There isn't enough info
return nil
}
}
package var hourCycle: Locale.HourCycle? {
if let setForce24Hour = force24Hour, setForce24Hour {
// Respect 24-hour override if both force24hour and force12hour are true
return .zeroToTwentyThree
} else if let setForce12Hour = force12Hour, setForce12Hour {
return .oneToTwelve
} else {
return nil
}
}
}
|