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.
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.
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
}
}
You can use NSLocale's preferredLanguages
method:
let language = NSLocale.preferredLanguages[0]
if language.hasPrefix("en") {
//english
}
else if language.hasPrefix("ar") {
//arabic
}