15

Is there any way to insert an NSLocalizedString in interface builder. For example set a label text to a localized string instead of a static string?

I really hate to create a property for every single item that requires a localized string.

sorin
  • 161,544
  • 178
  • 535
  • 806
aryaxt
  • 76,198
  • 92
  • 293
  • 442

6 Answers6

6

This post might have some tips for you:

http://blog.wilshipley.com/2009/10/pimp-my-code-part-17-lost-in.html

nevan king
  • 112,709
  • 45
  • 203
  • 241
2

Even if this post is old, for those interested in automatically localizing your IB files, check this out: https://github.com/angelolloqui/AGi18n

DISCLAIMER: I am the developer of the library

Angel G. Olloqui
  • 8,045
  • 3
  • 33
  • 31
  • @Angel, I am facing some issue with your library. Unable to properly install it in my machine using online curl statement. – Awais Tariq Mar 18 '14 at 07:07
  • I answered your issue in github, but basically you do not need to install the tools if all you want is to use the automatic localization of IB files. The tools are for extracting labels from IB to help you localize everything and merge the strings easily. – Angel G. Olloqui Mar 19 '14 at 08:52
2

You can take advanced of the User Defined Runtime Attributes:

http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html

First define a new category for UILabel:

#import "UILabel+Localized.h"

@implementation UILabel (Localized)
-(void) setTextLocalized:(NSString *)aText{
     [self setText:NSLocalizedString(aText, nil)];
}
@end

Then in the interface builder, User Defined Runtime Attributes :

textLocalized String your string to localized

enter image description here

rafaperez
  • 1,028
  • 12
  • 16
  • Very smart, but doesn't work for all elements, you end up writing a bunch of categories for each UI element (ex: segmentedCotnrol, title of pages, buttons, etc) – aryaxt Apr 26 '14 at 22:20
  • Great solution. Thank you! I had to use a one-word attribute ("localizable") because it failed with "textLocalized". Maybe I did something wrong but I comment this just in case it happens to someone else (I got: `this class is not key value coding-compliant for the key localizedText`). Also, I declared the method in the `interface` (maybe it's trivial, but I say that too). – Ferran Maylinch Jul 28 '14 at 21:18
  • You can select "Localized String" as the type of the attribute, and the value is supposed to be the string key... but it didn't work. It just displayed the key (like "user_greeting"). – Ferran Maylinch Jul 28 '14 at 21:26
2

To avoid creating a bunch of categories, create just one that categorize the NSObject and then check for the isKindOfClass as suggested. See the code below:

#import "NSObject+Localized.h"

@implementation NSObject (Localized)

///
/// This method is used to translate strings in .xib files.
/// Using the "User Defined Runtime Attributes" set an entry like:
/// Key Path: textLocalized
/// Type: String
/// Value: {THE TRANSLATION KEY}
///
-(void) setTextLocalized:(NSString *)key
{
    if ([self isKindOfClass:[UILabel class]])
    {
        UILabel *label = (UILabel *)self;
        [label setText:NSLocalizedString(key, nil)];
    }
    else if ([self isKindOfClass:[UIButton class]])
    {
        UIButton *button = (UIButton *)self;
        [button setTitle:NSLocalizedString(key, nil) forState:UIControlStateNormal];
    }
    else if ([self isKindOfClass:[UIBarButtonItem class]])
    {
        UIBarButtonItem *button = (UIBarButtonItem *)self;
        [button setTitle:NSLocalizedString(key, nil)];
    }
}

@end
OAK
  • 1,486
  • 2
  • 10
  • 6
1

NSLocalizedString is not the recommended way to localize Interface Builder files. Check out ibtool:

http://www.bdunagan.com/2009/03/15/ibtool-localization-made-easy/

Adam Ernst
  • 52,440
  • 18
  • 59
  • 71
  • 10
    I don't like using ibtool. because it creates multiple nib files for every language, and if you decide to change the interface you have to go to every nib for every language and change them all 1 by 1 – aryaxt Nov 02 '10 at 17:09
  • Regardless, ibtool is the right way to do it. Having separate nibs allows you to adjust button sizes or even the entire layout to fit longer strings or culture-specific expectations. ibtool also has some features that allow you to do "incremental localization"; see 'man ibtool' for more information. – Adam Ernst Nov 02 '10 at 18:43
  • ibtool doesn't work well when retrieving localization strings from a server as they aren't there at compile time. – Jeremy Wiebe Oct 04 '14 at 00:15
0

I have done same thing as @OAK mentioned. Here is full code.

Interface Builder Localization HowTo

Community
  • 1
  • 1
i.jameelkhan
  • 1,215
  • 1
  • 9
  • 10