I have to add arabic language support for an app written in objective c. How can i change the storyboard programatically with a language swap button, without restarting the app?
Even if usually the language is not meant to be changed from within the app, I faced the issue on a app I worked on and can face it regularly on StackOverflow.
I’ve created a Localisator class in both Swift and Objective-C, and you can find it at https://github.com/micazeve/iOS-CustomLocalisator.
The implementation is quite simple : when the user select a language from a list, the class post a
NSNotification. All your
UIViewController subclass must register to this notification and change the labels value according to the new language selected.
Thanks for the info, But i have to change the layout also.
The arabic contents are written from right to left
Oh sorry, I forgot about that !
The issue becomes more complicated since changing the whole layout instead of just the labels is much more complicated, and without changing language from App Settings it can be almost impossible for complicated layouts.
I suggest you to watch Platforms State of the Union video from WWDC 2015 (session 102), which presents Right to Left language support from at about 40:00.
If you have many storyboards, one per language, then you should just replace the view you are in (maybe throw a loading screen, then replace the underlying view … I guess in this case, the best option is to have Nib views instead)
But honestly, it’s a tedious way of doing things. If it’s pure translation (aka same app/layout/functionalities), it’s possible to achieve all this in a single storyboard (storyboards now support language direction as well, but you have to redo all your constraints if you did them in past versions of Xcode … Simply put, do your leading/trailing constraints not left/right ones).
Then, translate your storyboard (check “File Inspector” pane and be sure that each language is checked in “Localization”)
After that, you storyboard becomes a folder, that has many “strings” (txt files), one for each language, you can translate your text there.
The last thing is to use a notification as above, when a user switches the language, and just edit the `NSUserDefaults.standardUserDefaults().objectForKey(“AppleLanguages”)
PS: Don’t forget to rewrite any interface changing code to use current locale (`basically, anything that handles numbers, dates, strings, etc)