Getting current device language in iOS?

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

    This will probably give you what you want:

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

    It will show the name of the language, in the language itself. For example:

    Français (France)
    English (United States)
    
    0 讨论(0)
  • 2020-11-22 09:20

    Swift

    To get current language of device

    NSLocale.preferredLanguages()[0] as String
    

    To get application language

    NSBundle.mainBundle().preferredLocalizations[0] as NSString
    

    Note:

    It fetches the language that you have given in CFBundleDevelopmentRegion of info.plist

    if CFBundleAllowMixedLocalizations is true in info.plist then first item of CFBundleLocalizations in info.plist is returned

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

    In Swift 4.2 and Xcode 10.1

    let language = NSLocale.preferredLanguages[0]
    print(language)//en
    
    0 讨论(0)
  • 2020-11-22 09:21

    According to Apple documentation

    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString* preferredLang = [languages objectAtIndex:0];
    
    0 讨论(0)
  • 2020-11-22 09:24

    Swift 3

    let locale = Locale.current
    let code = (locale as NSLocale).object(forKey: NSLocale.Key.countryCode) as! String?
    print(code!)
    
    0 讨论(0)
  • 2020-11-22 09:26

    Two letters format. Apple uses the ISO standard ISO-3166.

    NSString *localeCountryCode = [[NSLocale autoupdatingCurrentLocale] objectForKey:NSLocaleCountryCode];
    
    0 讨论(0)
提交回复
热议问题