Reputation: 5711
There are some features within my application that are supposed to be based on the language settings of the device where it's running.
I want to get the actual language and not some country settings. Foe example, if the language is English, I don't care if it's US, UK, Australia, etc...
I'm familiar with the NSLocale
object, but it seems to relate to the Region Format
setting and not to the Language setting (see screen shot below) so when I try to retrieve the language out of it using [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier]
I get things like English (United States)
instead of English
; also, I think that what I need is the Language data and not the Region Format (am I right?).
Can anyone direct me to how to retrieve the language setting?
Upvotes: 25
Views: 38066
Reputation: 4127
In Swift 4:
let currentLanguage = Locale.current.languageCode
It will give you just the language code, no country code.
Upvotes: 3
Reputation: 1578
Working solution:
let language = NSLocale.preferredLanguages()[0]
let languageDic = NSLocale.componentsFromLocaleIdentifier(language) as NSDictionary
//let countryCode = languageDic.objectForKey("kCFLocaleCountryCodeKey")
let languageCode = languageDic.objectForKey("kCFLocaleLanguageCodeKey") as! String
print(languageCode)
Upvotes: 2
Reputation: 2870
Find the solution in XCode's helper document, it wrote:
Getting the Current Language
To get the language that the app is using from the main application bundle, use the preferredLocalizations method in the NSBundle class:
NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;
Upvotes: 1
Reputation: 1303
Use below code to fetch Localised language without having trouble to the en-india, en-us etc..
NSString *Ph = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
In and After ios9 this code need to take in cosideration
Upvotes: 0
Reputation: 20670
To know the current language selected within your localizations use
[[NSBundle mainBundle] preferredLocalizations]
Example:
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
To get two letter word
NSString *language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];
Swift:
let language = NSBundle.mainBundle().preferredLocalizations.first as NSString
Upvotes: -1
Reputation: 7238
Getting language and region in Swift
:
LF.log("language", NSLocale.preferredLanguages())
LF.log("locale", NSBundle.mainBundle().preferredLocalizations)
In my case I'm getting:
language: '(
"zh-Hans"
)'
locale: '(
en
)'
Upvotes: 3
Reputation: 39181
Swift:
let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString
Upvotes: 2
Reputation: 3416
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
Upvotes: 1
Reputation: 170839
User preferred languages are stored can be retrieved from locale as array and current language identifier is the first object in that array:
NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
If you want language in more readable form then use displayNameForKey:value:
method of NSLocale:
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
Upvotes: 52
Reputation: 1732
Try this:
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];
Upvotes: 5