Skip to content

Commit 3caa32c

Browse files
authored
Add Locale encoding option to prevent .current sentinel (#1500)
1 parent e682286 commit 3caa32c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Sources/FoundationEssentials/Locale/Locale.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,19 @@ extension Locale : Codable {
860860
self.init(identifier: identifier)
861861
}
862862
}
863-
864-
public func encode(to encoder: Encoder) throws {
863+
864+
// currentIsSentinel specifies whether .current should be encoded as a sentinel for compatibility with older runtimes
865+
// When true and encoding the current locale, decoding the archive on an older runtime will decode as the new "current"
866+
// When false and encoding the current locale, the locale is encoded as a fixed locale with preferences
867+
// When not encoding the current locale, this parameter has no effect
868+
internal func _encode(to encoder: Encoder, currentIsSentinel: Bool) throws {
865869
var container = encoder.container(keyedBy: CodingKeys.self)
866870
// Even if we are current/autoupdatingCurrent, encode the identifier for backward compatibility
867871
try container.encode(self.identifier, forKey: .identifier)
868-
872+
869873
if self == Locale.autoupdatingCurrent {
870874
try container.encode(Current.autoupdatingCurrent, forKey: .current)
871-
} else if self == Locale.current {
875+
} else if currentIsSentinel && self == Locale.current {
872876
// 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
873877
try container.encode(Current.current, forKey: .current)
874878
} else {
@@ -880,4 +884,8 @@ extension Locale : Codable {
880884
try container.encode(prefs, forKey: .preferences)
881885
}
882886
}
887+
888+
public func encode(to encoder: Encoder) throws {
889+
try _encode(to: encoder, currentIsSentinel: true)
890+
}
883891
}

0 commit comments

Comments
 (0)