Check language in iOS app
Asked Answered
H

5

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?

Him answered 22/3, 2015 at 10:30 Comment(2)
Did you log the result of NSLocale.preferredLanguages().first ? What did it give you.Auroora
it gives me Optional(en)Him
P
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
}
Pothook answered 22/3, 2015 at 11:17 Comment(3)
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?Outbreak
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.Pothook
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.Semen
K
15

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
Kemberlykemble answered 27/3, 2020 at 6:54 Comment(1)
Is there a way to determine whether this is a device level preference or application level preference? … since language settings can be done at either the device level or application level in the system Settings.app.Thaine
S
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")
 }
Seng answered 9/2, 2018 at 10:19 Comment(0)
R
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")
    }
Retroactive answered 12/10, 2017 at 12:48 Comment(0)
W
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
Willena answered 11/12, 2020 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.