What’s NSLocalizedString equivalent in Swift?

I use next solution: 1) create extension: extension String { var localized: String { return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: “”, comment: “”) } } 2) in Localizable.strings file: “Hi” = “Привет”; 3) example of use: myLabel.text = “Hi”.localized enjoy! 😉 –upd:– for case with comments you can use this solution: 1) Extension: extension … Read more

How to provide a localized description with an Error type in Swift?

As described in the Xcode 8 beta 6 release notes, Swift-defined error types can provide localized error descriptions by adopting the new LocalizedError protocol. In your case: public enum MyError: Error { case customError } extension MyError: LocalizedError { public var errorDescription: String? { switch self { case .customError: return NSLocalizedString(“A user-friendly description of the … Read more

How to force NSLocalizedString to use a specific language

NSLocalizedString() (and variants thereof) access the “AppleLanguages” key in NSUserDefaults to determine what the user’s settings for preferred languages are. This returns an array of language codes, with the first one being the one set by the user for their phone, and the subsequent ones used as fallbacks if a resource is not available in … Read more