Getting current device language in iOS?

前端 未结 30 2333
星月不相逢
星月不相逢 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:43

    Solution for iOS 9:

    NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
    

    language = "en-US"

    NSDictionary *languageDic = [NSLocale componentsFromLocaleIdentifier:language];
    

    languageDic will have the needed components

    NSString *countryCode = [languageDic objectForKey:@"kCFLocaleCountryCodeKey"];
    

    countryCode = "US"

    NSString *languageCode = [languageDic objectForKey:@"kCFLocaleLanguageCodeKey"];
    

    languageCode = "en"

    0 讨论(0)
  • 2020-11-22 09:43

    iOS13, Swift 5+, WWDC2019 https://developer.apple.com/videos/play/wwdc2019/403/

    Users can select the preferred language of an app independently from the OS language.

    You can use these:

        // Returns a list of the user's preferred languages.
        // Maybe more than (or none of) your app supports!
        Locale.preferredLanguages
    
        // a subset of this bundle's localizations, re-ordered into the preferred order
        // for this process's current execution environment; the main bundle's preferred localizations
        // indicate the language (of text) the user is most likely seeing in the UI
        Bundle.main.preferredLocalizations
    
    
    
        // The current running app language
        Bundle.main.preferredLocalizations.first
    
        // list of language names this bundle appears to be localized to
        Bundle.main.localizations
    
    0 讨论(0)
  • 2020-11-22 09:43
    -(NSString *)returnPreferredLanguage { //as written text
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSArray *preferredLanguages = [defaults objectForKey:@"AppleLanguages"];
    NSString *preferredLanguageCode = [preferredLanguages objectAtIndex:0]; //preferred device language code
    NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"]; //language name will be in English (or whatever)
    NSString *languageName = [enLocale displayNameForKey:NSLocaleIdentifier value:preferredLanguageCode]; //name of language, eg. "French"
    return languageName;
    
    }
    
    0 讨论(0)
  • 2020-11-22 09:43

    For Swift 3.0 below code can be used to answer your question:

     let language = Bundle.main.preferredLocalizations.first! as NSString
    
    0 讨论(0)
  • 2020-11-22 09:44

    Simple Swift 3 function:

      @discardableResult
      func getLanguageISO() -> String {
        let locale = Locale.current
        guard let languageCode = locale.languageCode,
              let regionCode = locale.regionCode else {
            return "de_DE"
        }
        return languageCode + "_" + regionCode
      }
    
    0 讨论(0)
  • 2020-11-22 09:46

    For Swift 3:

    NSLocale.preferredLanguages[0] as String

    0 讨论(0)
提交回复
热议问题