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?
Asked
Active
Viewed 105 times
1
Simon
- 24,728
- 42
- 144
- 256
3 Answers
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,155
- 3
- 35
- 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
- 697
- 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
- 73,440
- 16
- 126
- 165