在本地化故事板时从正确的捆绑中加载图像 [英] loading images from the correct bundle when localizing storyboards

查看:123
本文介绍了在本地化故事板时从正确的捆绑中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为用户添加一个选项,以便在阿拉伯语和阿拉伯语之间切换应用程序内部的英语语言(无需重新设置整个iPhone的语言),我设法在AppDelegate.m文件中使用此方法正确执行:

I'm trying to add an option for the user to switch between Arabic & English language from inside the app (without having to re-set the language of the whole iPhone), I managed to do it correctly by using this method in the AppDelegate.m file:

 -(void)switchTolanguage:(NSString *)lang{

    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObject:lang] forKey:@"AppleLanguages"];
     NSBundle *bnd = [NSBundle bundleWithPath:[[NSBundle mainBundle]pathForResource:lang ofType:@"lproj" ]];
     UIStoryboard *storyBoard;
     storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:bnd];
     UIViewController *initViewController = [storyBoard instantiateInitialViewController];
     self.window.rootViewController = initViewController;
}

我放了两个 UIButtons 在应用程序中的一个视图控制器上测试方法并且运行良好:在新加载的(本地化)故事板中出现所有 UILabels ,字符串等...使用正确的选定语言,但一个例外:新加载的故事板不会(加载)其本地化图像,它会拍摄旧故事板(旧包)的图像,即当前运行的应用程序在英语包上,用户点击了将语言切换为阿拉伯语的按钮,该应用程序将显示正确的阿拉伯语控件和字符串,但有英文图像,切换回英语一切都会好(英文字符串,标签和图像.. 。)

I placed two UIButtons on one of the view controllers in the app to test the method and it worked well: all UILabels, strings, etc ... in the new loaded (localized) storyboard appear with the correct selected language but with one exception: the new loaded storyboard does not take (load) its localized images, it takes the images for the old storyboard (old bundle), i.e. if the app currently runs on English bundle and the user tapped on the button that switches the language to Arabic, the app will appear with the correct Arabic controls and strings but with English images, switch back to english everything will be OK (english strings, labels and images ...)

这是故事板本地化的方式:

here's how my storyboard is localized:

以及每个图像的本地化方式:

and here's how each image is localized:

如何加载正确的图像包用户切换应用程序语言?

How can I load the correct bundle images when the user switches app language ?

更新:

这是一个示例项目的链接,我的意思是,运行示例,你会注意到当你从应用程序内部更改语言时,你会请注意图像不可更改...
您还会注意到本地化的故事板没有在其设计编辑器中加载正确的图像......

UPDATE:
Here's a link to a sample project for what I mean, run the sample and you will notice when you change the language from inside the app, you will notice that the image is not changeable ... and you will also notice that the localized storyboard does not load the right image in its design editor ...

PS 不会为我使用此类场景切换语言的原因添加注释,因为这是客户要求。

P.S. do not add comments for why I use such scenario to switch language, because it is a customer requirement.

推荐答案

我有同样的问题:( ..和什么对我有用(对于这种场景切换语言)..

I had same issue :(.. and what worked for me (for such scenarios to switch language) ..

你有2种可能性..

1)如果你想改变故事板(hot cha nge),您必须修改每个故事板中的所有Outlets。在故事板中选择所需的图像(具有不同的名称),标签等。对我来说,使用本地化图像(相同名称) - 仅在重新启动应用程序后才能工作..
For同样的img_name,我'尝试...从特定的lproj文件夹(languageBundle)调用特定的图像..但它在AppDelegate改变了故事板时没有用。

1) If you want to change the storyboard (hot change), you must modify all your Outlets in the each Storyboard. Choose in your storyboard the desired image (with different names), labels, etc.. For me, with localized images (same name) - only worked after restart of the application.. For the same img_name, I' tried ... to "call" the specific image.. from the specific lproj folder (languageBundle).. but it did not work at changed the storyboard from AppDelegate..

2)你可以只有一个故事板......并且只改变标签,图像等...(根据Localizable.strings),这样的东西可以解决这个问题... https://stackoverflow.com/a/12149553/1702413
此外,您必须拥有不同的图像文件名(用于热变) )

2) You can have only one storyboard... and change only labels, image, etc... (according to Localizable.strings), something linke this... https://stackoverflow.com/a/12149553/1702413 Also, you must have different file name for images (for "hot changing")

我不知道是否有bug ......或者是什么......

I don't know if a bug... or what is...

UPD

你有一个小项目 https://dl.dropbox.com/u/19438780/test5%20copy2.zip 同时使用..

You have a little project https://dl.dropbox.com/u/19438780/test5%20copy2.zip with both..

如果要更改故事板(与您一样),则在重新启动应用程序之前,您将拥有languageBundle的相同路径。所以..你必须在故事板中进行更改..

If you are changing the storyboard (like you) you will have the same path for languageBundle until you restart the application. So.. you must do the changing right in your storyboard..

对于第二个..你可以从特定的languageBundle(lproj文件夹)更改/召回出口.. 。

For the second.. you can change/recall outlets .. from specific languageBundle (lproj folders)..

对于1)..我发现一个技巧来覆盖languageBundle直到重启:

For 1) .. I found a "trick" to overwrite the languageBundle until restart:

-(id) initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        NSArray* languages = [[NSUserDefaults standardUserDefaults]      objectForKey:@"AppleLanguages"];
        Lang2 = [languages objectAtIndex:0]; //NSString
    }
    return self;
}

并初始化奥特莱斯:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

      NSString *path;
   if([Lang2 isEqualToString: @"ro"])
    {
        path = [[NSBundle mainBundle] pathForResource:@"ro" ofType:@"lproj"];
        NSLog(@"enc ro");
   }
    else{
        path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
        NSLog(@"enc en");
    }

    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    //find and return the desired string
    self.label.text=[languageBundle localizedStringForKey:@"kLabelText" value:@"" table:nil];
    self.img.image = [UIImage imageNamed:[languageBundle localizedStringForKey:@"myImage" value:@"" table:nil]];
}

这篇关于在本地化故事板时从正确的捆绑中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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