2

I'm trying to create if statements based on the device's language.

For instance: if device's language is English () else if device's language is Spanish () else if device's language is Arabic () ...etc.

but cannot figure out how to do it.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
RayX1993
  • 25
  • 5
  • Check out this post: http://stackoverflow.com/questions/29193284/check-language-in-ios-app .... You can find iOS language codes here: http://www.ibabbleon.com/iOS-Language-Codes-ISO-639.html ... @alexburtnik is correct, you use NSLocale.preferredLanguages. – user3353890 Nov 01 '16 at 21:15

2 Answers2

7

You have two parts. The first is to get the current language from the current locale.

Swift 3:

let languageCode = Locale.current.languageCode

The second part is to check which language it is:

if let languageCode = Locale.current.languageCode {
    switch languageCode {
        case "en":
            // handle English
        case "es":
            // handle Spanish
        case "ar":
            // handle Arabic
        default:
            // handle others
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

You can use NSLocale's preferredLanguages method:

let language = NSLocale.preferredLanguages[0]

if language.hasPrefix("en") {
    //english
}
else if language.hasPrefix("ar") {
    //arabic
}
alexburtnik
  • 7,661
  • 4
  • 32
  • 70