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

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

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


当前回答

提供的解决方案实际上会返回设备的当前区域——而不是当前选择的语言。这些通常是同一件事。但是,如果我在北美,我将我的语言设置为日语,我的区域仍然是英语(美国)。为了检索当前选择的语言,你可以这样做:

NSString * language = [[NSLocale preferredLanguages] firstObject];

这将为当前选择的语言返回两个字母的代码。英语是“en”,西班牙语是“es”,德语是“de”,等等。更多例子,请参阅维基百科的条目(特别是639-1列):

ISO 639-1代码列表

然后,将这两个字母的代码转换为您想要显示的字符串就很简单了。如果是"en",显示"English"。

EDIT

值得引用NSLocale.h中的头信息:

+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0); // note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information

对应用语言感兴趣的人可以看看@mindvision的答案

其他回答

根据苹果文档

NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];

提供的解决方案实际上会返回设备的当前区域——而不是当前选择的语言。这些通常是同一件事。但是,如果我在北美,我将我的语言设置为日语,我的区域仍然是英语(美国)。为了检索当前选择的语言,你可以这样做:

NSString * language = [[NSLocale preferredLanguages] firstObject];

这将为当前选择的语言返回两个字母的代码。英语是“en”,西班牙语是“es”,德语是“de”,等等。更多例子,请参阅维基百科的条目(特别是639-1列):

ISO 639-1代码列表

然后,将这两个字母的代码转换为您想要显示的字符串就很简单了。如果是"en",显示"English"。

EDIT

值得引用NSLocale.h中的头信息:

+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0); // note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information

对应用语言感兴趣的人可以看看@mindvision的答案

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

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

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

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

这可能会给你你想要的:

NSLocale *locale = [NSLocale currentLocale];

NSString *language = [locale displayNameForKey:NSLocaleIdentifier 
                                         value:[locale localeIdentifier]];

它会在语言本身中显示语言的名称。 例如:

Français (France)
English (United States)

@amir在Swift中的回应:

// Get language prefered by user
let langageRegion = NSLocale.preferredLanguages().first!
let languageDic = NSLocale.componentsFromLocaleIdentifier(langageRegion)
let language = languageDic[NSLocaleLanguageCode]