I am developing an application for Mac OS X. I want to change indication contents by the language locale (English, Spanish, etc.) of the application user, how do I get informati
NSLog(@"localeIdentifier: %@", [[NSLocale currentLocale] localeIdentifier]);
code snippet
NSLocale *locale = [NSLocale currentLocale];
[locale objectForKey:NSLocaleLanguageCode]
You can use the NSLocale API to get that information, but it isn't necessary to do what you want to do. OS X has support for localization built into the OS — all you need to do is supply the appropriate language files and the user can select which language he wants.
To be exact there is a change with iOS 9 and greater where [NSLocale preferredLanguages] now return - instead of only . So it's better to do:
NSString *languageOS = [[NSLocale preferredLanguages] objectAtIndex:0];
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
languageOS = [[languageOS componentsSeparatedByString:@"-"] objectAtIndex:0];
}
You're looking to "localize" your application. To get started, check out the Apple docs here: Internationalization - Apple Developer Docs. Without knowing more about your specific application, it'd be hard to suggest anything more here!
you can use any way of both ways below:
NSString *language = [[NSLocale currentLocale] localeIdentifier];
NSLog(@"Language: %@", language);
output: Language: en_US
or this:
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
NSLog(@"Language: %@", language);
output: Language: en