How to get the user's language in Objective-C?

I am developing an application for Mac OS X. I want to change the display contents to the language (English, Spanish, etc.) of the application user, how can I get information about which language is used?

+7
objective-c localization macos
source share
6 answers
NSLog(@"localeIdentifier: %@", [[NSLocale currentLocale] localeIdentifier]); 
+22
source share

You can use the NSLocale API to get this information, but there is no need to do what you want to do. OS X has localization support built into the OS - all you have to do is provide the appropriate language files, and the user can choose which language they want.

+8
source share

code snippet

  NSLocale *locale = [NSLocale currentLocale]; [locale objectForKey:NSLocaleLanguageCode] 
+6
source share

You want to "localize" your application. To get started, check out the Apple docs here: Internationalization - Apple Developer Docs . Without knowing more about your particular application, it would be hard to suggest anything else here!

+3
source share

You can use either method in two ways:

 NSString *language = [[NSLocale currentLocale] localeIdentifier]; NSLog(@"Language: %@", language); 

Output : Language: en_US

or that:

 NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0]; NSLog(@"Language: %@", language); 

Output : Language: ru

+2
source share

To be precise, there are changes from iOS 9 and above where [NSLocale preferredLanguages] are now coming back - and not only. So what better to do:

 NSString *languageOS = [[NSLocale preferredLanguages] objectAtIndex:0]; if([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) { languageOS = [[languageOS componentsSeparatedByString:@"-"] objectAtIndex:0]; } 
0
source share

All Articles