Swift中的本地化 – 即使应用程序有更多语言,也可以将应用程序限制为一种语言
我在Swift 3.2中开发了一个应用程序,目前使用的是基础语言英语。 我添加了阿拉伯语和法语的String文件。 但是直到我的团队完成和测试的所有内容我想将应用程序限制为一种语言,因为我需要解决的问题,即使是英文版本并上传到App Store。 删除sting和nib文件选项对我不起作用。
如果有任何代码我可以添加到appdelegate以限制仅适用于英语的应用,请告诉我。
这是一种做你想要的方法。 我不认为在代码中有另一种方法可以做到这一点。
// 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 }
编辑:基于这个答案,我找到了另一种方法。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { UserDefaults.standard.set(["Base"], forKey: "AppleLanguages") UserDefaults.standard.synchronize() // Rest of your init code }
编辑2:
您可以在初始ViewController
的viewDidLoad
中首次启动应用程序时执行的操作:
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() }
在didFinishLaunchingWithOptions
第一行添加以下代码到AppDelegate第一行强制app从左到右。 然后在编辑方案中 – 在Launch -AppleLanguages (Base)
上添加了一个参数。 我很努力地迫使用户使用英语。
UIView.appearance().semanticContentAttribute = .forceLeftToRight UserDefaults.standard.set(["Base"], forKey: "AppleLanguages") UserDefaults.standard.synchronize()
您是否尝试从项目文件中的本地化设置中删除语言?
这是基于apple提供的指南: 启用基础国际化
我相信你不必删除任何东西。 这只会禁用本地化。