Gianni Costanzi
Gianni Costanzi

Reputation: 6104

Interface Builder Localization HowTo

I was digging into Apple Developers' Docs to understand how localization/internationalization of apps should be done and I wanted to understand how Interface Builder helps you managing multiple NIB files for multiple languages... the docs point me to the "localization" section within the Interface Builder user Guide, but I can not find it.. should I find it within the Xcode 4 docs? Can someone point me a document that shows you how to implement localization within the Interface Builder/XCode? Where is the "Interface Builder User Guide"?

Thanks in advance.

PS: should I start implementing my app using Localized strings/bundles from the beginning even if I only start with a single language? Is it a pain to "internationalize" an application that has not been developed with multiple languages' support in mind?

Upvotes: 3

Views: 4851

Answers (3)

i.jameelkhan
i.jameelkhan

Reputation: 1225

I have wrote a simple category which handles Localisation with IB.

Header file looks like this.

@interface UIView (Localization)

@property (nonatomic, strong) NSString *mainTextKey;
@property (nonatomic, strong) NSString *secondaryTextKey;

- (void)updateMainText;
- (void)updateSecondaryText;

@end

Implementation

@implementation UIView (Localization)


- (NSString *)mainTextKey{

    return objc_getAssociatedObject(self, @selector(mainTextKey));

}

- (void)setMainTextKey:(NSString *)mainTextKey{

    objc_setAssociatedObject(self, @selector(mainTextKey), mainTextKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self updateMainText];


}

- (NSString *)secondaryTextKey{

    return objc_getAssociatedObject(self, @selector(secondaryTextKey));

}

- (void)setSecondaryTextKey:(NSString *)secondaryTextKey{

    objc_setAssociatedObject(self, @selector(secondaryTextKey), secondaryTextKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self updateSecondaryText];
}

- (void)updateMainText{

    //handle all cases one by one

    if([self isKindOfClass:[UILabel class]]){
        UILabel *label = (UILabel *)self;
        label.text= NSLocalizedString(self.mainTextKey, nil) ;
    }else if ([self isKindOfClass:[UIButton class]]){

        UIButton *btn = (UIButton *)self;
        [btn setTitle:NSLocalizedString(self.mainTextKey, nil)  forState:UIControlStateNormal];

    }


}
- (void)updateSecondaryText{
    //handle all cases one by one

}



@end

Basic Usage:

  1. Create a UIView Element Like UIButton or UIlable
  2. Specify your strings key inside UserDefinedAttributes against key "mainTextKey" or "secondaryTextKey".
  3. Run the app and it will load proper text from your strings file.

This is written without any proof reading , excuse for any stupid mistakes.

Upvotes: 0

Angel G. Olloqui
Angel G. Olloqui

Reputation: 8105

I agree with previous comments regarding the complexity of these solutions. Because of that, I have just created a new tool that automatically localizes your IB files. Check it out here: https://github.com/angelolloqui/AGi18n

Upvotes: 2

Rui Peres
Rui Peres

Reputation: 25917

Giani I think you were trying to find this:

http://developer.apple.com/internationalization/

(With more detail: http://developer.apple.com/library/ios/#documentation/MacOSX/Conceptual/BPInternational/BPInternational.html )

You should always start an app with that in mind. Because later, if your client has the need to add a new language you will have a ton of work. Even if this is app is just for you, you should use it. Besides learning how to do it, you are keeping your code flexible for a sudden change in your requirements.

Upvotes: 4

Related Questions