Overriding preferred strings localization on the fly for testing

前端 未结 3 922
遥遥无期
遥遥无期 2021-02-06 16:21

Using the Settings app in the iPhone simulator to switch languages is a PITA way of testing localization tweaks. I\'m trying to figure out a way to switch localizations (en/fr/e

3条回答
  •  有刺的猬
    2021-02-06 16:45

    The only way to do this that I've figured out is by using the subtle trick suggested by this answer. You can wrap NSLocalizedString() in a function that knows about a localization "override", and chooses how to get its strings based on whether that is set. When you want to override, you can create a "sub bundle" from the localization's directory, and then pull string from that bundle. Here's the gist of it:

    if (CurrentLocalization != nil) {
        NSBundle * locBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:CurrentLocalization ofType:@"lproj"]];
        return [locBundle localizedStringForKey:key value:nil table:nil];
    } else {
        return NSLocalizedString(key, @"");
    }    
    

    etc.

提交回复
热议问题