简体   繁体   中英

Adding general and specific localizable files for targets in Xcode

I have two targets in my project: target1 and target2. Each of them uses their localizable.strings resource for localization. But their localizable.strings files are same for 90%.

I want to realize such behavior:

  1. Create general localizable file with common strings

  2. Create localizable files for each target with their special strings that are different (ex app_name)

  3. Application should search the string in target's localizable.strings file at first and if this string is not exists, search it in common file

I tried to add localizable.string files for each target and then created common localizable.string file(with membership for target1 and target2), but application uses only common file :(

Does anybody know how to fix it?

Thank you for help!

Maybe you can create a pre-build script to concat the specific strings inside global strings.

Or an other solution, you can create a function to search first in global and after in a specific file (this is an example, not tested) :

- (NSString*) localizedString:(NSString*) key {
    //search in global file
    NSString *transcription = NSLocalizedString(key, "");
    if ([transcription isEqualToString:key]) {
        //search in specific file
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"specific_file" ofType:@"strings"];
        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
        return [dict objectForKey:key];
    }

    return transcription;
}

With thanks to lafalex, here is code for swift 2:

if let path = NSBundle.mainBundle().pathForResource("specific_file", ofType: "strings"), 
   let dict : NSDictionary = NSDictionary(contentsOfFile: path) {
   if let localizedString = dict.objectForKey(key) as? String {
       return localizedString
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM