Use localized strings in storyboard

Viewed 11667

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?

6 Answers

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()
            }
        }
    }

}

In addition to Leon's answer, you can get rid of the need for an explicit subclass by using an extension:

extension UILabel {
    @IBInspectable var localizableText: String? {
        get { return text }
        set(value) { text = NSLocalizedString(value!, comment: "") }
    }
}

Make a button class and set @IBInspectable attribute to the button class

    class Button: UIButton {                 

      @IBInspectable public var referenceText: String = "" {
          didSet {
              self.setTitle(NSLocalizedString(referenceText, comment: ""), for: .normal)
          }
      }
}

Then in the storyboard you can set referenceTextenter image description here

The button text will be "Sign In"

Another possible way is to localize the storyboard and simply change the values for labels and buttons directly on the .string file.

First select the storyboard, and click on localize:

enter image description here

Then you'll be able to select the languages you want.

enter image description here

This way you'll be able to continue developing on your language of choice and simply edit the .string file that is generated

for button

extension UIButton {
@IBInspectable var localizableText: String? {
    get { return titleLabel?.text }
    set(value)
    {
    setTitle(NSLocalizedString(value!, comment: ""), for: .normal)
    }}}
Related