Search code examples
iosobjective-cuistoryboardxcode-storyboardlocalizable.strings

StoryBoard Localization with IBInspectable setter in Objective C Extensions


As per this blog post you can localise a story board with IBInspectable extensions in swift. we can rely Localizable.strings file alone instead of having localised story boards and worrying about multiple files for strings.

So my question is how to achieve the same with objective c? If the answer is not possible what is the best approach for keeping all the UI facing strings in one single file and fetching it.


Solution

  • Are you aware you can use those extensions from Objective-C the same as you would in Swift, right? Anyway, to do the same in Objective-C you just create categories with the IBInspectable attribute.

    @interface UILabel(LocalizationUtils)
    @property (nonatomic, weak) IBInspectable NSString *localizedText;
    @end
    
    @implementation UILabel(LocalizationUtils)
    
    - (void)setLocalizedText:(NSString*)localizedText {
        self.text = NSLocalizedString(localizedText, @"");
    }
    
    - (NSString*)localizedText {
        return @"";
    }
    
    @end