Reputation: 2072
I am trying to grab currentLanguage of a device using the below function. But there has been an issue with substring and I don't seem to understand as previous answers have such simple solution of importing Foundation to the file, which didn't work for me.
class func currentLanguage() -> String
{
let str = "en-US"
if let indexOfDash = str.characters.index(of: "-")
{
let langCode = str.substringToIndex(indexOfDash)
return langCode
}
}
In addition, What can be the best approach to get current language?
Upvotes: 0
Views: 926
Reputation: 2341
You need to use
let langCode = str.substring(to: indexOfDash)
And you can get current language like that:
let pre = NSLocale.preferredLanguages[0]
Upvotes: 3