Reputation: 6296
How can I get the current ISO language code in IOS?
Upvotes: 4
Views: 5031
Reputation: 1211
[[NSLocale currentLocale] localeIdentifier]
will give you a string that's closer to what you are looking for. For my device here in the US, I get "en_US".
If you want a more standard looking "en-US", you can take use [[[NSLocale currentLocale] localeIdentifier] stringByReplacingOccurrencesOfString:@"_" withString:@"-"]
instead.
If you just want the language code, you can pass the output of [[NSLocale currentLocale] localeIdentifier]
to [NSLocale componentsFromLocaleIdentifier:]
which will give you an NSDictionary back. One of the keys in the dictionary will be NSLocaleLanguageCode
which will have a NSString object with just the language code.
Upvotes: 3
Reputation: 3886
NSString *isoCode = [[NSLocale preferredLanguages] objectAtIndex:0];
This will return a 2 letter iso code for the selected language on the device. "en" for english, "de" for german, and so on. I hope that helps!
Upvotes: 1