Bhavesh Lathigara
Bhavesh Lathigara

Reputation: 1415

How to get iOS device system language?

How to get system device language, using Swift iOS? Not app language, that I am having no issue.

I want to get device language from Setting -> General -> Language that user set language to.

I tried below code:

let appLang = Locale.preferredLanguages
print("appLang ======== \(appLang)")

let bungleLang = Bundle.main.preferredLocalizations
print("bungleLang ======== \(bungleLang)")

let code = Locale.autoupdatingCurrent.languageCode
print("code ======== \(code ?? "")")

if let code = Locale.current.languageCode {
    print("Locale.current.languageCode ======== \(code)")
}

print("currentLanguage ======== \(Localize.currentLanguage())")

let local = getPreferredLocale()
print("local ======== \(local)")
print("languageCode = \(local.languageCode ?? "")")
print("regionCode = \(local.regionCode ?? "")")

Output:

appLang ======== ["en"]
bungleLang ======== ["en"]
code ======== en
Locale.current.languageCode ======== en
currentLanguage ======== en
local ======== en (fixed)
languageCode = en
regionCode =

So I am not getting device language, whatever the user set in his device settings.

Question:

Is it possible or not? I tried everything.

All code getting only en or English, but if the user set the language to Italian then? How we can get Italian or it?

Note: Again I am saying I am not talking about app language, I am talking about device language.

Not working in simulator and real device both

Also tried: UserDefaults.standard.stringArray(forKey: "AppleLanguages")

Upvotes: 11

Views: 7898

Answers (3)

Gucci
Gucci

Reputation: 149

Up to version iOS 16, we used Locale.current.languageCode. But in iOS 16 it was renamed so now we must use Locale.current.language.languageCode?.identifier.

But I encountered a problem since Locale.current.language.languageCode?.identifier will not return us the language identifier that the user is currently using, it will return the language identifier of the country where the user is located… Imagine you are using English on your phone and are in China, in this case, if you launch the app for the first time and have not yet chosen the language you will use – it will be in Chinese…

So here is another effective way to get the language identifier that the user is currently using…

One of the conditions for onboarding in iOS when you purchase a new device is the choice of language, which means that we will always have a preferred language...

We take the list - Locale.preferredLanguages and extract the first language from it, and then its identifier:

func determineDeviceLanguage() {

    var languageIdentifier: String = "en"
    let preferredLanguages = Locale.preferredLanguages
    if let deviceLanguage = preferredLanguages.first {
        languageIdentifier = String(deviceLanguage.prefix(2))
    } else {
        print("📕: Could not determine the device language.")
    }
    
    print("Your device language -> \(languageIdentifier)")
}

Upvotes: 1

Tabish Sohail
Tabish Sohail

Reputation: 1044

Have you tried Locale.current.identifier? current identifier

Upvotes: -2

Chanpreet Singh
Chanpreet Singh

Reputation: 264

Try this

UserDefaults.standard.stringArray(forKey: "AppleLanguages")

The output will be like - ["en-US"].

It'll return an array with language codes, first index is the current language set in iPhone's setting (it'll be a single item array if the preferred language order in settings is empty.)

Upvotes: 9

Related Questions