RTL on the fly for iOS in Swift 5
Today I’ll be showing you how to implement an RTL switch on the Fly for iOS Mobile Apps using Swift 5 and Scene Delegate.
This the approach I use to implement a multi-language app.
Step #1:
Create a new strings file named Localizable.strings
Step #2:
We would need to add the languages we wish to support by selecting the Project name and in Localizations we would add the new language, for example: Arabic
Make sure that Use Base Internationalization is selected.
Step #3:
Create a new singleton class named AppLanguage and add the following code to it.
We created an Enum of the languages we would need to support, it’s not required but it’s good to organize the code.
Then we created a singleton class from AppLanguage and created a method that sets the current language for the selected project set(index: Languages)
The setKeyWindowFromAppDelegate will reset our rootViewController, make sure you pass your own RootViewController.
Set your language in the SceneDelegate:
The String is extended with a new variable that returns the localized string from by utilizing the function toLocal() which will lookup for the localization key in the Localizable.strings file.
To localize a string just use “your.string.key”.localizedString
You’re now all set.
I hope this tutorial would be helpful and provides a good example on how to switch a language on iOS using Swift 5.