iOS Como reiniciar o aplicativo para alterar o idioma Swift 4
Eu trabalho com o aplicativo multilíngue, quero alterar o idioma manualmente no perfil do usuário.
Eu já tenho Localization.string
E se eu mudar o idioma do dispositivo, o idioma do aplicativo mudará para.
Mas eu quero mudar o idioma manualmente a partir do exemplo de perfil de usuário:
E para isso eu uso o próximo código:
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)
}
E eu uso este pacote:
Bundle.main.localizations.filter({ $0 != "Base" })
Locale.current.localizedString(forLanguageCode: "en")
Para este código eu tenho:
E quando pressiono o botão, recebo um erro:
motivo: 'As ações adicionadas ao UIAlertController devem ter um título'
Eu não tenho idéia de onde está o problema.