My application changes the language at runtime and advises the user to restart the application. I do it like this:
typealias LanguageLocaleType = (language: String?, locale: String?, title: String)
let elements: [LanguageLocaleType] = [
(nil, nil, "System Default"),
("en", "en_US", "English"),
("ar", "ar_SA", "Arabic"),
]
let element = elements[indexPath.row]
guard let language = element.language else {
UserDefaults.standard.removeObject(forKey: "AppleLanguages")
UserDefaults.standard.removeObject(forKey: "AppleLocale")
return
}
UserDefaults.standard.set([language], forKey: "AppleLanguages")
if let locale = element.locale, !locale.isEmpty,
Locale.current.identifier != locale,
!Locale.current.identifier.hasPrefix("\(language)_") {
UserDefaults.standard.set(locale, forKey: "AppleLocale")
}
I want to offer to install the languages in the list with the right choice, one of which is suggested to him to set the language back to System Default. However, there is no way to find the OS language and the level of language that I could find. Since after I got confused with the setup UserDefaults, Bundle.main.preferredLocalizationsit is not reliable and does not comply with the system standard (event when I delete the key from User Default).
Is there a way to get the language and locale at the OS level instead of the application level?
source
share