I assume that Apple wants you to display a list of
Or on a French system
- Anglais
- Portugais
- Francais
So you'll need to turn ISO language codes into user-friendly, localized display names. In addition, you'll need to get the ISO language code for those .lproj bundles that are not named with the ISO language code. For this, you'll need a lookup table, e.g.:
NSLocale *englishLocale;
NSMutableDictionary *reverseLookupTable;
englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
reverseLookupTable = [NSMutableDictionary dictionaryWithCapacity:
[[NSLocale ISOLanguageCodes] count]
];
for (NSString *languageCode in [NSLocale ISOLanguageCodes]) {
NSString *displayName;
displayName = [[englishLocale
displayNameForKey:NSLocaleLanguageCode
value:languageCode
] lowercaseString];
if (displayName) {
[reverseLookupTable setObject:languageCode forKey:displayName];
}
}
[englishLocale release];
Then
// Find a pretty display name for a few sample languages
NSArray *testLanguages;
testLanguages = [NSArray arrayWithObjects:
@"en", @"fr", @"German", @"de", @"Italian", @"Some non-existing language", nil
];
for (NSString *language in testLanguages) {
if ([[NSLocale ISOLanguageCodes] containsObject:[language lowercaseString]]) {
// seems this already is a valid ISO code
NSLog(
@"%@ - %@",
language,
[[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:language]
);
} else {
// try finding an ISO code from our table
NSString *isoCode;
isoCode = [reverseLookupTable objectForKey:[language lowercaseString]];
if (isoCode) {
// yay
NSLog(
@"%@ - %@",
language,
[[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:isoCode]
);
} else {
// no result ... chances are this is not a real language, but hey
NSLog(@"%@ - no result", language);
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…