From 9cc876eb0c257244abdb30a991a8ad2b97535ed7 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Wed, 1 Feb 2023 16:41:22 +0200 Subject: [PATCH] Cache lproj bundles so that so we stop recreating them every time we request a localizable string --- ElementX/Sources/Other/Extensions/Bundle.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ElementX/Sources/Other/Extensions/Bundle.swift b/ElementX/Sources/Other/Extensions/Bundle.swift index 281d05eec..2a242a8b4 100644 --- a/ElementX/Sources/Other/Extensions/Bundle.swift +++ b/ElementX/Sources/Other/Extensions/Bundle.swift @@ -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: