仅在 iPhone 上重新启动后才更改语言
我正在尝试更改应用程序语言,但是当我在 main.h 中运行此代码时,在关闭应用程序并再次运行它后语言会发生变化。是否可以在不重新启动的情况下更改语言?
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *languages = [NSArray arrayWithObject:@"en_GB"];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
I'm trying to change app language, but when I run this code in main.h language chages after I shut down an App and run it again. Is this possible to change language without restarting?
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *languages = [NSArray arrayWithObject:@"en_GB"];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新答案“如何更改应用程序内的语言”
NSLocalizedString()
(及其变体)访问 NSUserDefaults 中的“AppleLanguages”键以确定用户的设置首选语言是。这将返回一组语言代码,第一个是用户为其手机设置的语言代码,如果资源在首选语言中不可用,则后续的代码将用作后备。如果您愿意,可以使用 setObject:forKey: 方法来覆盖您自己的应用程序的全局设置,就像您所做的那样。这将优先于全局设置值,并返回到应用程序中执行本地化的任何代码。其代码如下所示:
注意:为了安全起见,请确保使用适当的预定义语言名称。
下面是代码片段,但您的项目中必须包含所有本地化文件。
Update the answer "How to change the languages within the app"
NSLocalizedString()
(and variants thereof) access the "AppleLanguages" key in NSUserDefaults to determine what the user's settings for preferred languages are. This returns an array of language codes, with the first one being the one set by the user for their phone, and the subsequent ones used as fallbacks if a resource is not available in the preferred language.You can override the global setting for your own application if you wish by using the setObject:forKey: method to set your own language list just like you've done it. This will take precedence over the globally set value and be returned to any code in your application that is performing localization. The code for this would look something like:
Note: To be on safe side make sure that you use the appropriate pre-defined languages name.
Below is the code snippet, but you MUST have all localization files in your project.