Use localized strings in storyboard
I am new to iOS development and I was asking myself if it is possible to use localized strings from my Localizable.strings file directly into the storyboard. For example, on Android, you can do it from an XML file like this:
android:text="@string/notConnected"
I figured you could make a localized version of the storyboard, but having different line files and different storyboards looks pretty ugly to me.
So, is it possible to only have string files and use what I need in a storyboard? Preferably not installing it from code?
EDIT: This is practically what I want to do:
So is it possible? Is there a legal way to call a string from there like in Android?
source to share
As per your requirement, this is not possible, but
You don't need different storyboards for localization
Let's say you want to localize the label string.
- Draw and take out and modify text using
mylabel.text = nsLocalizedString ("THIS_IS_MY_STRING", nil);
Of course there will be a line in your localization file. You must have different files for different languages. Suppose you have a file for English and there should be a line there.
"THIS_IS_MY_STRING" = "This is my string";
When compiling your application, this function will use mapping to localize your application.
Edit:
If you want more details check out these internationalization-tutorial-for-ios-2014 tutorials
There is a script on the web (like localize.py) that will help you automatically search all your code and detect the nslocalizedString function and create strings in localizableString files. like this.
"THIS_IS_MY_STRING" = "THIS_IS_MY_STRING"
and then you just need to write the actual line. :)
source to share
I think the ability to localize String
in a storyboard has a significant advantage. I disagree with @elk_cloner that connecting IBOutlet
for everyone UILabel
is the way forward.
One way to make it work is to use the @IBInspectable property in the subclass UILabel
:
class LocalisableLabel: UILabel {
@IBInspectable var localisedKey: String? {
didSet {
guard let key = localisedKey else { return }
text = NSLocalizedString(key, comment: "")
}
}
}
The localizedKey field will appear in the storyboard and you can just add your key here.
What is it!
source to share