Muaaz Ahmed
Muaaz Ahmed

Reputation: 413

How to change language of iOS app run time( let say with a button) without changing device language

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

Upvotes: 2

Views: 2211

Answers (2)

Munib Hamza
Munib Hamza

Reputation: 98

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: "")
    }
}

Upvotes: 1

Brandon
Brandon

Reputation: 23475

Why bother using NSLocalizedString when you can retrieve the string directly from the bundle?

Also make sure the file is named Localizable.strings in both the en.lproj and es.lproj and whatever other languages you're supporting.

If the file is named Test.strings, then you need to specify the table: "Test" parameter for localizedString.

extension String {
    public static func localized(_ key: String, language: String? = nil) -> String {
        let language = language ?? Locale.preferredLanguages.first?.components(separatedBy: "-").first ?? "en"
        guard let path = Bundle.main.path(forResource: language, ofType: "lproj"), let bundle = Bundle(path: path) else {
            return Bundle.main.localizedString(forKey: key, value: nil, table: nil)
        }
        return bundle.localizedString(forKey: key, value: nil, table: nil)
    }
}

Usage:

firstNameLabel.text = .localized("FirstName") //default
firstNameLabel.text = .localized("FirstName", language: "es") //specified

Upvotes: 2

Related Questions