在 iO 中手动加载不同的本地化笔尖

发布于 2024-12-25 00:18:54 字数 1083 浏览 3 评论 0原文

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

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

然后,应用程序将从其 languange 文件夹中加载相应的本地化 xib 版本。 现在,我想知道是否可以手动加载本地化的 nib 文件。 例如,不是简单地加载 CustomController,而是加载 english / french / german / etc. 版本自定义控制器。

有什么方法可以实现这个目标吗?

提前感谢您的帮助!

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

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

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 带来麻烦,因为语言文件夹有不同的命名约定

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

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!

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花期渐远 2025-01-01 00:18:54

因此,就像我在编辑中所说的那样,这就是我找到的解决方案:

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

NSBundle* languageBundle = [NSBundle bundleWithPath:path];

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

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

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 包含常规非本地化图像,则在本地化之前它不会显示。
其他问题更加具体,并且与检索本地化数据的方式有关。

最后,我认为我不会使用它,因为对于当前的应用程序来说,它的问题太大,但将来可能会很方便。

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

More info here: http://learning-ios.blogspot.com/2011/04/advance-localization-in-ios-apps.html

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文