开发者

iOS: How to Get the Device Current Language Setting?

开发者 https://www.devze.com 2023-03-23 03:24 出处:网络
There are some features within my application that are supposed to be based on the language settings of the device where it\'s running.

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 and not some country settings. Foe example, if the language is English, I don't care if it's US, UK, Australia, etc...

I'm familiar with the NSLocale object, but it seems to relate to the Region Format setting and not to the Language setting (see screen shot below) so when I try to retrieve the language out of it using [locale displayNameForKey:NSLocaleIdentifier value:[locale localeIdentifier] I get things开发者_开发问答 like English (United States) instead of English; also, I think that what I need is the Language data and not the Region Format (am I right?).

Can anyone direct me to how to retrieve the language setting?

iOS: How to Get the Device Current Language Setting?


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];


Try this:

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0];


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


In Swift 4:

let currentLanguage = Locale.current.languageCode

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


Swift:

let language = NSBundle.mainBundle().preferredLocalizations[0] as NSString


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)


NSString * language = [[NSLocale preferredLanguages] objectAtIndex: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;


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号