Is it possible to create an inheritance like structure for strings files in Xcode?

StackOverflow https://stackoverflow.com/questions/15796446

  •  01-04-2022
  •  | 
  •  

문제

I have an application that has different sets of strings based on the target. Is it possible to have one base strings file and then only override a few keys in another?

For example, if my base file is this:

"some-string" = "base-value"
"other-string" = "1234"

Then for one of my targets, associate another strings file that has the following:

"some-string" = "overridden-value"

So, if I run the target that contains the additional strings file, the output would be:

NSLocalizedString(@"some-string", nil) => "overridden value"
NSLocalizedString(@"other-string", nil) => "1234"

I would greatly prefer not to throw unmodified strings in the overriding strings file. Any help would be greatly appreciated.

도움이 되었습니까?

해결책

How about

NSLocalizedStringWithDefaultValue(@"some-string",
                                  @"additionalStringsTableName", 
                                  [NSBundle mainBundle],
                                  NSLocalizedString(@"some-string", nil),
                                  nil);

Perform a lookup in your overriding strings file. If that fails, return the default NSLocalizedString() result.

It's a rather ugly thing to have all over your code. So you might want to work some macro magic to get a shorter call. Something like this:

#define MyLocalizedString(key, comment) NSLocalizedStringWithDefaultValue(key,
                                  OVERRIDE_TABLE_NAME, 
                                  [NSBundle mainBundle],
                                  NSLocalizedString(key, comment),
                                  comment);

(written on multiple lines for clarity). Then you could define OVERRIDE_TABLE_NAME as a compiler option.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top