Getting current device language in iOS?

前端 未结 30 2730
星月不相逢
星月不相逢 2020-11-22 09:10

I\'d like to show the current language that the device UI is using. What code would I use?

I want this as an NSString in fully spelled out format. (Not

30条回答
  •  一整个雨季
    2020-11-22 09:38

    warning The accepted, and the other answers all don't take into account that the preferred language can be another language than the device language.

    The device language is the language in which operating system elements and Apple apps are presented.

    The preferred language is the language the user would like to have apps localized in. Apple only provides a limited set of translations. If the preferred language is one language Apple translated their apps to, it will also be the device language. However if the user prefers a language for which Apple doesn't provide translations the device and preferred languages won't match. The device language will not be on first position in the preferred languages list.

    The following function will go through the preferred languages list and check if there is a translation in the Apple frameworks. The first language to have a translation is the device language. The function will return its language code.

    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
    }
    

    This works if the preferred language list is:

    1. Afrikaans (iOS is not translated into Afrikaans)
    2. Spanish (Device Language)

    The preferred language list can be edited in: Settings.app -> General -> Language & Region -> Preferred Language Order


    You can than use the device language code and translate it into the language name. The following lines will print the device language in the device language. For example "Español" if the device is set to spanish.

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

提交回复
热议问题