本文介绍了在iOs中手动加载不同的本地化nib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款支持多语言的应用。
正如您所料,我不时会使用如下代码加载一些nib文件:

I'm working on an app with multi-language support. As you may expect, from time to time I load some nib files using a code like this:

 self.currentController = [[newClass alloc] initWithNibName:@"CustomController" bundle:nil];

该应用程序将从其languange文件夹加载相应的本地化xib版本。
现在,我想知道是否可以手动加载本地化的nib文件。
例如,不是简单地加载CustomController,而是加载英语 / 法语 / 德语 / 等。 CustomController的版本。

The app will then load the corresponding localized xib version from its languange folder.Now, I am wondering if it is possible to load the localized nib file manually.For example, instead of simply loading the CustomController, loading the english / french / german / etc. version of the CustomController.

有没有办法实现这个目标?

Is there a way I can achieve this?

谢谢你的支持提前帮助!

Thank you for your help in advance!

PS我知道这可能不是在iphone / ipad应用程序中更改语言的正确方法,但这不是我的决定

P.S. I know this may not be the proper way to change languages in an iphone/ipad app, but this is not my decision

[稍后编辑]
这看起来有点奇怪,就像一个黑客,但它似乎工作(加载德国笔尖):

[later edit]This looks a bit weird and like a hack, but it seems to work (loading the german nib):

NSString* path= [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"];  
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
self.currentController = [[newClass alloc] initWithNibName:@"CustomController" bundle:languageBundle];

我在这里找到了提示:

I found the tip here: http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html

虽然感觉不太完全正确,但我想知道是否还有其他解决方案。
初学者,我觉得这会给旧版本的iO带来麻烦,因为语言文件夹有不同的命名约定

It doesn't feel completely right though, I am wondering if there are other solutions too. For starters, I have the feeling this will cause trouble with older versions of iOs, since the language folder had a different naming convention

推荐答案

所以,就像我在编辑中说的那样,这就是我发现的解决方案:

So, just like I said in the edit, this is what I found as a solution:

NSString* path= [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"]; 

NSBundle* languageBundle = [NSBundle bundleWithPath:path];

self.currentController = [[newClass alloc] initWithNibName:@"CustomController" bundle:languageBundle];

如果您需要将文本加载到本地化标签

And if you need to load a text into a localized label

NSString* path= [[NSBundle mainBundle] pathForResource:[[[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"] objectAtIndex:0] ofType:@"lproj"];

NSBundle* languageBundle = [NSBundle bundleWithPath:path];

someLabel.text = [languageBundle localizedStringForKey:@"textKey" value:@"" table:nil];       

此处有更多信息:

对于它可能涉及的人,这种方法引发了许多问题。
对于初学者来说,可能会影响到每个人:你需要让本地化xib使用的每个资源都本地化。
如果我使用此方法加载新的本地化xib,并且该xib包含常规的非本地化图像,则在本地化之前它不会显示。
其他问题更加特殊,并且与您检索本地化数据的方式有关。

To whom it may concern, this method raises quite a number of problems.For starters, something that might affect everyone: you need to have every resource used by a localized xib also localized. If I load a new localized xib using this method, and that xib contains a regular non-localized image, it won't show up until it's localized.The other problems are more particular and are connected to the way you retrieve the localized data.

最后,我认为我不会使用这一点,因为对于当前的应用程序而言,它的问题太多了,但将来可能会变得很方便。

In the end, I don't think I will be using this, because for the current app it's way too problematic, but it might turn out handy in the future.

这篇关于在iOs中手动加载不同的本地化nib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 19:11