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?
This example demonstrate about how to Get the current language in Android device. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Change The App Language in iPhone or iPad AppsTap on the Settings app on the Home screen. Scroll down, select the app you wish to change its language. Select Language under Preferred Language. Choose the language you want to use.
The language that your app runs in is determined by the language setting, that is, Settings > General > Language & Region > Preferred Language Order.
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 5
Locale.current.regionCode // Optional("US")
Locale.current.languageCode // Optional("en")
Locale.current.identifier // en_US
With extension
extension Locale {
var isKorean: Bool {
return languageCode == "ko"
}
}
Locale.current.isKorean => false
Swift 4 If you have more languages in a queue (preferredLanguage will returns: "uk-US" for example) but you want first in 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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With