7

I have an iPhone application which has a button to change display language in run time. I have looked at NSLocalizedString() which will return the appropriate strings according to system preference. What are my options rather than hard coding all the display strings and return according to user language selection in run time? Any pointers will be much appreciated.

  • See http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language – Elliot Jun 01 '10 at 02:54

4 Answers4

15

Based on the post by the user "object2.0", I've put together some sample code you can use in your application to change the UI language on the fly.

The main localization class that does the hard work:

-(NSString *) localized:(NSString *) key
{
  GameInfo *gameInfo = [GameInfo sharedInstance];

  // langCode should be set as a global variable somewhere
  NSString *path = [[NSBundle mainBundle] pathForResource:langCode ofType:@"lproj"];

  NSBundle* languageBundle = [NSBundle bundleWithPath:path];
  return [languageBundle localizedStringForKey:key value:@"" table:nil];
}

Assuming you have this function in a global class called utils, call this function with the following code (for example to output the word "Settings".

NSLog( [utils localized:@"Settings"] );

To change the language:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:langCode, nil] forKey:@"AppleLanguages"];
Chuck Smith
  • 2,101
  • 3
  • 16
  • 21
  • nice summary :) BTW, we can get langCode from NSUserDefaults as well, if we save the selected language to the NSUserDefaults after the user changes the language on the fly. (dunno if this operation is an expensive operation, though) – aslı Jul 08 '11 at 07:58
  • @CheckSmith why "ofType:@"lproj" ?? I often create Localizable.strings – onmyway133 Nov 13 '13 at 03:00
5

Use to set language order by force

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en",@"de",..., nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

then use

NSLocalizedString();

to show localized string...

kviksilver
  • 3,824
  • 1
  • 26
  • 27
5

The trick to use specific language by selecting it from the app is to force the NSLocalizedString to use specific bundle depending on the selected language ,

here is the post i have written for this http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html

and here is the code of one sample app https://github.com/object2dot0/Advance-Localization-in-ios-apps

object2.0
  • 891
  • 1
  • 12
  • 12
1

The correct "User experience" is for the user to select their language via the system preference panel; not your app (or your app's settings panel, etc.). There is no way to override this per-app and you wouldn't want any app changing the system wide setting.

geowar
  • 4,397
  • 1
  • 28
  • 24
  • 5
    I disagreed. For TV game, I prefer to Japanese because I play Japanese games from childhood. But I like to use my mother language for other apps. – AechoLiu Apr 07 '11 at 01:09
  • Our app may not support the system preference in several countries where it's introduced, so overriding say EN with DE would be a win for many customers that have another second language than default=EN. – Jonas Byström Oct 27 '11 at 09:05
  • This presumes the app isn't displaying multiple languages at once. – Monte Hurd Apr 22 '15 at 01:07
  • I'm hearing two enhancement requests: 1) a user should be able to specify the preferred language per-application and 2) be able to specify a language priority list. See . – geowar Apr 22 '15 at 16:10