iPhone localization for xib files
I am just getting familiar with the localization of xib files and was wondering if there's a way of localizing the strings within the xib by refering to the plists directly...
App开发者_运维问答reciate some thoughts..
If you don't want to localize the .xib files directly, you can extract the text they contain into .strings files and, once the .strings files are translated, inject them back into your .xib file to produce localized versions. These extract/inject operations are done using the ibtool
utility.
I found detailed instructions on how to do this on this website.
My method stems a bit off @Clafou answer, however it may be a bit more straightforward. I simply set IBOutlets for my button and label strings (created on xib) within my .h controller.
@interface DetailViewController : UIViewController {
IBOutlet UILabel *TitleLabelMain;
}
Then went to my .m controller and gave these labels and buttons a value with NSLocalizableStrings.
- (void)viewWillAppear:(BOOL)animated {
TitleLabelMain.text = NSLocalizedString(@"titleLabel",nil);
And of course you will need a value defined in Localizable.strings
"titleLabel" = "THIS TEXT IS LOCALIZED!";
The Apple suggested way to do this is to exprot the strings into .strings
files in .lproj
bundles, that will be switched out by Cocoa's localization framework.
Xcode can generate the .strings
files from the xib, which make localization pretty straight forward.
Right click on the xib file in Xcode, and choose Get Info
. Select the General
tab and on the bottom click Make File Localizable
. Then you will be able to add localizations by clicking Add Localization
on that same tab.
I would recommend this tutorial for step-by-step information (and pretty pictures).
I think localize xibs directly is not an good option. I'm using https://github.com/steipete/Aspects to hook the awakeFromNib method of UILabel and localize text there. Example:
#define Localized(_v_) NSLocalizedString(_v_, nil)
NSError *err = nil;
[UILabel aspect_hookSelector:@selector(awakeFromNib)
withOptions:AspectPositionAfter
usingBlock:^(id<AspectInfo> aspectInfo) {
UILabel *label = aspectInfo.instance;
NSString *lStr = Localized(label.text);
if (lStr) {
label.text = lStr;
}
} error:&err];
精彩评论