Skip to content

Instantly share code, notes, and snippets.

@hdmdhr
Created September 6, 2023 00:55
Show Gist options
  • Save hdmdhr/445b4fb01a370c511b817858b92cdde7 to your computer and use it in GitHub Desktop.
Save hdmdhr/445b4fb01a370c511b817858b92cdde7 to your computer and use it in GitHub Desktop.
Find bundle for a given language code, aka a String
import Foundation
typealias LanguageCode = String
extension LanguageCode {
var bundle: Bundle {
let path = Bundle.main.path(forResource: self, ofType: "lproj") ??
Bundle.main.path(forResource: "Base", ofType: "lproj")
return path.map { Bundle(path: $0) ?? .main } ?? .main
}
}
extension Optional where Wrapped == LanguageCode {
var bundle: Bundle { (self ?? "Base").bundle }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment