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.
Use ibtool
to manage your xib files. It's part of the command-line tools package one installs along with Xcode. While Xcode offers a GUI for editing the strings, ibtool
can be run from scripts.
Overall, Apple has some guidelines and advice for maintaining xib files; you can find their documentation here. Reading it, you'll find that ibtool
is fairly easy to use.
As impressive as the approach you've taken is (I've seen some truly awful ones in which every UI element had been connected to an IBOutlet just to effect localization!), it still winds up being additional glue code that has to be maintained (and believe me, you will wind up maintaining it as your needs change).
So as far as my two currency units go, I'd recommend sticking to localized nibs.