6

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

Bhavesh Lathigara
  • 1,355
  • 1
  • 15
  • 25
  • Does this answer your question? [How to get current language code with Swift?](https://stackoverflow.com/questions/24591167/how-to-get-current-language-code-with-swift) – Raptor May 13 '21 at 09:19
  • @Raptor no I tried everything from that link as well. you can check my code above and output, thanks. – Bhavesh Lathigara May 13 '21 at 09:25

2 Answers2

3

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

Raptor
  • 53,206
  • 45
  • 230
  • 366
Chanpreet Singh
  • 204
  • 1
  • 5
  • No, same issue only getting ["en"], I checked in both simulator and real device. In my preferred language in setting there are two languages in list Italian and English. – Bhavesh Lathigara May 13 '21 at 12:39
  • 2
    It worked for me. check for manage scheme under app target for app language and app region to system. – Chanpreet Singh May 14 '21 at 05:50
  • 1
    It worked, but just for others - App Target -> Edit Scheme -> App Language change to system language. And all variable will work not only "AppLanguages" but also you will get proper for all other variables like "preferredLanguages" "languageCode" etc... Thanks. – Bhavesh Lathigara May 14 '21 at 06:12
0

Have you tried Locale.current.identifier? current identifier

Tabish Sohail
  • 1,040
  • 2
  • 9
  • 22