Ios – Use localized strings in storyboard

ioslocalizationstoryboard

I'm pretty new to iOS development and I was asking my self if it is possible to use localized strings from my "Localizable.strings" file directly into the storyboard.
For example in Android you can do it from the XML file like this:

android:text="@string/notConnected"

I understood that you can make a localized version of the storyboard, but having different strings files and different storyboards looks pretty ugly to me.

So is it possible to have only strings files and use what I need into the storyboard? Preferably without setting it from code?

EDIT:
This is practically what I want to do:
referencing string in storyboard

So is this possible? Is there a legit way to call a string from there like in Android?

Best Answer

I think being able to localise Strings in the storyboard is of significant advantage. I don't agree with @elk_cloner that hooking up IBOutlets for every UILabel is the way forward.

One way of getting it to work is using an @IBInspectable property on a UILabel subclass:

class LocalisableLabel: UILabel {

    @IBInspectable var localisedKey: String? {
        didSet {
            guard let key = localisedKey else { return }
            text = NSLocalizedString(key, comment: "")
        }
    }

}

In the storyboard set the custom class:

enter image description here

In the attributes inspector the localisedKey field will appear and you can just add your key here.

enter image description here

That's it!

EDIT:

You can localise UIButtons the same way, BUT if the text in the storyboard's title field differs from the localised String (which it will in other languages) the setting of the title will animate.

To fix this, put the setTitle in a performWithoutAnimation block:

class LocalisableButton: UIButton {

    @IBInspectable var localisedKey: String? {
        didSet {
            guard let key = localisedKey else { return }
            UIView.performWithoutAnimation {
                setTitle(key.localized, for: .normal)
                layoutIfNeeded()
            }
        }
    }

}