Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

objective c - Manually loading a different localized nib in iOs

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...