Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

objective c - Parsing ISO-639 language codes to show full English language names

I have an app on the app store which lets you launch apps in different languages. You can see a screenshot here: http://linguaswitch.com/

You can't see it in that screenshot, but the app uses NSBundle -localizations as an array to populate the pop-up menu.

Apple moved away from "English" to "en" in later versions of Xcode. NSBundle -localizations returns whatever is in the bundle. So if an app is old (like iCal / Pages) then you get "Dutch","English","pt","es","it", i.e. a mixture depending on when the localization was added.

Apple have requested I parse this list. So it is "user friendly". I need it create some sort of array that parses ISO-639 two-letter codes into full English names again.

Any hints on how I could approach this? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I assume that Apple wants you to display a list of

  • English
  • Portugese
  • French

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...