11

I want to get current selected iphone language. If using "NSLocale" it returns always the same language. It seems that this is not the one you choose inside iphone settings.

NSLocale * locale = [NSLocale currentLocale];
NSString * localLanguage = [locale objectForKey:NSLocaleLanguageCode];
NSLog (@"Language : %@", localLanguage); // Returns always : "en_US"

How to obtain the current language?

hhamm
  • 1,511
  • 15
  • 22
  • this returns "Region Format" instead of language. – Raptor Feb 23 '12 at 02:34
  • 1
    I'm also having same problem as yours. Tried all kinds of methods, even delete Xcode and simulators and reinstall, but not work. This happens to me after using Xcode 6.1. Before that, everything works well. Have you fixed this? What's the reason behind it? – S1U Oct 26 '14 at 18:41
  • 2
    s1u, see answer below. There's a bug in Xcode 6.1 – user3099609 Nov 04 '14 at 09:38

5 Answers5

16

The answer is to check the preferred language :

NSString *preferredLang = [[NSLocale preferredLanguages] objectAtIndex:0];
hhamm
  • 1,511
  • 15
  • 22
8

This question is old, but the reason is new since upgrading to Xcode 6.1
Due to a bug, the iOS8 simulator's [NSLocale currentLocale] always returns "en_US". It is alright on the device though. Hence, I recommend doing a test on a real device before bashing your head against a wall in frustration.


Sources: https://stackoverflow.com/a/26510914/3099609
https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc6_release_notes.html#//apple_ref/doc/uid/TP40001051-CH4-DontLinkElementID_23

Community
  • 1
  • 1
user3099609
  • 2,318
  • 18
  • 20
3

Locales encapsulate information about linguistic, cultural, and technological conventions and standards. Your code returns the "Region Format" information, if set "China", it will return "zh". Settings | General | International | Region Format

See “NSLocale Class Reference” for more information.

2

To add to what SteamTrout suggested:

To get your language to respond to changes in iPhone settings you're going to want to use autoupdatingCurrentLocale: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSLocale_Class/Reference/Reference.html#//apple_ref/occ/clm/NSLocale/autoupdatingCurrentLocale

If you read closely in the currentLocale reference you'll see that this value will not update from the settings panel: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSLocale_Class/Reference/Reference.html#//apple_ref/occ/clm/NSLocale/currentLocale

dcrow
  • 570
  • 5
  • 11
-1

Try [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleLanguageCode]

SteamTrout
  • 1,684
  • 1
  • 13
  • 9
  • this is also not working - it returns "en_US". It seems that Apple always returns the iphone initial language but not the one the user selected. – hhamm Aug 03 '10 at 14:07
  • The thing is, NSLocaleLanguageCode should not be even returning en_US but en only. - [NSLocale localeIdentifier] would be the one to return en_US. This is kinda odd. Maybe try CFLocale API? Sadly I don't have iDevice at hand to test it myself. Maybe anyone else can see if it works on theirs? – SteamTrout Aug 03 '10 at 15:26