RayX1993 RayX1993 - 1 year ago 104
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 ()

but cannot figure out how to do it.

Answer Source

You can use NSLocale's preferredLanguages method:

let language = NSLocale.preferredLanguages[0]

if language.hasPrefix("en") {
else if language.hasPrefix("ar") {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download