import Foundation

func localizedString(_ key: String, language: AppLanguage) -> String {
    let resolvedLanguage = language.resolved()

    if let path = Bundle.main.path(forResource: resolvedLanguage.localeIdentifier, ofType: "lproj"),
       let bundle = Bundle(path: path) {
        let localized = bundle.localizedString(forKey: key, value: nil, table: nil)
        if localized != key {
            return localized
        }
    }

    if let path = Bundle.main.path(forResource: "en", ofType: "lproj"),
       let bundle = Bundle(path: path) {
        let english = bundle.localizedString(forKey: key, value: nil, table: nil)
        if english != key {
            return english
        }
    }

    return NSLocalizedString(key, comment: "")
}
