Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 967
BBog Avatar asked Jan 03 '12 12:01

BBog


1 Answers

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.

like image 157
BBog Avatar answered Oct 21 '22 03:10

BBog