Found this one , it will be useful for you to use NSLocalizedString in the side classes. tested it on fast 4 with the following settings
extension NSObject { func NSLocalizedString(_ key: String, comment: String) -> String { return "My custom localization" } static func NSLocalizedString(_ key: String, comment: String) -> String { return "My custom localization" } } class ViewController: UIViewController { @IBOutlet weak var myLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() myLabel.text = NSLocalizedString("mystring",comment:"") } }
should work the same for swift 2.3 (without underscore before the key parameter)
Update
This requires a workaround when working with closure, because swift will expect you to use NSLocalizedString with self so self.NSLocalizedString("mystring",comment:"") . The workaround in this scenario is to assign the let / var outside the closure.
Example:
// Doesn't work self.present(anotherVc, animated: true) { [weak self] in self?.myLabel.text = NSLocalizedString("mystring", comment: "") // compiler will throw an error } // Does work let string = NSLocalizedString("mystring", comment: "") self.present(anotherVc, animated: true) { [weak self] in self?.myLabel.text = string }
For property initializers, the compiler will use a static function, so it is important to set them as
Example:
class MyClass: NSObject { // This let let myStr = NSLocalizedString("mystring",comment:"") } extension NSObject { func NSLocalizedString(_ key: String, comment: String) -> String { return "My custom localization" } // Uses the static method static func NSLocalizedString(_ key: String, comment: String) -> String { return "My custom localization" } }
source share