Cache lproj bundles so that so we stop recreating them every time we request a localizable string

This commit is contained in:
Stefan Ceriu
2023-02-01 16:41:22 +02:00
committed by Stefan Ceriu
parent a9c24abcdf
commit 9cc876eb0c
+14 -2
View File
@@ -17,12 +17,24 @@
import Foundation
public extension Bundle {
private static var cachedLocalizationBundles = [String: Bundle]()
/// Get an lproj language bundle from the receiver bundle.
/// - Parameter language: The language to try to load.
/// - Returns: The lproj bundle if found otherwise nil.
func lprojBundle(for language: String) -> Bundle? {
guard let lprojURL = url(forResource: language, withExtension: "lproj") else { return nil }
return Bundle(url: lprojURL)
if let bundle = Self.cachedLocalizationBundles[language] {
return bundle
}
guard let lprojURL = url(forResource: language, withExtension: "lproj") else {
return nil
}
let bundle = Bundle(url: lprojURL)
Self.cachedLocalizationBundles[language] = bundle
return bundle
}
/// Preferred app language for translations. Takes the highest priority in translations. The priority list for translations: