Interface Builder Localization HowTo

a 夏天 提交于 2019-12-05 22:23:21

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!