1

I have a Unit test where I need to print a string so I decide to use the t() function to translate it:

$this->assertTrue(is_numeric($expected), t('Parameter is numeric.'));

But the problem is that in the Unit test the t() function is not available. So I decide to check a similar Unit test in core (SortArrayTest) and the test don't translate the string:

$this->assertTrue(is_numeric($expected) && is_numeric($result), 'Parameters are numeric.');

I know that at this moment Drupal don't have integration with the Unit test from the UI, but this can be possible in future, so:

Should I translate string inside a Unit test? If the answer is Yes, How I can do it?

3

No, you should not. An assertion message is basically on the same level as a code comment, you're not translating that either.

We used to do that in core, also because it was all Simpletest then and the messages were shown in the UI, so an argument could be made that should be translatable. But we did massive patches to get rid of that, it just adds more complexity without any additional benefit.

  • Thanks. So in all the tests (functionnal, kernel or unit) is better don't use the t() function? – Adrian Cid Almaguer Mar 27 '18 at 0:43
  • 1
    In my opinion yes. I've also stopped using t() for things like button labels. Even if you'd test a multilingual site that really has a translation there could still be a problem then with mismatching languages or so, so in 99.9% of the time, it's just a waste of characters/function calls. – Berdir Mar 27 '18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.