iOS如何重新启动应用程序以更改语言Swift 4 [英] iOS How to restart app for changing language Swift 4

查看:577
本文介绍了iOS如何重新启动应用程序以更改语言Swift 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多语言应用程序,我想在个人资料用户中手动更改语言.

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

我已经有Localization.string

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:

为此,我使用下一个代码:

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

我使用这个捆绑包:

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

对于此代码,我有:

当我按下按钮时,我得到了错误:

And when i press the button i get error:

原因:添加到UIAlertController的动作必须具有标题"

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

我不知道问题出在哪里.

I have no idea where the problem.

推荐答案

在我最近的应用程序中,我使用了此 localize ,它非常简单,并且没有关闭应用程序,我们可以根据语言切换应用程序.

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

将localize.swift文件添加到您的项目中,最后在需要的地方调用以下函数

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
            }

        }

这篇关于iOS如何重新启动应用程序以更改语言Swift 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆