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?
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
}
Swift 4 If you have more languages on queue (preferredLanguage will returns: "uk-US" for example) but you want first of it.
You can do it like this:
let preferredLanguage = NSLocale.preferredLanguages[0]
if preferredLanguage.starts(with: "uk"){
print("this is Ukrainian")
} else{
print("this is not Ukrainian")
}
you may use the below code it works fine with swift 3
if Bundle.main.preferredLocalizations.first == "en" {
print("this is english")
}else{
print("this not english")
}
来源:https://stackoverflow.com/questions/29193284/check-language-in-ios-app