19

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?

Fattie
  • 27,874
  • 70
  • 431
  • 719
Pavel Zagorskyy
  • 443
  • 1
  • 4
  • 20

5 Answers5

41

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
}
YanSte
  • 10,661
  • 3
  • 57
  • 53
Jonauz
  • 4,133
  • 1
  • 28
  • 22
  • Do be aware that you are unwrapping an optional using the `as String`, and it could possibly be wise to use that within an `if let` construction. Just my two cents. And do you really just want to look at the first/[0] of the preferred languages, and not the entire array? – holroy Mar 24 '15 at 23:19
  • I think question was asking in regards for one language. You most likely don't need multiple languages. If you do, you can just take all array and work with each of the values. Also, I would like to believe there is always one language in the array, but because it would be an assumption, I updated my answer. – Jonauz Mar 24 '15 at 23:47
  • Does this take into account fallback languages? For example, someone might have their language choice set to "fr-CA" but the app may only have "fr" which iOS would choose to use. – Warpling Dec 10 '19 at 21:38
14

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
Hun
  • 3,652
  • 35
  • 72
5

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")
 }
Mohit Dharmadhikari
  • 3,750
  • 2
  • 20
  • 27
Grzegorz R. Kulesza
  • 1,324
  • 16
  • 10
3

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")
    }
Amr Angry
  • 3,711
  • 1
  • 45
  • 37
1

In addition to what mentioned before, you can add an entry in each Localizable file to tell you which dictionary is being used.

// Localizable.strings (en)

"language" = "en";

// Localizable.strings (ar)

"language" = "ar";

// Usage

NSLocalizedString("language", comment: "")
// check the resulted string