在 iO 中手动加载不同的本地化笔尖 [英] Manually loading a different localized nib in iOs
问题描述
我正在开发一款支持多语言的应用.正如您所料,我有时会使用如下代码加载一些 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];
然后应用程序将从其语言文件夹中加载相应的本地化 xib 版本.现在,我想知道是否可以手动加载本地化的 nib 文件.例如,不是简单地加载 CustomController,而是加载 english/french/german/etc. 版本的自定义控制器.
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?
提前感谢您的帮助!
附:我知道这可能不是在 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
[稍后编辑]这看起来有点奇怪,像一个 hack,但它似乎工作(加载德国笔尖):
[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];
我在这里找到了提示: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];
更多信息:http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html
对谁来说,这种方法会引发很多问题.对于初学者来说,可能会影响到每个人:您需要将本地化 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.
这篇关于在 iO 中手动加载不同的本地化笔尖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!