How to change app locale independently from iOS system locale

Flags of european languagesI just want to share a trick on how to gain control over the Locale used for your app. The following code snippet is put in the main-method which kicks of your app.

But basically you should call this code snippet in your app and then make the user quit/terminate the app afterwards to restart with the new set default language AND set these parameters here again depending on a flag you may have stored elsewhere. Because actually all those set default values are actually already used at an earlier point in time i.e. for date formatting.

int main(int argc, char *argv[])
{
    @autoreleasepool {
        
        // OVERRIDE LOCALE (BASICALLY DO THIS WHILE APP RUNS AND LET USER RESTART APP)
        BOOL shouldOverrideLocale = NO; // LET'S TRY TO OVERRIDE IT WITH en_US
        NSString *languageIdentifier = @"en";
        NSString *countryIdentifier = @"US";
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        
        if( shouldOverrideLocale ) {
            NSLog( @"OVERRIDING SYSTEM LOCALE WITH : %@_%@",languageIdentifier, countryIdentifier );
            [defaults setObject:[NSArray arrayWithObject:languageIdentifier] forKey:@"AppleLanguages"];
            [defaults setObject:[NSArray arrayWithObjects:languageIdentifier, nil] forKey:@"NSLanguages"];
            [defaults setObject:[NSString stringWithFormat:@"%@_%@", languageIdentifier, countryIdentifier] forKey:@"AppleLocale"];
            [defaults synchronize];
        }
        else {
            NSArray *keysToRemove = @[@"AppleLanguages",@"NSLanguages",@"AppleLocale"];
            NSLog( @"RESETTING TO USE SYSTEM LOCALE" );
            @try {
                for( NSString *currentKey in keysToRemove ) {
                    if( [defaults objectForKey:currentKey] ) {
                        [defaults removeObjectForKey:currentKey];
                    }
                }
            }
	        @catch (NSException *exception) {
	            // NOTHNG TO CATCH HERE
	        }
	        @finally {
				[defaults synchronize];
	        }
        }
        NSString *languageCode = [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleLanguageCode];
        NSString *countryCode = [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode];
        NSLog( @"USING SYSTEM LOCALE: %@_%@", languageCode, countryCode );

        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

If you use this code, keep in mind, that strings and processes related to In-App-Purchase are still delivered in the native language of the user.

See also my Example App in the Github repo called LocaleOverrider.


preview

There may exist other ways of doing this. It may also be the better approach to handle all localization on your own an reinitializing NSBundle usage.

A more advanced insight into localization issues provides help on all the NSNumberFormatter and NSDateFormatter thingys, too. It is written by Ray Lillywhite and has the title „The Nitty Gritty of iOS Internationalization and Localization“.

Update 26.12.2018
How to change language elegantly at runtime.

Why do I blog this? I mentioned this code in UISprech podcast and I want to make it available to others. But be warned, it has side effects of providing the user e.g. with inconsistent language when he enters IAP e.g. and you need to find a way to elegantly terminate the app after you configured the new default language. (Pro-Tipp: shutting down an app that is already in the background won’t be noticed as a crash.)

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.