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

查看:18
本文介绍了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
}

编辑 2:

您可以在初始 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天全站免登陆