Skip to content

Commit d0590b4

Browse files
fix: allow override of strings whilst falling back to module bundle
1 parent 285bf5b commit d0590b4

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Utils/StringUtils.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,43 @@ let kKeyNotFound = "Key not found"
1919

2020
public class StringUtils {
2121
let bundle: Bundle
22+
let fallbackBundle: Bundle
2223
let languageCode: String?
2324

2425
init(bundle: Bundle, languageCode: String? = nil) {
2526
self.bundle = bundle
27+
self.fallbackBundle = Bundle.module // Always fall back to the package's default strings
2628
self.languageCode = languageCode
2729
}
2830

2931
public func localizedString(for key: String) -> String {
3032
// If a specific language code is set, load strings from that language bundle
3133
if let languageCode, let path = bundle.path(forResource: languageCode, ofType: "lproj"),
3234
let localizedBundle = Bundle(path: path) {
33-
return localizedBundle.localizedString(forKey: key, value: nil, table: "Localizable")
35+
let localizedString = localizedBundle.localizedString(forKey: key, value: nil, table: "Localizable")
36+
// If string was found in custom bundle, return it
37+
if localizedString != key {
38+
return localizedString
39+
}
40+
41+
// Fall back to fallback bundle with same language
42+
if let fallbackPath = fallbackBundle.path(forResource: languageCode, ofType: "lproj"),
43+
let fallbackLocalizedBundle = Bundle(path: fallbackPath) {
44+
return fallbackLocalizedBundle.localizedString(forKey: key, value: nil, table: "Localizable")
45+
}
3446
}
3547

36-
// Use default localization
48+
// Try default localization from custom bundle
3749
let keyLocale = String.LocalizationValue(key)
38-
return String(localized: keyLocale, bundle: bundle)
50+
let localizedString = String(localized: keyLocale, bundle: bundle)
51+
52+
// If the string was found in custom bundle (not just the key returned), use it
53+
if localizedString != key {
54+
return localizedString
55+
}
56+
57+
// Fall back to the package's default strings
58+
return String(localized: keyLocale, bundle: fallbackBundle)
3959
}
4060

4161
public func localizedErrorMessage(for error: Error) -> String {

0 commit comments

Comments
 (0)