2

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 and append individual values to the base file?

EDIT

I want my app to have two .strings files - Localizable.string and Override.strings. If a string, Title.Welcome, is not found in OverrideLocalizable.strings then I would like the app to search Localizable.strings for Title.Welcome. Essentially, specifying Localizable as the fallback, but using OverrideLocalizable.strings by default.

Cezar
  • 55,636
  • 19
  • 86
  • 87
tentmaking
  • 2,076
  • 4
  • 30
  • 53

2 Answers2

9

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

tentmaking
  • 2,076
  • 4
  • 30
  • 53
  • the iOS is not able to find the key in OverrideLocalizable and always returns value from main Localizable file. Do we have to place the override file in some specific place ? – atastrophic Aug 04 '14 at 10:54
  • Make sure the Override file belongs to the target you are specifying. I have an Override.strings for each target within the app and one Localizable.strings for all the targets have access to. If the string is not found in the Override.strings, it then goes and gets it from the Localizable.strings. – tentmaking Aug 04 '14 at 14:27
3

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
    )
}
Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34
Bart Mommens
  • 145
  • 2
  • 13