我正在尝试测试分层的本地化结构,该结构首先在一个文件中搜索Localization,然后如果在第一个表中找不到它,则默认为Localizable.strings。
这是执行此操作的代码:
+ (instancetype)sharedInstance {
static LLLocalizationUtil *sharedLocalizationUtil = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedLocalizationUtil = [[self alloc] init];
});
return sharedLocalizationUtil;
}
- (LLLocalizationUtil *)init {
self = [super init];
if (self) {
_bundle = [NSBundle bundleForClass:[self class]];
}
return self;
}
- (NSString *)locateString:(NSString *)key {
NSString *locString =
[self.bundle
localizedStringForKey:key
value:[self.bundle localizedStringForKey:key
value:nil
table:@"Localizable"]
table:@"Localizable_Specific"];
return locString;
}
这是试图测试它的单元测试:
SPEC_BEGIN(LocalizationUtil)
describe(@"We want a tool to support hierarchical localization", ^{
__block NSFileManager *manager;
__block LLLocalizationUtil *localUtil;
beforeAll(^{
localUtil = [LLLocalizationUtil sharedInstance];
localUtil.bundle = [NSBundle bundleForClass:NSClassFromString(@"LLLocalizationUtilTests")];
});
context(@"with multiple localization files", ^{
it(@"and one carrier", ^{
NSString *attString = [localUtil locateString:@"button.text"];
[[attString should] equal:@"Click me!"];
});
});
});
SPEC_END
我尝试了几种不同的设置Bundle路径的方法,但是似乎都没有用。显然,这也是错误的。
我在Xcode项目中有Localizable.strings文件,据我所知,本地化是正常的:
Localizable.strings:
"some.key" = "some_value";
"button.title" = "wrong";
Localizable_Specific.strings:
"button.title" = "Click me!";
"activity.title" = "Activity Window";
问题似乎是该捆绑包找不到Localizable_Specific文件来执行查找,并且它似乎也忽略了Localizable.strings。测试将返回原始键值,并且都不会“点击我!”也不“错”。
最佳答案
通过对最愚蠢的事情的仔细分析解决了:当我想要“ button.title”时,我正在寻找“ button.text”
叹。
关于ios - Xcode单元测试找不到Localizable.strings,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23530652/