Alex Rivera | Logout

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

Asked 2011-09-14T08:54:13.977
18

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?

Edit
Report

3 Answers

18

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.

answered 2011-09-14T09:01:35.207
5

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<

answered 2011-09-14T10:24:45.760
1

You can do this in code:

    label.text = NSLocalizedString(@"key", @"comment");

But I don't think there is a way to do it with just nibs other than localizing the entire nib. I've certainly not found another way in the past.

answered 2011-09-14T08:56:48.610

Your Answer