From 92684acdffb76941268b6627f894274754771e41 Mon Sep 17 00:00:00 2001 From: Richard Topchii Date: Wed, 31 Jan 2024 22:28:45 +0200 Subject: [PATCH] Simplify localization code, remove extension --- Sources/Localization.swift | 52 ++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/Sources/Localization.swift b/Sources/Localization.swift index 2f13971a..fea6d19a 100644 --- a/Sources/Localization.swift +++ b/Sources/Localization.swift @@ -1,36 +1,34 @@ import Foundation -extension Bundle { - static var localizationBundle: Bundle { - // When installed via the Swift Package Manager, the bundle name is "CalendarKit_CalendarKit", - // via CocoaPods - "CalendarKit" - let bundleNames = ["CalendarKit_CalendarKit", "CalendarKit"] - - let candidates = [ - // Bundle should be present here when the package is linked into an App. - Bundle.main.resourceURL, - - // Bundle should be present here when the package is linked into a framework. - Bundle(for: DayViewController.self).resourceURL, - - // For command-line tools. - Bundle.main.bundleURL, - ] - - for candidate in candidates { - for bundleName in bundleNames { - let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle") - if let bundle = bundlePath.flatMap(Bundle.init(url:)) { - return bundle - } +func localizedString(_ key: String) -> String { + var localizationBundle = Bundle.main + // When installed via the Swift Package Manager, the bundle name is "CalendarKit_CalendarKit", + // via CocoaPods - "CalendarKit" + let bundleNames = ["CalendarKit_CalendarKit", "CalendarKit"] + + let candidates = [ + // Bundle should be present here when the package is linked into an App. + Bundle.main.resourceURL, + + // Bundle should be present here when the package is linked into a framework. + Bundle(for: DayViewController.self).resourceURL, + + // For command-line tools. + Bundle.main.bundleURL, + ] + +outer: + for candidate in candidates { + for bundleName in bundleNames { + let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle") + if let bundle = bundlePath.flatMap(Bundle.init(url:)) { + localizationBundle = bundle + break outer } } - return Bundle.main } -} -func localizedString(_ key: String) -> String { - Bundle.localizationBundle.localizedString(forKey: key, + return localizationBundle.localizedString(forKey: key, value: nil, table: nil) }