Question

I'm trying to test a hierarchical localization structure that first searches for a Localization in one file, and then defaults to Localizable.strings if it can't find it in the first table.

Here's the code doing that:

+ (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;

}

And here's the unit test trying to test it:

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

I've tried a few different ways of setting the Bundle path, but none seem to work; obviously this one is also wrong.

I have the Localizable.strings files in the Xcode project, Localized as normal as best I can tell:

Localizable.strings:

"some.key" = "some_value";
"button.title" = "wrong";

Localizable_Specific.strings:

"button.title" = "Click me!";
"activity.title" = "Activity Window";

The issue seems to be that the Bundle cannot find the Localizable_Specific file to do the lookup, and it also seems to ignore Localizable.strings. Tests come back with the original key value, and neither "Click me!" nor "wrong".

Était-ce utile?

La solution

Solved via careful analysis of the stupidest thing: I was looking for 'button.text' when i wanted 'button.title'

Sigh.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top