iOS: How to Get the Device Current Language Setting?

后端 未结 10 1257
北荒
北荒 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:56

    Swift:

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

    User preferred languages are stored can be retrieved from locale as array and current language identifier is the first object in that array:

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

    If you want language in more readable form then use displayNameForKey:value: method of NSLocale:

    NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSString *lang = [[NSLocale currentLocale] displayNameForKey:NSLocaleLanguageCode value:langID];
    
    0 讨论(0)
  • 2021-02-01 05:57

    Use below code to fetch Localised language without having trouble to the en-india, en-us etc..

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

    In and After ios9 this code need to take in cosideration

    0 讨论(0)
  • Find the solution in XCode's helper document, it wrote:

    Getting the Current Language

    To get the language that the app is using from the main application bundle, use the preferredLocalizations method in the NSBundle class:

    NSString *languageID = [[NSBundle mainBundle] preferredLocalizations].firstObject;

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