Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check language in iOS app

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?

like image 309
Pavel Zagorskyy Avatar asked Mar 22 '15 10:03

Pavel Zagorskyy


People also ask

How do I get current app 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.

How do I change the language on my iPhone apps?

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.

How do I get the language in Swift?

The language that your app runs in is determined by the language setting, that is, Settings > General > Language & Region > Preferred Language Order.


3 Answers

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
}
like image 114
Jonauz Avatar answered Oct 23 '22 22:10

Jonauz


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
like image 13
Changhoon Avatar answered Oct 23 '22 22:10

Changhoon


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")
 }
like image 5
Grzegorz R. Kulesza Avatar answered Oct 23 '22 22:10

Grzegorz R. Kulesza