Check language in iOS app

橙三吉。 提交于 2019-11-29 12:24:34

问题


Task is : I have got two UIImageViews, and I want present ImageView1 if system language is Ukrainian, and if it is not Ukrainian(English/Polish etc) I want present ImageView2.

I tried :

println(NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages"))

but this code gives only list of available languages. I also tried

var language: AnyObject? = NSLocale.preferredLanguages().first

but how can I compare this variable with English or Ukrainian language?


回答1:


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
}



回答2:


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")
 }



回答3:


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")
    }


来源:https://stackoverflow.com/questions/29193284/check-language-in-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!