Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: How to Get the Device Current Language Setting?

Tags:

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?

enter image description here

like image 904
Ohad Regev Avatar asked Jul 26 '11 11:07

Ohad Regev


People also ask

How do I find current device language?

You can use Locale. getDefault(). getLanguage(); to get the usual language code (e.g. "de", "en").

How do I get current locale in Swift?

Detailed Answer: current returns the current locale, that is, the value set by Settings > General > Language & Region > Region Formats.


2 Answers

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]; 
like image 59
Vladimir Avatar answered Sep 23 '22 08:09

Vladimir


Try this:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"]; NSString* currentLanguage = [arrayLanguages objectAtIndex:0]; 
like image 40
Sergio Avatar answered Sep 26 '22 08:09

Sergio