iOS How to Restart App for Changing Language Swift 4

Prevent Restart of IOS application after Language change

No you can't. Once Springboard is reloaded, all running apps are killed.

UPDATE: Thanks for @Mike's hint, iOS 6 introduced a new feature called State Preservation and Restoration. Read the links for more information. The App has to write handlers for preservation and restoration process in order to use this feature, and this feature is not guaranteed to have the app fully restored, as noted in the page (e.g. data model is not preserved).

Will I be able to restart my iPhone app programatically to apply language changes in Swift

Swift is a language, not an API. You have the same functionality available to you in Swift as in Objective C, although the syntax and ease of use may differ between the languages.

The answer you linked to therefore already answers the question.


Also, as Hemang points out, don't do this.

It's symptomatic of bad design that you would need to restart the app to change the displayed language.
It would be much better to fix the underlying problem than to apply a hacky band-aid solution such as this.

language change only after restart on iphone

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:

[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

Will I be able to restart my iPhone app programatically to apply language changes in Swift

Swift is a language, not an API. You have the same functionality available to you in Swift as in Objective C, although the syntax and ease of use may differ between the languages.

The answer you linked to therefore already answers the question.


Also, as Hemang points out, don't do this.

It's symptomatic of bad design that you would need to restart the app to change the displayed language.
It would be much better to fix the underlying problem than to apply a hacky band-aid solution such as this.



Related Topics



Leave a reply



Submit