1

I am going to make a multi-langual iPad app, in which I want user to select a preferred langauge. So my question is, how can I change my application language at run-time ? I don't want to change iPad language.

Krrish
  • 135
  • 15

2 Answers2

3

This is not recommended by Apple guidelines.

My solution (user should restart app manually; to handle the language switch "on fly" you should reload all resources including XIBs) is:

// Use "nil" to fallback to system settings
void loadCustomLocalization(NSString * locCode) {
  NSString * appleLanguages = @"AppleLanguages";
  if (locCode) {
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:locCode] forKey:appleLanguages];
  } else {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:appleLanguages];
  }
  [[NSUserDefaults standardUserDefaults] synchronize];
}

void loadLocalization() {
  const int result = [[[NSUserDefaults standardUserDefaults] stringForKey:@"language"] intValue];
  NSString * language = 0;
  switch (result) {
    case 1: language = @"ru"; break;
    case 2: language = @"en"; break;
  }
  loadCustomLocalization(language);
}

int main(int argc, char *argv[]) {
  loadLocalization();
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  int retVal = UIApplicationMain(argc, argv, nil, nil);
  [pool release];
  return retVal;
}
1

This has been asked a few times. There's no great solution for this, it's non-trivial (notice for example that iOS does a restart when you change the language). Some people have achieved this by replacing NSLocalizedString calls and also by changing NIB loading so that they specify the localized version to use. See for example this other SO question.

Community
  • 1
  • 1
Clafou
  • 15,250
  • 7
  • 58
  • 89