I have tried everything available on internet but still I am missing something key values are also same in both .strings files, added Spanish language, Localized my LocalizeAble file (I don't want to adit schema for simulator) Any response will be appreciated.Thanks
My ViewController
class ViewController: UIViewController
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
override func viewDidLoad() {super.viewDidLoad() }
@IBAction func selectLanguageBtn(_ sender: UIButton) {
if sender.tag == 1 {
//sended button tag 1 for selecting English
firstNameLabel.text = "FirstLabel".localizeableString(loc: "en")
} else if sender.tag == 2 {
//sended button tag 2 for selecting Spanish
firstNameLabel.text = "FirstLabel".localizeableString(loc: "es")
}}}
extension String {
func localizeableString(loc:String) -> String {
let path = Bundle.main.path(forResource: loc, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
} [enter image description here][1]
[1]: https://i.sstatic.net/mrS0s.png
I found this one, It is similar to Brandon answer but more handier.
You have to follow same steps and just write .localized()
when assigning the string, It will get the string as key
Happy Codding :)
let LANGUAGE = "es" // Choose your language
extension String {
func localized() -> String{
let path = Bundle.main.path(forResource: LANGUAGE, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
}