Use localized strings in storyboard
Asked Answered
T

5

22

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?

Trompe answered 26/5, 2017 at 9:54 Comment(3)
There are multiple WWDC videos across many years that cover Localisation and Internationalisation. Which ones have you watched? Apple also provide an Internationalization and Localization Guide which covers this topic; what problem are you having when you follow the documentation (please be specific): developer.apple.com/library/content/documentation/MacOSX/…Paulie
Please see my edit, it seems that I haven't been clear enoughTrompe
Did you read the documentation that I linked? What you are talking about is Base Internationalization.Paulie
D
7

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. :)

Dotted answered 26/5, 2017 at 10:11 Comment(3)
I meant that if I have a static text on my label Xcode creates a specific file for storyboard localization, but my question was about using the strings in my localization file without writing from code where to use it, but directly into the storyboard editorTrompe
Please see my edit to understand what I really asked, sorry if I wasn't clearTrompe
Well I'm perplessed about it being better but I'll document myself better, and if you edit your answer to also say that is not possible I'm gonna accept it, thank you for your time and patienceTrompe
Q
30

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

}
Quandary answered 30/4, 2018 at 18:19 Comment(6)
is there any way that Xcode will render the string correctly in the storyboard?Zobe
@Zobe I don't think that's possible no.Quandary
@Zobe sure, just use @IBDesignable in subclasses. @IBDesignable class DesignableButton: UIButton { } where UIButton has setter with set(value) { text = NSLocalizedString(value!, comment: "") }Melonie
@Melonie Niklas was asking about whether the localised text itself can be rendered in the storyboard.Quandary
@Quandary yes, and I provided the solution on how to render localised text in the storyboard. It is done by using IBDesignable specifier in a subclassMelonie
@Melonie Oh I see! Could you give me a full example as I'm not quite sure what you mean with the setter. I'm guessing what people want is the ability to put the localisedKey in the storyboard and it automatically renders the translated text, is that what you're suggesting can be done?Quandary
E
19

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: "") }
    }
}
Exception answered 26/11, 2018 at 11:18 Comment(2)
In this post you can find a detailed explanation of this approach, together with an example for a few widgets.Corned
Very nice and neat.Trueman
D
7

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. :)

Dotted answered 26/5, 2017 at 10:11 Comment(3)
I meant that if I have a static text on my label Xcode creates a specific file for storyboard localization, but my question was about using the strings in my localization file without writing from code where to use it, but directly into the storyboard editorTrompe
Please see my edit to understand what I really asked, sorry if I wasn't clearTrompe
Well I'm perplessed about it being better but I'll document myself better, and if you edit your answer to also say that is not possible I'm gonna accept it, thank you for your time and patienceTrompe
F
3

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"

Fortalice answered 26/11, 2018 at 11:38 Comment(1)
A great solution works perfectly! I adapted it for a UILabel and all my different views with no problem.Extraterrestrial
C
2

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

Cryogenics answered 7/11, 2019 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.