Search code examples
iosswiftlocalizationlocalizable.strings

Having a language select option in an app using Swift


I have Googled this for hours but could find no advice for the following problem (using Swift).

I have seen in many apps an option to select the language from inside the app like this:

Choose your language screen
(source: unity3d.com)

I would like to achieve something similar, but am having trouble implementing it. I have localized my app and have my Localizable.strings files all set up. I'd imagine it has something to do with storing the language selected in NSUserDefaults, but how can I make the app use the appropriate Localizable.strings file once a language has been chosen? Or is that the wrong way to approach it?

How have others successfully implemented this feature in Swift?


Solution

  • I've already faced this issue on one of my project and found one solution :

    When the user select a language from a list, post a NSNotification. All your UIViewController subclass must register to this notification and change the labels value according to the new language selected.

    You can use the localized strings in order to set the labels, but NSLocalizedString(...) shouldn't be called since it's relying on the device language which is different from the application language.

    You can find my implementation of a custom localisator on Github, available both on Swift and objective-C.