t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead

Calls to t( ) should be replaced with $this->t( ) in classes. Automated tests are always run in English for the UI to save processing resources, hence those do not need any translating, just simply remove the call to t() completely. See https://www.drupal.org/docs/8/phpunit/phpunit-browser-test-tutorial#t

Comments

brahmjeet789 created an issue. See original summary.

brahmjeet789’s picture

StatusFileSize
new16.43 KB

u have added patch to remove t() to $this->t() used in Drupal best practices, please test it.

brahmjeet789’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: remove-t-2968843-1.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

akshay kashyap’s picture

StatusFileSize
new3.41 KB

I have created the patch for this. Please review the patch.

akshay kashyap’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 5: remote-t-2968843-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

prashant.c’s picture

@Akshay kashyap and @brahmjeet789

$this->t only works within the Classes and in Class Objects. So in .install and .module files you won't be able to change t() with $this->t() it will not work.

Please re-work on the patch and replace the instance only in case of class and objects if any.

brahmjeet789’s picture

Status: Needs work » Needs review
StatusFileSize
new0 bytes

@prashant as suggested by your comment i added new patch ,please review.

Status: Needs review » Needs work

The last submitted patch, 9: remote-2968843-9.patch, failed testing. View results

vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new6.71 KB

@Prashant.c Adding a patch which will add $this->t() in controller with classes only.

moshe weitzman’s picture

Status: Needs review » Needs work

Needs reroll

jonathan1055’s picture

Parent issue: » #3081785: [meta] Devel 8.x coding standards messages
ravi.shankar’s picture

Status: Needs work » Closed (outdated)

This issue has been fixed in Devel version 8.x-3.x

jonathan1055’s picture

jonathan1055’s picture

Status: Closed (outdated) » Needs work

Ah! Only some of the t( ) calls were replaced in that issue, the ones affected by drupal_set_message(). There are others which need replacing, as you can see in the patch #11

Take a look at the current full list of coding standards faults as loaded on #3081785: [meta] Devel 8.x coding standards messages and you can see some, for exemple in src/Form/SettingsForm.php and src/Form/ConfigsList.php, both of which were included in the patch #11

ravi.shankar’s picture

Version: 8.x-1.1 » 8.x-3.x-dev
Status: Needs work » Needs review
StatusFileSize
new3.48 KB

I have fixed this.

jonathan1055’s picture

Status: Needs review » Needs work

Looks good, thanks for working on this. But you only fixed it for the two example files I named. You need to search the whole of the current full list of coding standards faults and find all the files which have this problem.

ravi.shankar’s picture

Status: Needs work » Needs review

I checked all the files, I didn't find any other file which has this problem.

jonathan1055’s picture

Title: Remove t() to $this->t() used in Drupal best practices » t() calls should be avoided in classes
Status: Needs review » Needs work

If you search for t() calls in that text file and you will find 57 matches in 11 files, e.g.

FILE: /home/travis/build/jonathan1055/drupal/modules/devel/devel_generate/src/DevelGeneratePermissions.php
----------------------------------------------------------------
FOUND 12 ERRORS AND 2 WARNINGS AFFECTING 9 LINES
----------------------------------------------------------------
...
 50 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and
    |         |     $this->t() instead
...

and

FILE: /home/travis/build/jonathan1055/drupal/modules/devel/devel_generate/tests/src/Functional/DevelGenerateBrowserTest.php
----------------------------------------------------------------
FOUND 16 ERRORS AND 26 WARNINGS AFFECTING 38 LINES
----------------------------------------------------------------
...
  45 | WARNING | [ ] t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and
     |         |     $this->t() instead
...

and in 7 more files.

However, phpunit browser tests are always run in English for the UI to save processing resources, hence we do not need to translate the text on the buttons, just simply remove the call to t() completely for those. See https://www.drupal.org/docs/8/phpunit/phpunit-browser-test-tutorial#t

You can add the fixes to all these 11 files in this patch if you want to, as they all have the same coding standards message. Or you could just add the extra fix for DevelGeneratePermissions.php here, and raise a separate issue to remove the t() calls in those 8 test files.

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new18.46 KB

I have removed all.

Status: Needs review » Needs work

The last submitted patch, 21: 2968843-21.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

gg24’s picture

Assigned: Unassigned » gg24

@ravi, I see few tests are failing.

Thanks!

gg24’s picture

Assigned: gg24 » Unassigned
staalex’s picture

StatusFileSize
new17.64 KB

Updated t calls, removed t calls from tests, added StringTranslationTrait to DevelGeneratePermissions

staalex’s picture

Status: Needs work » Needs review

Patch applied cleanly.

jonathan1055’s picture

Issue summary: View changes
Status: Needs review » Needs work

Hi alex.stanciu,
Looks good in general, a major improvement on #21. Thanks for adding the StringTranslationTrait to DevelGeneratePermissions. You also fixed a previously missed call to t() in src/Form/SettingsForm.php

I've done a comparison between patch #21 and patch #25 and spotted a few things:

  1. In tests/src/Functional/DevelDumperTest.php line 92 you missed one call to t()
  2. In tests/src/Functional/DevelMenuLinksTest.php testRedirectDestinationLinks there are 9 calls to t() which have not been removed in your patch
  3. tests/src/Functional/DevelModulesReinstallTest.php line 67, the t() call should be removed, and use sprintf() for the placeholder substitutions

Nice work though. With the above fixes this should be ready.
Jonathan

staalex’s picture

StatusFileSize
new19.49 KB

Hi jonathan1055.
You're absolutely right, my IDE played some tricks on me and I missed those occurences.
I uploaded a new patch, hopefully it'll pass.

staalex’s picture

Status: Needs work » Needs review
jonathan1055’s picture

Status: Needs review » Reviewed & tested by the community

Thanks. Looks good, you have fixed the 3 points in #27

moshe weitzman’s picture

LGTM as well.

jonathan1055’s picture

OK I will see if I can push this change.

jonathan1055’s picture

Status: Reviewed & tested by the community » Fixed

OK, so the commit worked, my first as Devel maintainer :-) Thanks, everyone who contributed a patch.

However the coding standards report for the branch https://www.drupal.org/pift-ci-job/1465968 says we only reduced the number of warnings by 4. The t() calls should be avoided in classes message was not being generated before, which means the default phpcs configuration is not checking for it. We therefore definitely do need our own phpcs.xml.dist file so that we can specify Drupal and DrupalPractice standards. I will follow that up on #3081785: [meta] Devel 8.x coding standards messages

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.