Check language in iOS app

左心房为你撑大大i 提交于 2019-11-30 08:49:13
Jonauz

Swift 3 You can take the language code like this

let preferredLanguage = NSLocale.preferredLanguages[0]

And then you need to compare it with code string

if preferredLanguage == "en" {
    print("this is English")
} else if preferredLanguage == "uk" {
    print("this is Ukrainian")
}

You can find codes here

An example to check if French ...

/// Is Device use french language 
/// Consider, "fr-CA", "fr-FR", "fr-CH" et cetera
///
/// - Returns: Bool
static func isFrench() -> Bool {
    return NSLocale.preferredLanguages[0].range(of:"fr") != nil
}

Swift 4 If you have more languages on queue (preferredLanguage will returns: "uk-US" for example) but you want first of it.
You can do it like this:

let preferredLanguage = NSLocale.preferredLanguages[0]
if preferredLanguage.starts(with: "uk"){
 print("this is Ukrainian")
 } else{
 print("this is not Ukrainian")
 }

you may use the below code it works fine with swift 3

    if Bundle.main.preferredLocalizations.first == "en" {
        print("this is english")
    }else{
       print("this not english")
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!