仅在 iPhone 上重新启动后更改语言 [英] language change only after restart on iphone

查看:28
本文介绍了仅在 iPhone 上重新启动后更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改应用程序语言,但是当我在 main.h 中运行此代码时,在我关闭一个应用程序并再次运行它之后,语言会发生变化.这是否可以在不重新启动的情况下更改语言?

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;
}

推荐答案

更新答案如何在应用内更改语言"

NSLocalizedString()(及其变体)访问 NSUserDefaults 中的AppleLanguages"键以确定用户对首选语言的设置.这会返回一组语言代码,第一个是用户为其手机设置的语言代码,如果资源不以首选语言提供,则后续代码用作备用.

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.

如果您愿意,您可以使用 setObject:forKey: 方法来覆盖您自己的应用程序的全局设置,以设置您自己的语言列表,就像您所做的那样.这将优先于全局设置值,并返回到应用程序中执行本地化的任何代码.代码如下所示:

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:

[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de", @"en", @"fr", nil] forKey:@"AppleLanguages"]; 

注意:为了安全起见,请确保使用适当的预定义语言名称.

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.

@implementation LocalizeLanguage

static NSBundle *bundle = nil;

+(void)initialize {
     NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
     NSArray* languages = [defs objectForKey:@"AppleLanguages"];
     NSString *current = [[languages objectAtIndex:0] retain];
     [self setLocalizeLanguage:current];
}

/*
  [LocalizeLanguage setLocalizeLanguage:@"en"];
  [LocalizeLanguage setLocalizeLanguage:@"fr"];
*/

+(void)setLocalizeLanguage:(NSString *)lang {
     NSLog(@"preferredLang: %@", lang);
     NSString *path = [[ NSBundle mainBundle ] pathForResource:lang ofType:@"lproj" ];
     bundle = [[NSBundle bundleWithPath:path] retain];
}

+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
    return [bundle localizedStringForKey:key value:alternate table:nil];
}

@end

这篇关于仅在 iPhone 上重新启动后更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆