Can i link a uilabel to a localizable string in Interface Builder?

16,934

Solution 1

You could implement a subclass of UILabel and have it automatically pull the localized value when it's initialized from the nib. In the XIB you would then just set the token (or english text if you prefer) and have UILabel pull the localized text from this value.

Solution 2

I present to you the swifty way

1. Create a subclass of UILabel

class LozalizedLabel : UILabel {

    @IBInspectable var keyValue: String {
        get {
            return self.text!
        }
        set(value) {
            self.text = NSLocalizedString(value, comment: "")

        }
    }
}

2. Set the class to be LocalizedLabel in IB

enter image description here

3. Enter your key from Localizable.strings directly in IB

enter image description here

Voila, now spend less time on creating useless IBOutlets just for making it a localizable UILabel.

NOTE This does not make the localized string show in the interface builder, but rather just the "key". So when building your TextView's remember to set constraints accordingly

Solution 3

NSLocalizedString has the advantage of being easy to implement. Risk is that you might forget to add an entry for a new Gui element.

You can use itool to dump the string from the original xib file, translate these dumped strings and then load the translated string into the other-language-xib file.

There are some advantages doing it this way compared to NSLocalizedString.

  • All the consideration for translation and string length still apply but you need to touch only the original xib, the other languages can be updated by script.
  • You see the elements already translated and the different possible states for each UIElement from the dumped file.
  • You will have more single strings files to send to your translator but it is easier to keep an overview as to what part is in need of translation, what part is done.
  • GUI Changes done to the english xib are applied to the localized xib, you are basically merging the original xib with the translated strings.

When you create a XIB you set it to being localized (as before), then run itool against this original xib to extract the strings. Translate the strings file into the desired language then load the translation into the localized xib.

If you google for ibtool --generate-strings-file you should find a few tutorials and examples.

The syntax should be similar to:

  #dump original xib to a $xibfile.strings file:
  ibtool --generate-strings-file Resources/English.lproj/$xibfile.strings Resources/English.lproj/$xibfile.xib
  # offline: translate the $xibfile.xib, place it into the correct lproj folder
  # then: merge translated $xibfile.strings with original xib to create localized xib
  ibtool --strings-file Resources/de.lproj/$xibfile.strings --write Resources/de.lproj/$xibfile.xib Resources/English.lproj/$xibfile.xib

$xibfile.strings in the Resources/English.lproj directory will contain the original (English language) strings. The translated $xibfile.strings must go in the appropriate xx.lproj directory.

If you add a buildscript to xcode then the loading of the translation will be called at each build.

Solution 4

There is no way to set then from the Localizable.strings directly with Interface Builder.

The reason you might want to translate the NIB is because the length of the words may vary in each language. Thus allowing you to change the layout of you nib per language.

What you could set them in the viewDidLoad method of your UIViewController.

- (void) viewDidLoad {
   [supper viewDidLoad];

   self.someLabel.text = NSLocalizedString(@"Some Label", @"Some label on some view.");
}

Solution 5

IB cannot use strings from Localizable.strings, but there is another option called Base Internationalization.

In short: you can create separate .strings file attached to the xib/storyboard for every required language. In the result you will have xib with it's own translations for each language. So it can duplicate some of the values that you have in Localizable.strings, but it's more clear way than setting through the code or copying xib files.

Please refer to the Apple Documentation about Base Internationalization.

Share:
16,934

Related videos on Youtube

Mathias
Author by

Mathias

Updated on June 04, 2022

Comments

  • Mathias
    Mathias almost 2 years

    have googled around but found no solution:

    Basically, i have a Localizable.strings set up, which i'm using in my code. However, it would be really sweet if i somehow could just refer those values in my XIB's too, so that i can avoid having to create one annoying XIB per language...

    Is this possible?

  • Mathias
    Mathias over 12 years
    Thats what i am doing, just wondered if there is a better way. Have to say that Android dev env setup is way ahead in this department. Ah well...
  • Resh32
    Resh32 over 11 years
    Alternatively, if you don't want to have IBOutlets for each of your labels just for the sake of translations, you can use tags instead.
  • Angel G. Olloqui
    Angel G. Olloqui about 11 years
    There is a better way. Check out my answer
  • KPM
    KPM about 9 years
    Obsolete answer. You'll want to use Base internationalisation with auto-layout constraints.
  • James Webster
    James Webster about 9 years
    What makes you say that? This is still possible
  • KPM
    KPM about 9 years
    "I don't think there is a way to do it with just nibs other than localizing the entire nib" is wrong.