How to get localized language name from language code?

Did you ever needed full localized language name for language code? Today I was that same situation so I made small investigation. So, from the server side I am getting list of entities that have language string. This language string was in fact language code.
This is great practice when you wanna keep everything under current language abbreviation standard but it can be pain in the ass from the UX perspective. First thing that drop on my mind is to try to find language code for name list (.xml  or .plist format) and then parse list into NSDictionary. Wrong way. I have forgot that everything I needed was already under CoreFoundation.

+ (NSString*)languageNameForCode:(NSString*)locStr {
NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:locStr];
NSString *langStr = [NSString stringWithFormat:NSLocalizedString(@"%@", nil),
[loc displayNameForKey:NSLocaleIdentifier value:[loc localeIdentifier]]];
[loc release];
return langStr;
}

This simple method will return full language name string for language code string. The loop below will print all full languages names for [NSLocale ISOLanguageCodes] codes.

for (NSString *languageCode in [NSLocale ISOLanguageCodes]) {
NSLocale *loc = [[NSLocale alloc] initWithLocaleIdentifier:languageCode];
NSString *langStr = [NSString stringWithFormat:NSLocalizedString(@"%@", nil),
[loc displayNameForKey:NSLocaleIdentifier value:[loc localeIdentifier]]];
NSLog(@"%@ : %@", languageCode, langStr);
[loc release];
}

Hope it helps. That’s all for today! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *