0
votes

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

  • I have two targets:

    1. TargetApp1
    2. TargetApp2
  • I have two Localizable.strings:

    1. For translation for a second language.
    2. For different names (on both languages), only for TargetApp2.

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

  • TargetApp1: en: "Store" - lg: "STORE_"
  • TargetApp2: en: "Dealer" - lg: "DEALER_"

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.

1

1 Answers

2
votes

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.