kelkarm
kelkarm

Reputation: 359

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

Appreciate some thoughts..

Upvotes: 8

Views: 11676

Answers (4)

Jidong Chen
Jidong Chen

Reputation: 450

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

Upvotes: 0

WiseGuy
WiseGuy

Reputation: 429

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

Upvotes: 4

Clafou
Clafou

Reputation: 15400

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.

Upvotes: 7

csexton
csexton

Reputation: 24783

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

Upvotes: 2

Related Questions