如何在应用内切换本地化的 Nib? [英] How can you switch localized Nibs within an app?

查看:30
本文介绍了如何在应用内切换本地化的 Nib?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是从应用程序内切换本地化的 XIB.通常,当您本地化 XIB 并从设置"更改语言时,特定语言的本地化 XIB 会自动加载.

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.

我有如下图所示的 XIB:

I have XIBs like in the below image:

我想在应用程序中的按钮点击上选择特定语言时加载 XIB.可以在此处找到此类应用程序的源代码.

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.

我该怎么做?

推荐答案

我通过创建一个简单的方法解决了这个问题,见下文

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

它真的有效.. :)

这篇关于如何在应用内切换本地化的 Nib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆