Interface Builder Localization HowTo

你离开我真会死。 提交于 2019-12-22 10:05:07

问题


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?


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/7766547/interface-builder-localization-howto

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