0

I need to change my app language tapping a button in my app. I've used NSLocalizedStrings all around the app View Controllers and I already have Localizable.strings file created with the translations inside.

What code should I use to change the language without restarting my app?

I've tried this but it's not working.

@IBAction func changeToSpanish(_ sender: Any) {
    UserDefaults.standard.set("es", forKey: "i18n_language")
    UserDefaults.standard.synchronize()
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
eifersucht
  • 661
  • 8
  • 26
  • 1
    In general, you should not make your app's language different than the system setting. – Duncan C Aug 11 '17 at 13:31
  • It's a University app with 3 languages one of them regional. It's necessary to allow the user to change app language inside of the app. – eifersucht Aug 11 '17 at 13:35
  • You're setting the key in user defaults? How are you reading this key and applying it. – Abizern Aug 11 '17 at 13:35
  • I'm setting the key using the function above but I don't know how to read the key. – eifersucht Aug 11 '17 at 13:38
  • You can outsource the problem to a third party framework, or at least check their implementation for inspiration if you want. There are several, here is the first one coming on Google : https://github.com/marmelroy/Localize-Swift – Yoam Farges Aug 11 '17 at 13:43
  • Look [here](https://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language) – General Failure Aug 11 '17 at 13:43

0 Answers0