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 .
このアプローチの欠点は、すべてのアセットを常にローカライズする必要があることです。バンドルに多くの画像がある場合、これは面倒な場合があります (しかし、これはありそうもないと思います)。
私のカテゴリ アプローチを採用したくない場合は、次のようにコードを変更できます。
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];
}