gpt4 book ai didi

ios - NSLocale NSLocale preferredLanguages 不更新语言更改

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:15:36 45 4
gpt4 key购买 nike

我正在尝试使用以下代码获取用户选择的当前首选语言:

NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];

这是第一次运行应用程序时给我的首选语言,但是一旦我在系统设置中更改语言,更改就不会反射(reflect)出来。如果我卸载应用程序,那么更改语言然后重新安装应用程序,应用程序现在可以使用这种新语言(并且只显示这种语言)

这是正在发生的事情:

  • The language is set to English (Australian) when the app is first installed.
  • The app will show *language gives the value en-AU (correct value).
  • Now the language is now changed to Serbian.
  • After restarting the app *language is still giving en-AU whereas it should have sr-XX.
  • After re-installing the app, the language is showing the correct value sr-XX and the app is working as expected but now it won't change to any other language (as described)

我直接在 iPhone (iOS 11) 上测试该应用。我在项目设置中添加了本地化资源,只是为了确保我还在方案中将默认应用程序语言设置为首选系统

我也尝试使用 currentLocale 但它仍然给出相同的结果。

我还在某处找到了以下代码,这也是给数组 2 个元素,但其行为与 preferredLanguage 相同,并且不会随着语言的变化而变化。

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *languages = [[NSMutableArray alloc] initWithArray:[userDefaults objectForKey:@"AppleLanguages"]];

我是 iOS 开发人员的新手,不确定我哪里做错了。请帮忙。

最佳答案

如果您想要“始终反射(reflect)最新配置设置”的区域设置,请使用 autoupdatingCurrent在 Swift 中,autoupdatingCurrentLocale在 Objective-C 中。

您可能还想将自己确立为 NSLocale.currentLocaleDidChangeNotification 的观察者在 Swift 中,NSCurrentLocaleDidChangeNotification在 Objective-C 中,如果您希望在区域设置更改时应用程序恰好挂起或在后台运行时收到任何更改通知(例如,为了相应地更新 UI)。

关于ios - NSLocale NSLocale preferredLanguages 不更新语言更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48004191/

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