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:
@implementation NSBundle (KiosKitAdditions)
+ (NSBundle *)kioskitBundle
{
static NSBundle* kioskitBundle = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
NSString *libraryBundlePath = [[NSBundle mainBundle] pathForResource:KKBundleName
ofType:@"bundle"];
NSBundle *libraryBundle = [[NSBundle bundleWithPath:libraryBundlePath] retain];
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *path = [libraryBundle pathForResource:langID ofType:@"lproj"];
kioskitBundle = [[NSBundle bundleWithPath:path] retain];
});
return kioskitBundle;
}
@end
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:
#define KKLocalizedString(key) NSLocalizedStringFromTableInBundle(key, @"Localizable", [NSBundle kioskitBundle], @"")
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:
NSString *MyLocalizedString(NSString* key, NSString* comment)
{
static NSBundle* bundle = nil;
if (!bundle)
{
NSString *libraryBundlePath = [[NSBundle mainBundle] pathForResource:@"MyStaticLib"
ofType:@"bundle"];
NSBundle *libraryBundle = [NSBundle bundleWithPath:libraryBundlePath];
NSString *langID = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *path = [libraryBundle pathForResource:langID ofType:@"lproj"];
bundle = [[NSBundle bundleWithPath:path] retain];
}
return [bundle localizedStringForKey:key value:@"" table:nil];
}
I had to modify Luca's answer to get it working how I wanted. My problem was very similar: Using my bundled localized strings from the consuming application didn't resolve to correct [lang].lproj folder. In my case, I had an es.lproj folder I wanted to look up from. However, if the user sets their preferred langauge to es-MX, using [NSLocale preferredLanguages] objectAtIndex:0] will try to find the es-MS.lproj folder, which doesn't exist.
@implementation NSBundle (CCPAdditions)
+ (NSBundle *)CCPBundle
{
static NSBundle* corePaymentBundle = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
NSString *libraryBundlePath = [[NSBundle mainBundle] pathForResource:@"ClipCorePayments"
ofType:@"bundle"];
NSBundle *libraryBundle = [NSBundle bundleWithPath:libraryBundlePath];
for (NSString *langID in [NSLocale preferredLanguages])
{
NSString *path = [libraryBundle pathForResource:langID ofType:@"lproj"];
if (path)
{
corePaymentBundle = [NSBundle bundleWithPath:path];
break;
}
}
});
return corePaymentBundle;
}
@end
This iterates through the preferred language list, and checks to see if that path exists. If es-MX.lproj is missing, it will next check es.lproj, etc. and eventually finds en.lproj if nothing else is there.