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

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

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


当前回答

两封信的格式。苹果使用ISO标准ISO-3166。

NSString *localeCountryCode = [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode];

其他回答

Swift 4的更新答案

let language = Bundle.main.preferredLocalizations.first

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

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

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

下面的函数将遍历首选语言列表,并检查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)
    }
} 

I actually misread the original question, thought it asked for the "app UI" language (that's what I had googled for), not the "device UI", in which case the best answers would be the ones using preferredLocalizations, but those answers still give you a code, there is one more step to get a nice string to display. So, while the "device UI" language is already answered, if you want to display a nice string for which of the UI languages you support is currently in use, obviously the simplest solution is:

NSLocalizedString(@"currentLanguage", @"")

在你的每一个UI本地化中你都指定了你想要的显示方式。例如,在你的。strings文件的en版本中,你会有:

"currentLanguage"="English";

在你的fr版本的.strings文件中,你会有:

"currentLanguage"="Francais";

等。不要混淆代码等,你可以让你自己的字符串很好地匹配你的UI。

如果你只想得到语言,这里是我建议的答案:

NSString *langplusreg = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString * langonly = [[langplusreg componentsSeparatedByString:@"-"] 
objectAtIndex:0];

在我的情况下,我只是想Locale语言而不是Locale区域。

输出: 如果你的区域语言是日语,区域区域是日本,那么:

朗加雷格 = 是-JP

朗只 = 和

两封信的格式。苹果使用ISO标准ISO-3166。

NSString *localeCountryCode = [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode];