如何将CLPlacemark与语言关联(理想情况下为bcp47)

时间:2014-01-18 21:40:11

标签: ios core-location reverse-geocoding

我需要知道给定CLPlacemark中使用的语言。 有什么想法吗?

2 个答案:

答案 0 :(得分:1)

你可以从地方标记获得国家并使用其主要语言 - 这不是100%的解决方案,但在90%的情况下它应该工作

NSString *countryIso = [self countryIsoFromPlacemark:placemark]; 
NSString *languageIso = [self languageIsoFroPrimaryLanguageOfCountry:countryIso];

在你的应用程序中放置一个iso代码映射的plist,你就完成了

答案 1 :(得分:0)

我最终得到了以下100%自动解决方案,您根本不需要收集任何ISO代码。

+ (NSString*)detectBestLanguageBCP47:(CLPlacemark *)placemark {

    NSString * countryCode = placemark.ISOcountryCode;

    for (AVSpeechSynthesisVoice *voice in AVSpeechSynthesisVoice.speechVoices) {

        NSString *langCode = [voice.language substringWithRange:NSMakeRange(0, 2)];
        NSString *cc = [voice.language substringWithRange:NSMakeRange(3, 2)];

        int c = [cc compare:countryCode];

        if (c==0) {
             NSString *bcp47 =[ NSString stringWithFormat:@"%@-%@", langCode, cc];

            return bcp47;
        }

    }

    return nil; // Not found
}