解析ISO-639语言代码以显示完整的英语名称 [英] Parsing ISO-639 language codes to show full English language names

查看:348
本文介绍了解析ISO-639语言代码以显示完整的英语名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用商店上有一个应用,可让您启动其他语言的应用.您可以在此处查看屏幕截图: http://linguaswitch.com/

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/

在该屏幕快照中看不到它,但是该应用程序使用NSBundle -localizations作为数组来填充弹出菜单.

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

在更高版本的Xcode中,Apple从英语"移至"en". NSBundle -localizations返回捆绑包中的任何内容.因此,如果某个应用程序较旧(例如iCal/Pages),则会显示荷兰语",英语","pt","es","it",即根据本地化的添加时间进行混合.

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已要求我解析此列表.因此它是用户友好的".我需要它创建一种将ISO-639两个字母的代码再次解析为全英文名称的数组.

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!

推荐答案

我认为Apple希望您显示以下列表

I assume that Apple wants you to display a list of

  • 英语
  • 葡萄牙语
  • 法语

或者在法国系统上

  • 盎格鲁人
  • 葡萄牙人
  • Francais

因此,您需要将ISO语言代码转换为用户友好的本地化显示名称.此外,您需要获取未使用ISO语言代码命名的.lproj捆绑包的ISO语言代码.为此,您需要一个查找表,例如:

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];

然后

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

这篇关于解析ISO-639语言代码以显示完整的英语名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆