How can you switch localized Nibs within an app?

喜夏-厌秋 提交于 2019-12-24 15:53:31

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/17956709/how-can-you-switch-localized-nibs-within-an-app

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