RayX1993 RayX1993 - 1 month ago 20
iOS Question

Conditional code based on a device's language in Swift

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.

Answer

You can use NSLocale's preferredLanguages method:

let language = NSLocale.preferredLanguages[0]

if language.hasPrefix("en") {
    //english
}
else if language.hasPrefix("ar") {
    //arabic
}