[SOLVED] How to get system device language, swift iOS

Issue

How to get system device language, using swift iOS, Not app language, that I am handing 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 what ever 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.

Edit:
Not working in simulator and real device both

  • Checked in Simulator iPhone 11, OS 14.4

  • Checked in Real Device iPhone 7, OS 13.3

  • Xcode Version 12.4

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

Update and Solved
Thanks to @Charnpreet Singh
From Edit scheme you need to change App Language and above all variables print the same result as you will see in your device settings.
enter image description here

Solution

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.)

Answered By – Chanpreet Singh

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published.