Override a base localizable.strings file

后端 未结 2 1851
庸人自扰
庸人自扰 2021-02-11 03:15

Is there a way to have one base localizable.strings file for multiple targets within a project, and also have a second localizable.string file for each target that will override

相关标签:
2条回答
  • 2021-02-11 03:56

    Here is the solution I found:

    NSString *PSILocalizedString(NSString *key, NSString *comment)
    {
        return NSLocalizedStringWithDefaultValue(key,
                                                 @"OverrideLocalizable",
                                                 [NSBundle mainBundle],
                                                 NSLocalizedString(key, nil),
                                                 comment);
    }
    

    What this will do is search a file called OverrideLocalizable.strings for the key. If the value for key is not found in OverrideLocalizable.strings, it will search localizable.strings for key. NSLocalizedString(key, nil) by default will search localizable.strings

    Pretty simple and elegant solution

    0 讨论(0)
  • 2021-02-11 04:07

    For Swift 4 users that come across this issue...

    func localizedString(
        for key: String, tableName: String = "OverrideLocalizable", 
        bundle: Bundle = .main, comment: String = ""
    ) -> String {
        let defaultValue = NSLocalizedString(key, comment: comment)
        return NSLocalizedString(
            key, tableName: tableName, bundle: bundle, 
            value: defaultValue, comment: comment
        )
    }
    
    0 讨论(0)
提交回复
热议问题