iPhone localization for xib files

谁都会走 提交于 2019-12-22 05:04:54

问题


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...

Appreciate some thoughts..


回答1:


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.




回答2:


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!";



回答3:


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).




回答4:


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];


来源:https://stackoverflow.com/questions/3703662/iphone-localization-for-xib-files

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