freddiefujiwara
freddiefujiwara

Reputation: 59089

How to get language locale of the user in Objective-C?

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?

Upvotes: 8

Views: 26824

Answers (6)

chepiok
chepiok

Reputation: 245

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];
}

Upvotes: 0

Mohamad Chami
Mohamad Chami

Reputation: 1234

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

Upvotes: 4

lenhhoxung
lenhhoxung

Reputation: 2776

code snippet

 NSLocale *locale = [NSLocale currentLocale]; 
 [locale objectForKey:NSLocaleLanguageCode]

Upvotes: 6

chaostheory
chaostheory

Reputation: 1653

NSLog(@"localeIdentifier: %@", [[NSLocale currentLocale] localeIdentifier]);

Upvotes: 25

Ben Gotow
Ben Gotow

Reputation: 14893

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!

Upvotes: 3

Chuck
Chuck

Reputation: 237110

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.

Upvotes: 8

Related Questions