DeviceLanguage SaveInUserDefaults Swift 3
akandor opened this issue · comments
Hi, when I switch to a different language, the app switches to that language and the language is saved for the next time. If I switch back to the DeviceLanguage the app switches to the Device Language on the fly. But after restarting the app, the app is translated into the language translated which I selected before switching back to the DeviceLanguage.
This is in my viewDidLoad:
Localisator.sharedInstance.saveInUserDefaults = true
And this I what I call in the didSelectRowAt function
if indexPath.section == 0 { if SetLanguage(arrayLanguages[0]) { print("Set Device Language") } } else { if SetLanguage(arrayLanguages[indexPath.row + 1]) { print("Set User wanted language") } }
Nothing more
Cheers
Ok I added userDefaults.removeObject(forKey: kSaveLanguageDefaultKey) on your setLanguage
fileprivate func setLanguage(_ newLanguage:String) -> Bool {
if (newLanguage == currentLanguage) || !availableLanguagesArray.contains(newLanguage) {
return false
}
if newLanguage == "DeviceLanguage" {
currentLanguage = newLanguage
dicoLocalisation = nil
userDefaults.removeObject(forKey: kSaveLanguageDefaultKey)
NotificationCenter.default.post(name: kNotificationLanguageChanged, object: nil)
return true
} else if loadDictionaryForLanguage(newLanguage) {
NotificationCenter.default.post(name: kNotificationLanguageChanged, object: nil)
if saveInUserDefaults {
userDefaults.set(currentLanguage, forKey: kSaveLanguageDefaultKey)
userDefaults.synchronize()
}
return true
}
return false
}
}
Nice catch, thanks for submitting the workaround :)