Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use localized strings in storyboard

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?

like image 325
JamesRGNT Avatar asked May 26 '17 09:05

JamesRGNT


People also ask

How do you set a localized string on a storyboard?

Another possible way is to localize the storyboard and simply change the values for labels and buttons directly on the . string file. Then you'll be able to select the languages you want.

What is a localized string?

A localized string can have different values depending on the language in which the project is being build. There are two categories of localized strings: the strings included in the installation package's UI, common to every MSI file.


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

}
like image 193
Leon Avatar answered Sep 25 '22 22:09

Leon


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: "") }
    }
}
like image 39
Johnson_145 Avatar answered Sep 21 '22 22:09

Johnson_145


According to your requirement it's not possible but

You don't need different storyboards for localization

Suppose you want to localize a label string.

  1. Draw and outlet and change text using

mylabel.text = nsLocalizedString("THIS_IS_MY_STRING",nil);

Of course in your localization file there will be a line.You must have different files for different language.Suppose you have a file for english and there must be a line.

"THIS_IS_MY_STRING" = "This is my string";

When you compile your app, that function will use mapping to localize your app.

Edit:

If you want detail information please have a look at these tutorials internationalization-tutorial-for-ios-2014

and ios-localization-tutorial

There are some online script(e.g localize.py) which will help you to automatically search all of your code and find out nslocalizedString function and make lines in your localizableString files. like this.

"THIS_IS_MY_STRING" = "THIS_IS_MY_STRING"

and later on you just have to write actual string there. :)

like image 25
elk_cloner Avatar answered Sep 24 '22 22:09

elk_cloner