开发者

Dont want to restart the app when changing language

开发者 https://www.devze.com 2023-04-03 11:08 出处:网络
I use this line of code to change the language in my app [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@\"en\", @\"fr\", nil]

I use this line of code to change the language in my app

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

Changes won't take place before the user restarts the app. Do the user really need to restart the app before the changes will take place, or is it possible to just reload th开发者_开发百科e view controllers in some way to make the app change its language immediately.


write below method in your appDelegate class
+(void)GetLangKey:(NSString *)Langkey
{
    NSString *tmpstr=[NSString stringWithFormat:@"%@",[[NSBundle mainBundle]pathForResource:@"LanguageResources" ofType:@"bundle"]]; 
    tmpstr =[tmpstr stringByAppendingString:@"/"];
    tmpstr=[tmpstr stringByAppendingString:Langkey];
    tmpstr =[tmpstr stringByAppendingString:@".lproj"];

    //  NSLog(@"%@",tmpstr);
    myLocalizedBundle=[NSBundle bundleWithPath:tmpstr];
}
and call this method when your language  change
if([language isEqualToString:@"English"])
{
[app_obj GetLangKey:@"en"];
} 
else
{
 [app_obj GetLangKey:@"fr"];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消