Getting current device language in iOS?

前端 未结 30 2355
星月不相逢
星月不相逢 2020-11-22 09:10

I\'d like to show the current language that the device UI is using. What code would I use?

I want this as an NSString in fully spelled out format. (Not

30条回答
  •  太阳男子
    2020-11-22 09:30

    I tried to found out the right solution for myself. When I use Locale.preferredLanguages.first was returned the preferred language from your app settings.

    If you want get to know language from user device settings, you should the use string below:

    Swift 3

    let currentDeviceLanguage = Locale.current.languageCode
    // Will return the optional String
    

    To unwrap and use look at the line below:

    if let currentDeviceLanguage = Locale.current.languageCode {
        print("currentLanguage", currentDeviceLanguage)
    
        // For example
        if currentDeviceLanguage == "he" {
            UIView.appearance().semanticContentAttribute = .forceRightToLeft
        } else {
            UIView.appearance().semanticContentAttribute = .forceLeftToRight
        }
    }
    

提交回复
热议问题