我想要显示设备UI使用的当前语言。我应该使用什么代码?

我想把它作为一个NSString完全拼写出来的格式。(@ en_US)

编辑:对于那些开车路过的人来说,这里有大量有用的评论,因为随着新iOS版本的发布,答案也在不断变化。


当前回答

甚至还有更好的方法来获取当前设备语言。让我们试试下面的代码-

NSLog(@"Current Language - %@", [[NSLocale preferredLanguages] firstObject]);

这是阿比森的建议

其他回答

接受的答案和其他答案都没有考虑到首选语言可以是设备语言以外的另一种语言。

设备语言是操作系统元素和苹果应用程序所使用的语言。

首选语言是用户希望应用程序本地化的语言。苹果只提供有限的一组翻译。如果首选语言是苹果将其应用程序翻译成的一种语言,那么它也将是设备语言。但是,如果用户喜欢苹果没有提供翻译的语言,那么设备和首选语言将不匹配。设备语言将不在首选语言列表的第一位置。

下面的函数将遍历首选语言列表,并检查Apple框架中是否有翻译。第一种有翻译的语言是设备语言。函数将返回其语言代码。

func deviceLanguage() -> String? {
    let systemBundle: NSBundle = NSBundle(forClass: UIView.self)
    let englishLocale: NSLocale = NSLocale(localeIdentifier: "en")

    let preferredLanguages: [String] = NSLocale.preferredLanguages()

    for language: String in preferredLanguages {
        let languageComponents: [String : String] = NSLocale.componentsFromLocaleIdentifier(language)

        guard let languageCode: String = languageComponents[NSLocaleLanguageCode] else {
            continue
        }

        // ex: es_MX.lproj, zh_CN.lproj
        if let countryCode: String = languageComponents[NSLocaleCountryCode] {
            if systemBundle.pathForResource("\(languageCode)_\(countryCode)", ofType: "lproj") != nil {
                // returns language and country code because it appears that the actual language is coded within the country code aswell
                // for example: zh_CN probably mandarin, zh_HK probably cantonese
                return language
            }
        }

        // ex: English.lproj, German.lproj
        if let languageName: String = englishLocale.displayNameForKey(NSLocaleIdentifier, value: languageCode) {
            if systemBundle.pathForResource(languageName, ofType: "lproj") != nil {
                return languageCode
            }
        }

        // ex: pt.lproj, hu.lproj
        if systemBundle.pathForResource(languageCode, ofType: "lproj") != nil {
            return languageCode
        }
    }

    return nil
}

如果首选语言列表是:

南非荷兰语(iOS不翻译成南非荷兰语) 西班牙语(设备语言)

首选语言列表可在“设置”中编辑。app ->通用->语言和地区->首选语言顺序


您可以使用设备语言代码并将其转换为语言名称。下面几行将以设备语言打印设备语言。例如,如果设备设置为西班牙语,则为“Español”。

if let deviceLanguageCode: String = deviceLanguage() {
    let printOutputLanguageCode: String = deviceLanguageCode
    let printOutputLocale: NSLocale = NSLocale(localeIdentifier: printOutputLanguageCode)

    if let deviceLanguageName: String = printOutputLocale.displayNameForKey(NSLocaleIdentifier, value: deviceLanguageCode) {
        // keep in mind that for some localizations this will print a language and a country
        // see deviceLanguage() implementation above
        print(deviceLanguageName)
    }
} 

简单Swift 3功能:

@discardableResult
func getLanguageISO() -> String {
    let locale = Locale.current
    guard let languageCode = locale.languageCode,
          let regionCode = locale.regionCode else {
        return "de_DE"
    }
    return languageCode + "_" + regionCode
}

选择的答案返回当前设备语言,但不是应用程序中使用的实际语言。如果你没有在应用程序中为用户的首选语言提供本地化,则使用第一个可用的本地化,按用户的首选顺序排序。

要发现在本地化中选择的当前语言,请使用

[[NSBundle mainBundle] preferredLocalizations];

例子:

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

迅速:

let language = NSBundle.mainBundle().preferredLocalizations.first as NSString

从iOS 9开始,如果你只想要语言代码而不需要国家代码,你将需要这种帮助函数——因为语言将包含国家代码。

// gets the language code without country code in uppercase format, i.e. EN or DE
NSString* GetLanguageCode()
{
    static dispatch_once_t onceToken;
    static NSString* lang;
    dispatch_once(&onceToken, ^
    {
        lang = [[[NSLocale preferredLanguages] objectAtIndex:0] uppercaseString];
        NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^[A-Za-z]+" options:0 error:nil];
        NSTextCheckingResult* match = [regex firstMatchInString:lang options:0 range:NSMakeRange(0, lang.length)];
        if (match.range.location != NSNotFound)
        {
            lang = [lang substringToIndex:match.range.length];
        }
    });
    return lang;
}

对于Swift 3.0,下面的代码可以用来回答你的问题:

 let language = Bundle.main.preferredLocalizations.first! as NSString