How to get language locale of the user in Objective-C?
Solution 1
NSLog(@"localeIdentifier: %@", [[NSLocale currentLocale] localeIdentifier]);
Solution 2
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.
Solution 3
code snippet
NSLocale *locale = [NSLocale currentLocale];
[locale objectForKey:NSLocaleLanguageCode]
Solution 4
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
Solution 5
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!
Ashwin J
Updated on May 26, 2020Comments
-
Ashwin J almost 4 years
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 information of which language is used?
-
Ashwin J almost 15 yearsThank you so much! I'm happy , If you give me some example codes
-
Chuck almost 15 yearsIt's not exactly the sort of thing where you can just give example code, since Mac user interfaces are usually stored in nibs and thus there isn't really any code responsible. Basically, there's a directory structure your app's resources need to follow and the OS will handle the rest for you. Read the localization docs in the link I gave you and you'll see how it works. For occasions where you are rendering text programmatically, you'll generally use the NSLocalizedString() function.
-
Alexander almost 10 years
[[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[[NSLocale currentLocale] localeIdentifier]]
can be used to get the plain-english version of the localeIdentifier