Skip to content

Commit cef4ea4

Browse files
authored
Make sure to turn off bundle matching when resetting the autoupdating locale for testing purposes. (#347)
Follow up for #342
1 parent ea334c6 commit cef4ea4

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Sources/FoundationEssentials/Locale/Locale_Cache.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ struct LocaleCache : Sendable {
8585
}
8686
}
8787

88-
mutating func current(preferences: LocalePreferences?, cache: Bool) -> (any _LocaleProtocol)? {
88+
/// Get or create the current locale.
89+
/// `disableBundleMatching` should normally be disabled (`false`). The only reason to turn it on (`true`) is if we are attempting to create a testing scenario that does not use the main bundle's languages.
90+
mutating func current(preferences: LocalePreferences?, cache: Bool, disableBundleMatching: Bool) -> (any _LocaleProtocol)? {
8991
resetCurrentIfNeeded()
9092

9193
if let cachedCurrentLocale {
@@ -98,7 +100,7 @@ struct LocaleCache : Sendable {
98100
return nil
99101
}
100102

101-
let locale = LocaleCache.localeICUClass.init(name: nil, prefs: preferences, disableBundleMatching: false)
103+
let locale = LocaleCache.localeICUClass.init(name: nil, prefs: preferences, disableBundleMatching: disableBundleMatching)
102104
if cache {
103105
// It's possible this was an 'incomplete locale', in which case we will want to calculate it again later.
104106
self.cachedCurrentLocale = locale
@@ -255,13 +257,14 @@ struct LocaleCache : Sendable {
255257
func resetCurrent(to preferences: LocalePreferences) {
256258
lock.withLock {
257259
$0.reset()
258-
let _ = $0.current(preferences: preferences, cache: true)
260+
// Disable bundle matching so we can emulate a non-English main bundle during test
261+
let _ = $0.current(preferences: preferences, cache: true, disableBundleMatching: true)
259262
}
260263
}
261264

262265
var current: any _LocaleProtocol {
263266
var result = lock.withLock {
264-
$0.current(preferences: nil, cache: false)
267+
$0.current(preferences: nil, cache: false, disableBundleMatching: false)
265268
}
266269

267270
if let result { return result }
@@ -270,7 +273,7 @@ struct LocaleCache : Sendable {
270273
let (prefs, doCache) = preferences()
271274

272275
result = lock.withLock {
273-
$0.current(preferences: prefs, cache: doCache)
276+
$0.current(preferences: prefs, cache: doCache, disableBundleMatching: false)
274277
}
275278

276279
guard let result else {

0 commit comments

Comments
 (0)