Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NSLocalizedString in IB [iPhone SDK]?

When I assign text programmatically, I can use NSLocalizedString(....), but if I am writing the text directly in IB (say to a UILabel) how do I localize it?

like image 678
erotsppa Avatar asked Jul 07 '09 19:07

erotsppa


4 Answers

One solution is to create multiple localized nib files. This probably works best if your localization needs are very straightforward and you're simply going hand your resources over to expert software localizers at the end of development.

If you're localizing while you're developing and the UI is changing rapidly, duplicate nib files can be a big pain, since every UI tweek must be duplicated in each local version of the nib. To avoid this, you'll need to write some code in your view controllers to handle setting the localized strings, typically in the view controller's -viewDidLoad method. Make sure that every control with localized text is an IBOutlet and wired them up to your view controller in IB. Then your view controller's -viewDidLoad will look something like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    hello.text = NSLocalizedString(@"Hello", @"Hello label");
    world.text = NSLocalizedString(@"world", @"world label");
    // ... etc
}

You do this type of view setup in -viewDidLoad since the objects in your nib aren't fully created until after your -init method runs. -viewDidLoad runs after -init but before your view becomes visible.

like image 62
Don McCaughey Avatar answered Sep 18 '22 11:09

Don McCaughey


This is a large and complex topic. A good starting place is Introduction to Internationalization Programming Topics and there is also Preparing Your Nib Files for Localization

like image 45
Mark Avatar answered Sep 19 '22 11:09

Mark


You can create multiple versions of a .nib file for each locale. There are also tools to let you edit the strings in them easily. Apple has pretty good documentation on this.

like image 34
Jesse Rusak Avatar answered Sep 21 '22 11:09

Jesse Rusak


I also was searching for a solution, not necessarily for iPhone, but XCode/IB in general. All references did not deal with the fact that you might need a key internally to indicate a state and want to display a localized string for the user in a label or text cell corresponding to that key. I did not found in first step a standard approach how to store e.g. a key value for a key in shared user defaults and display the localized string for that key value in a label.

I found a solution wich does not need many coding and is compliant with the bindings in ib.

First you provide a file Localizable.strings e.g. with a line containing

"MyKeyValue" = "Localized display label";

Now you can localize the key value with: NSLocalizedString(aKeyValue,nil).

In the label you did not find any value transformer dealing with NSLocalized String. So I created a class KeyToLocalizedStringTransformer to transform a key value into a localized string:

@interface KeyToLocalizedStringTransformer : NSValueTransformer {}

@implementation KeyToLocalizedStringTransformer

+ (Class)transformedValueClass
{
    return [NSString class];
}

+ (BOOL)allowsReverseTransformation
{
    return NO;
}

- (id)transformedValue:(id)aValue
{
    NSString *NLString = [NSString stringWithString:NSLocalizedString(aValue,nil)];
return NLString;
}

Last step for preparing is to register the transformer e.g. in +initialize:

NSValueTransformer *transformer = [[KeyToLocalizedStringTransformer alloc] init];
[NSValueTransformer setValueTransformer:transformer forName:@"KeyToLocalizedStringTransformer"];   

Now you can use a value transformer in the bindings for the text field or cell (Simply type in the name if you do see only the NSUnArchiveFromData and so on...)

Sorry no image here from IB 'cause I am new here and "have no reputation": you have to imagine the binding to the shared user defaults controller, Kontroller key: values, Model Key Path: MyStateKey and a value transformer as described.

As a result you dont have to do anything in the nl duplicated nib with the label, simply translate the string in the Localizable.strings.

like image 45
user767644 Avatar answered Sep 22 '22 11:09

user767644