Check language in iOS app

Pavel Zagorskyy picture Pavel Zagorskyy · Mar 22, 2015 · Viewed 18.5k times · Source

Task is : I have got two UIImageViews, and I want present ImageView1 if system language is Ukrainian, and if it is not Ukrainian(English/Polish etc) I want present ImageView2.

I tried :

println(NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages"))

but this code gives only list of available languages. I also tried

var language: AnyObject? = NSLocale.preferredLanguages().first

but how can I compare this variable with English or Ukrainian language?

Answer

Jonauz picture Jonauz · Mar 22, 2015

Swift 3 You can take the language code like this

let preferredLanguage = NSLocale.preferredLanguages[0]

And then you need to compare it with code string

if preferredLanguage == "en" {
    print("this is English")
} else if preferredLanguage == "uk" {
    print("this is Ukrainian")
}

You can find codes here

An example to check if French ...

/// Is Device use french language 
/// Consider, "fr-CA", "fr-FR", "fr-CH" et cetera
///
/// - Returns: Bool
static func isFrench() -> Bool {
    return NSLocale.preferredLanguages[0].range(of:"fr") != nil
}