Swift中的本地化-将应用程序限制为一种语言,即使该应用程序具有更多语言 [英] Localization in Swift - Restrict app to one language even if the app has more languages

查看:55
本文介绍了Swift中的本地化-将应用程序限制为一种语言,即使该应用程序具有更多语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Swift 3.2中开发了一个应用程序,该应用程序目前使用基本语言英语.我已经添加了阿拉伯文和法文的字符串文件. 但是直到我的团队完成并测试所有事情之前,我都希望将应用程序限制为一种语言,因为即使是英文版本,我也需要解决一些问题并上传到App Store. 删除sting和nib文件选项对我不起作用.

I have developed an app in Swift 3.2 which is currently live with base language English. I have added String files for Arabic and French. But till the time everything in completed and tested by my team I want to restrict app to one language as there would be issues I need to address even for the English version and upload to App store. Deleting the sting and nib file option will not work for me.

请告诉我是否有任何代码可以添加到appdelegate中,以限制仅英语的应用程序.

Please let me know if there is any code that I can add to appdelegate to restrict app only for English.

推荐答案

这是一种可以执行您想要的方法的方法.我认为代码中没有另一种方法.

Here's a method that do what you want. I don't think there's another way to do that in the code.

// You can change 'Base' to 'en' if you don't have Base.lproj folder 
func localizedString(_ key: String) -> String? {
    if let path = Bundle.main.path(forResource: "Base", ofType: ".lproj"),
        let baseBundle = Bundle(path: path) {
        return baseBundle.localizedString(forKey: key, value: nil, table: nil)
    }
    return nil
}

我基于此 answer 找到了另一种方法.

I found another way to do it based on this answer.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UserDefaults.standard.set(["Base"], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()
    // Rest of your init code
}

在初始ViewControllerviewDidLoad中首次启动应用程序可以做什么:

What you could do for the first launch of the app in the viewDidLoad of your initial ViewController:

    if let languageArray = UserDefaults.standard.object(forKey: "AppleLanguages") as? [String],
        languageArray.count >= 1,
        languageArray[0] != "Base" && languageArray.count == 1 {
        UserDefaults.standard.set(["Base"], forKey: "AppleLanguages")
        UserDefaults.standard.synchronize()
    } 

这篇关于Swift中的本地化-将应用程序限制为一种语言,即使该应用程序具有更多语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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