1

I can get the user's language preferences by looking at [NSLocale preferredLanguages]. But if I offer 'en' and 'fr' translations, and the user's preferences are 'en-GB', 'de', 'fr', 'en' will they see my 'en' or my 'fr' translation? How do I get the language that the user is currently seeing in my app?

Simon
  • 25,468
  • 44
  • 152
  • 266

3 Answers3

3

If you want to see which of your localizations is being used, store a string in your strings file describing it ("en", "fr" or whatever) and then load it with NSLocalizedString.

Alastair Stuart
  • 4,165
  • 3
  • 36
  • 33
  • +1, clever workaround! Although be aware that your localizers might not understand why they are asked to translate "en" and might get the translation completely wrong as a result, so you'd need to remember to check this particular string carefully if you have a localization workflow. – Clafou Oct 11 '11 at 14:59
  • You could always add it at the build stage using a script negating the need to manually manage it. – Alastair Stuart Oct 11 '11 at 22:47
0
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
anderly
  • 727
  • 7
  • 16
  • Doesn't that give me the language they see on the home screen? I'm trying to find the language they see in my app. – Simon Oct 11 '11 at 08:56
  • See this post as it describes methods for allowing in-app language preferences and switching: http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language/1746920#1746920 – anderly Oct 11 '11 at 14:59
0

Multiple people claim the right way to do this would be:

NSArray *languages = [NSLocale preferredLanguages];
NSString *currentLanguage = [languages objectAtIndex:0];

Use at your own risk, though. NSLocale docs don't mention anything about first element being the current one.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169