gpt4 book ai didi

ios - 无需重启应用程序即可快速切换应用程序语言

转载 作者:可可西里 更新时间:2023-10-31 23:59:49 34 4
gpt4 key购买 nike

我希望我的应用程序语言在单击按钮时发生变化..

我的代码是:

@IBAction func convertlang(_ sender: Any) {
if L102Language.currentAppleLanguage() == "en" {
L102Language.setAppleLAnguageTo(lang: "ar")
UIView.appearance().semanticContentAttribute = .forceRightToLeft
} else {
L102Language.setAppleLAnguageTo(lang: "en")
UIView.appearance().semanticContentAttribute = .forceLeftToRight
}
}

let APPLE_LANGUAGE_KEY = "AppleLanguages"
class L102Language {
class func currentAppleLanguage() -> String{
let userdef = UserDefaults.standard
let langArray = userdef.object(forKey: APPLE_LANGUAGE_KEY) as! NSArray
let current = langArray.firstObject as! String
return current
}

class func setAppleLAnguageTo(lang: String) {
let userdef = UserDefaults.standard
userdef.set([lang,currentAppleLanguage()], forKey: APPLE_LANGUAGE_KEY)
userdef.synchronize()
}}

这很好用,当我点击按钮时转换语言..

问题是我需要重新启动应用程序才能看到语言更改..

我搜索过这个,但其中大部分是针对 objective-c 的,并尝试了一些针对 swift 但没有用..

如何做到这一点?

最佳答案

您无法通过正常的按钮单击在运行时更改语言。如果您真的需要它,则需要改用自定义本地化系统。

为了在运行时更改应用程序语言,我通过创建两个不同的 Storyboard手动完成了此操作。我在 NSUserDefaults 中保存了语言首选项,但没有使用键 AppleLanguages,然后调用 AppDelegate 的 didFinishLaunchingWithOptions 方法来选择 Storyboard。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainEnglish" bundle:nil];
UINavigationController *navigationcontroller=[[UINavigationController alloc]init];
RegistorViewController *registor=[storyboard instantiateViewControllerWithIdentifier:@"Registor"];
[self.window setRootViewController:navigationcontroller];
[self.window makeKeyAndVisible];
[navigationvontroller pushViewController:registor animated:NO];

虽然这不是苹果希望开发人员在运行时做的事情。

Someone even asked an Apple engineer这是回应:

In general, you should not change the iOS system language (via use of the AppleLanguages pref key) from within your application. This goes against the basic iOS user model for switching languages in the Settings app, and also uses a preference key that is not documented, meaning that at some point in the future, the key name could change, which would break your application.

If you want to switch languages in your application, you can do so via manually loading resource files in your bundle. You can use NSBundle:pathForResource:ofType:inDirectory:forLocalization: for this purpose, but keep in mind that your application would be responsible for all loading of localized data.

关于ios - 无需重启应用程序即可快速切换应用程序语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49773929/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com