iOS: How to Get the Device Current Language Setting?

后端 未结 10 1256
北荒
北荒 2021-02-01 05:10

There are some features within my application that are supposed to be based on the language settings of the device where it\'s running.

I want to get the actual language

相关标签:
10条回答
  • 2021-02-01 05:35

    To know the current language selected within your localizations use

    [[NSBundle mainBundle] preferredLocalizations]
    

    Example:

    NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
    

    To get two letter word

    NSString *language = [[[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0] substringToIndex:2];
    

    Swift:

    let language = NSBundle.mainBundle().preferredLocalizations.first as NSString
    
    0 讨论(0)
  • 2021-02-01 05:36

    Working solution:

        let language = NSLocale.preferredLanguages()[0]
        let languageDic = NSLocale.componentsFromLocaleIdentifier(language) as NSDictionary
        //let countryCode = languageDic.objectForKey("kCFLocaleCountryCodeKey")
        let languageCode = languageDic.objectForKey("kCFLocaleLanguageCodeKey") as! String
        print(languageCode)
    
    0 讨论(0)
  • 2021-02-01 05:47

    Getting language and region in Swift:

        LF.log("language", NSLocale.preferredLanguages())
        LF.log("locale", NSBundle.mainBundle().preferredLocalizations)
    

    In my case I'm getting:

    language: '(
        "zh-Hans"
    )'
    locale: '(
        en
    )'
    
    0 讨论(0)
  • 2021-02-01 05:47
    NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 
    
    0 讨论(0)
  • 2021-02-01 05:50

    In Swift 4:

    let currentLanguage = Locale.current.languageCode
    

    It will give you just the language code, no country code.

    0 讨论(0)
  • 2021-02-01 05:54

    Try this:

    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
    NSString* currentLanguage = [arrayLanguages objectAtIndex:0];
    
    0 讨论(0)
提交回复
热议问题