Programming Languages Hacks

Importanti regole per linguaggi di programmazione rilevanti come Java, C, C++, C#…

  • Subscribe

  • Lettori

    I miei lettori abituali

  • Twitter

Archive for the 'iphone' Category

iOS NIB Localization in Localizable.strings

Posted by Ricibald on 3rd June 2011


I want to localize my app AllSync but I was taken by a little frustration.

Following this good guide you can see that there are two main way to localize your iOS (iPhone/iPad) app:

  • localize strings using Localizable.strings
  • localize single nib file creating one version for each
The second point is very frustrating for three reasons:
  • you have to mantain updated all version of your nib localized files
  • there are specialized sites like icanlocalize that translates only your Localizable.string, not your nib files
  • you have to take care about the resulting layout of single translation
The problem:
I want to localize my nib files in the same way of using Localizable.strings

A possible solution can be to externalize in code every text setting but is a tedious process and you can’t use the flexibility of xcode 4. The real solution I hope will be used by many of you is this:

The solution:

Create an extended version of iOS user control that automatically translates every single text

Simple to obtain: just see this extended UIButton

@interface UIButtonThemed : UIButton

@implementation UIButtonThemed
- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder]))
        self.titleLabel.adjustsFontSizeToFitWidth = YES;
        NSString* text = NSLocalizedString(self.titleLabel.text, nil);
        [self setTitle:text forState:UIControlStateNormal];
        [self setTitle:text forState:UIControlStateHighlighted];
        [self setTitle:text forState:UIControlStateDisabled];
        [self setTitle:text forState:UIControlStateSelected];
	return self;

You can use it inside xcode 4 configuring the base class for your control:

Then you can see my italian Localizable.strings:
"Sync" = "Sincronizza";
"Settings" = "Impostazioni";
"Help" = "Guida";
"Share" = "Condividi";
"Rate us" = "Votaci";
"Follow us" = "Seguici";
And the corresponding result:

Posted in iphone, projects | 12 Comments »