3

Is it possible to have my app running in a different language than the one that is set in the OS? I want to have a language switch in my app’s setting menu, where the user can e.g. select german for the app, while his system is running in english.

From what I’ve already read, it seems it’s not possible without having my own localization mechanism…

What do you think?

starball
  • 20,030
  • 7
  • 43
  • 238
pawi
  • 123
  • 1
  • 5

2 Answers2

10

it can be done easily although it took me weeks to work out how ;-)

I have an iPhone app called iDEX which is a Romanian dictionary. On the iPad, Romanian is not available as a UI language, so I wanted to give the user the option of selecting Romanian in the application settings, as the device settings do not include it.

What you have to do is 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 your 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 you set the AppleLanguages key before you run UIApplicationMain(), because it is at that point that the UIKit framework is loaded and the default language determined.

It also important that you specify both language and country (eg, @"ro_RO" and not just @"ro") otherwise your app might crash when setting the key.

By doing this, all calls to NSBundle that support localization will look for the language that you have programatically established, as opposed to the one set on the device.

Hope that helps...

fedmest
  • 709
  • 5
  • 17
  • From what I've seen of Apple's documentation, only retrieval of this AppleLanguages key's values is mentioned. Have you met any difficulties to submit your app to the App Store? – Mick F Jan 11 '12 at 16:30
  • I have one app on the App Store that uses exactly this code. It was last updated about a year ago, with no problem... – fedmest Jun 21 '13 at 14:05
-1

You need to implement your custom resource loading mechanism to achieve that. For example you will no longer be able to use NSLocalizedString macro or [NSBundle pathForResource:]. You will always have to specify paths including the localization (as in de.lproj/MyStrings.strings instead of just MyStrings.strings). I would suggest against doing this unless absolutely necessary.

SteamTrout
  • 1,684
  • 1
  • 13
  • 9