做同样的事情时我遇到了同样的问题:我有一个静态库和一个带有图像、本地化字符串等的配套捆绑文件。
我发现静态似乎无法确定正确的设备本地化(很抱歉,我无法找到此问题的原因),并且我已通过执行以下操作进行了修复:
@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];
}