iPhone apps and device language setting

Though it is possible and even relatively easy to create an iPhone app that uses a different language to the one set in the device settings, the feature is not very well documented and it can take a while to work out how to use it properly.

My iDEX for iPhone and iPad apps are an interface to a Romanian monolingual dictionary. On the iPad, though, Romanian is not available as a UI language, but I still wanted to give the user the option of selecting Romanian in the application settings so they can use the app in Romanian instead of English.

The way to go is to internationalize the app and localize it as usual. Then, in the main method, set the AppleLanguages key of the standard NSUserDefaults to the language and country of choice, like so


int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObject:@"ro_RO"] forKey:@"AppleLanguages"];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool drain];
return retVal;
}

It is imperative that the AppleLanguages key be set before the execution of UIApplicationMain(), because it is at that point that the UIKit framework is loaded and the default language determined.

It also important that both language and country (eg, @”ro_RO” and not just @”ro”) be specified, otherwise the app might crash when setting the key.

By doing as described in this post, all calls to NSBundle that support localization will look for the language that has been established programatically, as opposed to the one set on the device.

Hope this will save someone the pain I had to go through to find this out 😉

Join the Conversation

2 Comments

Leave a comment

Your email address will not be published. Required fields are marked *


*