Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Sources/FoundationEssentials/Locale/Locale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -860,15 +860,19 @@ extension Locale : Codable {
self.init(identifier: identifier)
}
}

public func encode(to encoder: Encoder) throws {

// currentIsSentinel specifies whether .current should be encoded as a sentinel for compatibility with older runtimes
// When true and encoding the current locale, decoding the archive on an older runtime will decode as the new "current"
// When false and encoding the current locale, the locale is encoded as a fixed locale with preferences
// When not encoding the current locale, this parameter has no effect
internal func _encode(to encoder: Encoder, currentIsSentinel: Bool) 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 {
} else if currentIsSentinel && self == Locale.current {
// Always encode .current for the current locale to preserve existing decoding behavior of .current when decoding on older runtimes prior to FoundationPreview 6.3 releases
try container.encode(Current.current, forKey: .current)
} else {
Expand All @@ -880,4 +884,8 @@ extension Locale : Codable {
try container.encode(prefs, forKey: .preferences)
}
}

public func encode(to encoder: Encoder) throws {
try _encode(to: encoder, currentIsSentinel: true)
}
}