iphonic
iphonic

Reputation: 12719

How can you switch localized Nibs within an app?

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:

enter image description here

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

Answers (1)

iphonic
iphonic

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

Related Questions