Reputation: 12719
My requirement is to switch localized XIBs from within the app. Usually when you localize a XIB and change the language from Settings, the specific language localized XIB gets loaded automatically.
I have XIBs like in the below image:
I want to load the XIB when the specific language is selected on a button tap within an application. Source code for such an application can be found here.
How can I do this?
Upvotes: 1
Views: 287
Reputation: 12719
I solved the problem by creating a simple method see below
-(void)changeControllersForLanguage:(NSString *)language{
NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"]; //Detecting specific language lproj folder
if (path) {
//Creating Local Bundle from the language specific bundle path
NSBundle *localeBundle = [NSBundle bundleWithPath:path];
NSArray *controllers=self.tabBarController.viewControllers;
for(UIViewController *view in controllers){
[localeBundle loadNibNamed:NSStringFromClass([view class]) owner:view options:nil];
}
}
}
Then calling the function as below on button tap
- (IBAction)toggleLanguage:(id)sender {
UIButton *btn=(UIButton *)sender;
switch (btn.tag) {
case 1://French
[self changeControllersForLanguage:@"fr"];
break;
case 2://English
[self changeControllersForLanguage:@"en"];
break;
default:
break;
}
}
It really worked.. :)
Upvotes: 1