3

In Objective-C, I can easily get a list of the available Locales, like this:

NSArray *test = [NSLocale availableLocaleIdentifiers];
NSLog(@"%@", test);
for (int i = 0; i < [test count]; i++) {
    NSLog(@"%@", [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[test objectAtIndex:i]]);
}

This gives me a list such as this one:

Spanish (United States)
Macedonian (Macedonia)
Oromo (Kenya)
Danish (Denmark)
Korean (South Korea)
Tachelhit (Latin)
Fulah (Senegal)
Indonesian
Serbian (Cyrillic, Montenegro)
Makonde (Tanzania)
Welsh

However, rather than a list of the Locale names, I'd like to get a localized list of language names, as in the Settings app. For example, if the phone is in the US locale, I want to get "English," if the phone is in French, "Anglais," and if in German, "Englisch." What is the best way to accomplish creating such a localized list of language names?

Jason
  • 14,517
  • 25
  • 92
  • 153

2 Answers2

10
NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];

for (int i = 0; i < [languages count]; i++) {

    NSLog(@"%@", [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[languages objectAtIndex:i]]);

}
chunxi
  • 116
  • 1
  • 3
  • very nice... just notice that it will return the translated items in the current language, so take care, you could get "italian" or "italiano", depending on which language is setted in system preference... – meronix Feb 25 '11 at 18:36
1

You can use also this code:

NSArray *test = [NSLocale availableLocaleIdentifiers];
NSLog(@"%@", test);

for (int i = 0; i < [test count]; i++) {
    NSLog(@"%@", [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:[test objectAtIndex:i]]);
}

It shows more of languages.

Vitaliy
  • 64
  • 6