CFlux
CFlux

Reputation: 345

Two targets and two Localizable.Strings (one base)

I have a simple problem and I can't figure out how to make it work.

Example: I have a button, a text, or something. On each app it should be different.

I tried keeping the first Localizable.strings for both targets and making the second Localizable.strings for the second target only. The problem is that only ONE Localizable.strings is loaded at a time. I wanna know how to make the TargetApp2 make both Localizable.strings complement each other.

Upvotes: 0

Views: 916

Answers (1)

James Webster
James Webster

Reputation: 32066

If I've understood your problem correctly, you'll need to give your Localizable.strings files different names and then use NSLocalizedStringFromTable, otherwise, as you've discovered, they'll conflict and only one is used at runtime.

e.g.

  • Leave your Target1's Localizable.strings as it is
  • Rename Target2's Localizable.strings to Extra.strings
  • Anywhere that uses a string from the second table, use NSLocalizedStringFromTable(key, @"Extra", comment)

I first encountered this problem when adding the Amazon AWS library which contains its own Localizable.strings which would arbitrarily be used instead of mine. However, because there are only a few strings in that file, it appeared as if localisation just wasn't working. That was a fun 2 days debugging!


Swift

The NSLocalizedStringFromTable doesn't exist with that exact name in Swift. Instead the table is an optional parameter in NSLocalizedString

func NSLocalizedString(
    key: String,
    tableName: String? = default,
    bundle: NSBundle = default,
    value: String = default,
    #comment: String) -> String

With Swift, you'll just need to change your NSLocalizedString(key: key, comment: "") to NSLocalizedString(key: key, table:"Extra" comment: "") for the affected strings.

Upvotes: 2

Related Questions