我试图测试首先在一个文件中搜索本地化的分层本地化结构,然后默认为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";
问题似乎是Bundle找不到Localizable_Specific文件来进行查找,它似乎也忽略了Localizable.strings。测试以原始键值返回,并且都没有"点击我!"也不是"错误"。
答案 0 :(得分:1)
通过仔细分析最愚蠢的事情来解决:当我想要'button.title'时我正在寻找'button.text'
叹息。