1

I work with multi language app, i want to change language manually in profile user.

I already have Localization.string

And if i change device language, language in app changin to.

But i want to change language manually from user profile example:

enter image description here

And for this i use next code:

private func changeToLanguage(_ langCode: String) {
    if Bundle.main.preferredLocalizations.first != langCode {
        let message = "In order to change the language, the App must be closed and reopened by you."
        let confirmAlertCtrl = UIAlertController(title: "App restart required", message: message, preferredStyle: .alert)

        let confirmAction = UIAlertAction(title: "Close now", style: .destructive) { _ in
            UserDefaults.standard.set([langCode], forKey: "AppleLanguages")
            UserDefaults.standard.synchronize()
            exit(EXIT_SUCCESS)
        }
        confirmAlertCtrl.addAction(confirmAction)

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        confirmAlertCtrl.addAction(cancelAction)

        present(confirmAlertCtrl, animated: true, completion: nil)
    }
    UserDefaults.standard.set([langCode], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()
}



  @IBAction func didPressChangeLanguageButton() {
    let message = "Change language of this app including its content."
    let sheetCtrl = UIAlertController(title: "Choose language", message: message, preferredStyle: .actionSheet)

    for languageCode in Bundle.main.localizations.filter({ $0 != "Base" }) {
        let langName = Locale.current.localizedString(forLanguageCode: languageCode)
        let action = UIAlertAction(title: langName, style: .default) { _ in
            self.changeToLanguage(languageCode) // see step #2
        }
        sheetCtrl.addAction(action)
    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    sheetCtrl.addAction(cancelAction)

    sheetCtrl.popoverPresentationController?.sourceView = self.view
    sheetCtrl.popoverPresentationController?.sourceRect = self.changeLanguageButton.frame
    present(sheetCtrl, animated: true, completion: nil)
}

And i use this bundle:

Bundle.main.localizations.filter({ $0 != "Base" })
Locale.current.localizedString(forLanguageCode: "en")

For this code i have:

enter image description here

And when i press the button i get error:

reason: 'Actions added to UIAlertController must have a title'

I have no idea where the problem.

Andrew
  • 372
  • 2
  • 5
  • 25

2 Answers2

2

in my recent app I used this localize , its simple and with out closing app we can switch the app based on the language .

add the localize.swift file to your project and finally call the following func where you need

if let pre = Bundle.main.preferredLocalizations.first, !pre.isEmpty  {
            if pre == Language.english.rawValue{
                Language.language = Language.arabic
            }else{
                 Language.language = Language.english
            }

        }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

You can't restart the app after language selection as the apple doesn't allows this.Instead, one thing you can do is modify the localization solution you have implemented to change app language without restarting it.

Follow this link for more details: manual language selection in an iOS-App (iPhone and iPad)

nikksindia
  • 1,147
  • 1
  • 10
  • 18