I have the following problem with a small iOS 7 project on which I\'m testing the localisation capabilities.
Using Dirk's answer here is the Swift equivalent implemented as a computed property in a String extension:
extension String {
var localized: String {
var localizedString = NSLocalizedString(self, comment: "")
// If a localized string was found then return it.
// This check is based on the fact that if no localized string
// exists then NSLocalized() returns the key itself.
if self != localizedString {
return localizedString
}
// No localized string exists. Retrieve the display string
// from the base strings file.
var bundleForString: Bundle
if let path = Bundle.main.path(forResource: "Base", ofType: "lproj"),
let bundle = Bundle(path: path) {
bundleForString = bundle
} else {
bundleForString = Bundle.main
}
localizedString = bundleForString.localizedString(forKey: self, value: self, table: nil)
return localizedString
}
}