1

Our iOS app is translated into 19 languages, and recently I found an issue where two buttons on a screen had the same title - which mean that users couldn't tell which to press!

I want to write a unit test that will check two NSLocalizedString keys in a particular locale, and make sure they don't collide.

How can I lookup the value of an NSLocalizedString in an arbitrary locale?

(For example, let's say I want to find the translated versions of "Delete" and "Remove", or something like that, and make sure those two translated strings aren't the same.)

(I'm guessing that the answer is going to involve something with Bundle?)

Here's the pseudocode of what I'm looking for:

class TranslationTest: XCTest {
  func testNoTranslationCollision() {
    let allLocales = // list of all supported locales in the app
    allLocales.forEach { locale in
      let translatedStringA = "Remove".asTranslatedIn(locale)
      let translatedStringB = "Delete".asTranslatedIn(locale)
      XCTAssertNotEqual(
        translatedStringA, 
        translatedStringB, 
        "The translations for these different strings shouldn't be identical!"
      )
    }
  }
}
bryanjclark
  • 6,247
  • 2
  • 35
  • 68

1 Answers1

0

Now in Xcode 11 you can define different configurations for each test with Test Plans. So define different configurations that each one launch the app with specific language. If specific language like persian not shown in Application Language, add its language code in Arguments Passed On Launch like this:

-AppleLanguages (en)

In Your case first you need to create an array of your supported language codes and traverse through array and set language and compare your strings

let stringA = NSLocalizedString("Remove",comment: "stringA")
let stringB = NSLocalizedString("Delete",comment: "stringB")
XCTAssertNotEqual(
    stringA, 
    stringB, 
    "The translations for these different strings shouldn't be identical!"
  )
T.Ratzz
  • 71
  • 8