I have a question regarding static libraries. I need to localize some text inside my library. So I created a bundle where I put my different localized files. Then, I created a function like this :
NSString *MyLocalizedString(NSString* key, NSString* comment)
{
static NSBundle* bundle = nil;
if (!bundle)
{
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyStaticLib.bundle"];
bundle = [[NSBundle bundleWithPath:path] retain];
}
return [bundle localizedStringForKey:key value:@"" table:nil];
}
But when I use it, it always return the english localized string (besides my phone langage is French). I do not know why.
I’ve got the very same issue when doing the exactly same: I’ve a static library and a companion bundle file with image, localized string, etc..
I’ve figured out that seems that the static can’t figure out the correct device localization (I’m sorry but I wasn’t able to find the reason of this issue) and I’ve fixed by doing this:
As you can see I have created a category of NSBundle with a Class method that act very similar to
[NSBundle mainBundle]and that return me the correct bundle for the static libray so I can use it everywhere I want, for example:The code is very simple first I find the path for the static library bundle, find the current device language and then I create a new NSBundle whose path is library_path/device_language.lproj .
A drawback of this approach is that you need to alway localize all of your asset and this can be a pain if you have a lot image in your bundle (but I think this is unlikely).
If you don’t want to adopt my category approach you can change your code like this: