I've been localizing my programs by creating strings file and/or localized xib files for every language. This process is very slow since it's complex to maintain all the localized xib files and add new features to the interface.
To avoid this, in my latest projects, I simply made IBOutlets for every UI element on the interface and at -(void)awakeFromNib
changed the titles and string values for all the elements using NSLocalizedString
and a strings file.
Is there any other more efficient way to bulk translate all the UI without the need of creating multiple xibs and/or wasting time trying to "keep them in sync"?
I had one idea to solve this, I made a Category of NSObject like the following:
@implementation NSObject (Localizable)
-(void)awakeFromNib {
if ([self isKindOfClass:[NSTextField class]]) {
NSTextField *txtField = (NSTextField *)self;
txtField.stringValue = NSLocalizedString(txtField.stringValue, nil);
}
else if ([self isKindOfClass:[NSWindow class]]) {
NSWindow *windowC = (NSWindow *)self;
windowC.title = NSLocalizedString(windowC.title, nil);
}
}
@end
As you can see on my category I can easily change the values for all the NSTextField
objects on my application by keeping the translation placeholder as the NSTextField
text...
This works fine, however I'm concerned about performance since this will run for all objects in my application.
Is it to bad to do this? Are there any better alternatives?
PS: I made a category of NSObject
and not NSTextField
or NSWindow
because this way I can keep all the localization logic in one place and simply add another else if
statement for every UI class I might want to translate instead of creating a new Category.