Skip to content

[DX] Remove t() calls from tests unless specifically testing for translation-related tasks. #6473

Open
@klonos

Description

@klonos

t() should not be used in tests and assertions - if variables need to be added to strings, then format_string()/format_plural() should be used instead.

See https://www.drupal.org/project/drupal/issues/3133726 as well:

There is no need to use t() in tests, unless we're testing translations, however in core we do not follow this consistently, which does not set a good example for new contributions.

Also see these (specifically for avoiding t() calls inside classes and Form Builders and using $this->t() instead):

If you google drupal do not use t() in tests, a bunch of issues in d.org contrib project will come up.

I am not 100% sure, but I believe that this should also result in faster-running tests, which should be a DX+ for contributors.

PHPCS

We should also add PHPCS rules to prevent these from creeping back into the tests code.

Documentation

https://docs.backdropcms.org/api/backdrop/core%21includes%21bootstrap.inc/function/format_string/1 mentions the following:

In most cases, you should use t() rather than calling this function directly, since it will translate the text (on non-English-only sites) in addition to formatting it.

We should add to that ^^ that tests are an exception where format_string() should be used instead of t(). Perhaps there's other places that we need to update as well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions