如何使用 swift iOS 获取系统设备语言,而不是应用程序语言,我没有遇到任何问题。
我想从以下位置获取设备语言设置 -> 常规 -> 语言该用户将语言设置为。
我尝试了下面的代码:
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 =
因此,我没有获得用户在设备设置中设置的设备语言。
问题:
可能还是不可能?我尝试了一切。
所有代码仅获取en
or English,但是如果用户将语言设置为意大利语呢?我们怎样才能得到Italian or it
?
Note:我再说一遍,我不是在谈论应用程序语言,而是在谈论设备语言。
Edit:在模拟器和真实设备中都不起作用
还尝试过: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.
Try this
UserDefaults.standard.stringArray(forKey: "AppleLanguages")
输出将类似于 -["en-US"]
.
它将返回一个包含语言代码的数组,第一个索引是 iPhone 设置中设置的当前语言(如果设置中的首选语言顺序为空,它将是一个单项数组。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)