I'd like to show the current language that the device UI is using. What code would I use?
I want this as an NSString
in fully spelled out format. (Not @"en_US")
EDIT: For those driving on by, there are a ton of useful comments here, as the answer has evolved with new iOS releases.
This will probably give you what you want:
It will show the name of the language, in the language itself. For example:
If you're looking for preferred language code ("en", "de", "es" ...), and localized preferred language name (for current locale), here's a simple extension in Swift:
}
SWIFT-4
Translating language codes such as en_US into English (United States) is a built in feature of
NSLocale
andNSLocale
does not care where you get the language codes from. So there really is no reason to implement your own translation as the accepted answer suggests.Prints: English, German, Swedish
Solution for iOS 9:
language = "en-US"
languageDic will have the needed components
countryCode = "US"
languageCode = "en"
You can use the
displayNameForKey:value:
method ofNSLocale
:This will print out:
If you specify the same locale identifier for the
initWithLocaleIdentifier:
and also thedisplayNameForKey:value:
method, then it will give you the native name of the language. I've discovered that if you remove the country code and use justfr
anden
, that it will also omit the country from the display name (on Mac OS X at least, not sure about iOS).