Home > Back-end >  Change a parameter from a single class to all other classes
Change a parameter from a single class to all other classes

Time:04-09

I am having a problem that I cant seem to figure it out, I am trying to change the language of the application through sender buttons but I cant change the parameter of the language from the signInViewController to other VC's I am getting an error.

In other classes I created a static let shared = resetPasswordViewController and then call it at the signInViewController to change the labels buttons etc but I am getting a nil error at labels and buttons when its trying to change the language

signInViewController:

class signInViewController: UIViewController {

@IBOutlet weak var welcomeLabelSignIn: UILabel!


    override func viewDidLoad() {
    super.viewDidLoad()
   
    }
}


@IBAction func changeLanguageToAlbanian(_ sender: Any) {
    localizeSignIn()
    resetPasswordViewController.shared.localizeResetPassword()
}

@IBAction func changeLanguageToSerbian(_ sender: Any) {
    localizeSignIn1()
    resetPasswordViewController.shared.localizeResetPassword()
}

@IBAction func changeLanguageToEnglish(_ sender: Any) {
    localizeSignIn2()
    resetPasswordViewController.shared.localizeResetPassword()
}

func localizeSignIn() {

     welcomeLabelSignIn.text = NSLocalizedString("Welcome!", tableName: nil, bundle: changeLanguage.createBundlePath(lang: "sq" ), value: "", comment: "")
}

resetPasswordViewController:

class resetPasswordViewController: UIViewController {

static let shared = resetPasswordViewController()

@IBOutlet weak var enterEmailLabel: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
    
    localizeResetPassword()
}
 
func localizeResetPassword() {
    
     enterEmailLabel.text = NSLocalizedString("Welcome!", tableName: nil, bundle: changeLanguage.createBundlePath(lang: "sq" ), value: "", comment: "")
}

Now you see that the (lang: "") is being called as static, I need to make a global one that when the client press the button to change the language it will replace that "" in every other class and then change the language.

I hope I was clear about the problem, for any questions feel free to comment please.

CodePudding user response:

If you want to make a singleton LangHandler you should seperate it into another class so you can make reusable code without typing again

class LangHanlder

class LangHandler {
    static let shared = LangHandler()
        
    var lang: String
    //Make default lang
    private init(){
        lang = "sq"
    }
        
    func changeLang(_ langChange: String){
        lang = langChange
    }
}

Have localized extension

extension String {
    func localized(_ lang:String) ->String {

        let path = Bundle.main.path(forResource: lang, ofType: "lproj")
        let bundle = Bundle(path: path!)

        return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
    }
}

How to use When you called for the first time you always have default value signInViewController:

class signInViewController: UIViewController {

@IBOutlet weak var welcomeLabelSignIn: UILabel!


    override func viewDidLoad() {
    super.viewDidLoad()
   
    }
}


@IBAction func changeLanguageToAlbanian(_ sender: Any) {
    localizeSignIn()
}

@IBAction func changeLanguageToSerbian(_ sender: Any) {
    // change localized handler language only
    LangHandler.shared.changeLang("sr")
    localizeSignIn()
}

@IBAction func changeLanguageToEnglish(_ sender: Any) {
    // change localized handler language only
    LangHandler.shared.changeLang("en")
    localizeSignIn()
}

func localizeSignIn() {
     welcomeLabelSignIn.text = "Welcome!".localized(LangHandler.shared.lang)
}

resetPasswordViewController:

class resetPasswordViewController: UIViewController {

@IBOutlet weak var enterEmailLabel: UILabel!


override func viewDidLoad() {
    super.viewDidLoad()
    
    localizeResetPassword()
}
 
func localizeResetPassword() {
     // default is sq
     enterEmailLabel.text = "Welcome!".localized(LangHandler.shared.lang)
}
  • Related