Skip to content

Commit

Permalink
Simplify localization code, remove extension
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtop committed Jan 31, 2024
1 parent 1d8cfbf commit 92684ac
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions Sources/Localization.swift
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 92684ac

Please sign in to comment.