Problem/Motivation

This is just a continuation of

https://www.drupal.org/node/2540886#comment-10639286

I am creating a new issue to maintain traceability....

It is better to have only one commit per issue

The initial patch is a renamed copy of

replace_t_with_this_t-2540886-10.patch

Just a pre commit node :

krishnan.n deserves credit see

Steps to reproduce

grep -rl "use[ ]*StringTranslationTrait" * |grep -v "^modules" |xargs grep -n "\<t\>(" | grep -v "\* *" | grep -v "this->t"

Proposed resolution

Use $this->t where possible in files using StringTranslationTrait
See #42 for latest results of the grep command

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-2629508

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

martin107 created an issue. See original summary.

martin107’s picture

Project: Simple Google Maps » Drupal core
Version: 8.x-1.x-dev » 8.1.x-dev
Component: Code » configuration system

@krishnan.n

This issue is mis-labelled. It is not a simple gmap issue ... it is a core thing.

krishnan.n’s picture

StatusFileSize
new39.48 KB

@martin107, should we brute-force change *all* t()'s to $this->t() in core?

Adding another patch again only for quick review. Let me know if these changes seem good and will then script it.

martin107’s picture

should we brute-force change *all* t()'s

I don't think that is practical ...

There are a couple of issues which make a global replace difficult

1) in *.module files or other procedural code t() is still the only way forward.
2) The class in which the change is made must have use the stringTranslationTrait. ( or one of its base classes).
3) It is a tough ask to review thousands of changes at a time

... so t() will still be in Drupal15 ... I hope not :)

dawehner’s picture

In general to make such conversions feasible ensure that you follow the following rule: Do just one kind of change at the same time, so like have an issue which converts all t() usages in classes that already have the StringTranslationTrait. With that you have an easier time as you can make less mistakes and reviewing is much easier as the changes are all in one logical changeset.

krishnan.n’s picture

StatusFileSize
new720 bytes

Following dawehner's suggestion which seems sensible, grep'd thru the core and found only one instance where t() is used without $this->t(), other than in ./core/modules, which we should ignore? In any case, there is one instance in node module and a couple in tests.

An uglyish grep added just for future reference/(ab)use: search for files containing "use StringTranslationTrait", ignore modules dir, and then search for t() that is not in comments, return() or has this->t()

grep -rl "use[ ]*StringTranslationTrait" * |grep -v "^modules" |xargs grep -n "\<t\>(" | grep -v "\* *" | grep -v "this->t"

Attaching patch -- this should be safe to commit.

krishnan.n’s picture

Assigned: Unassigned » krishnan.n
krishnan.n’s picture

@martinFrances:
Think making t() this-t() in only core is a good starting point. There was only one occurrence in the patch, but now few more; 'still manageable. Note: 'git blame' showed the current changes were all made by alexpott, so may want to discuss if this is valid/important patch

neha.gangwar’s picture

Status: Active » Needs review
StatusFileSize
new54.51 KB

Made the suggested changes in Drupal/core/modules/config module.
If need to made changes in any other modules. Please let me know.

Status: Needs review » Needs work

The last submitted patch, 9: replace_t_with_this_262908-9.patch, failed testing.

neha.gangwar’s picture

Status: Needs work » Needs review
StatusFileSize
new54.92 KB

Status: Needs review » Needs work

The last submitted patch, 11: replace_t_with_this_262908-11.patch, failed testing.

The last submitted patch, 11: replace_t_with_this_262908-11.patch, failed testing.

heykarthikwithu’s picture

Assigned: krishnan.n » Unassigned
Status: Needs work » Needs review
StatusFileSize
new2.8 KB
akishankar’s picture

Assigned: Unassigned » akishankar
aditya_anurag’s picture

Issue tags: +drupalconasia2016
Bandana.kaur’s picture

I have reviewed the patch and it looks fine.

akishankar’s picture

StatusFileSize
new5.37 KB

While reviewing the latest pull as of now, I figured out a couple of code snippets which need to updated with 't() to $this->t()' change.

Please review the patch.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

martin107’s picture

Assigned: akishankar » Unassigned

Just going back through old issues... triggering a retest

martin107’s picture

StatusFileSize
new5.54 KB

@akishankar

Sorry this issue got lost ... here is a reroll .. all your changes look good!

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Some of these appear to have already been completed.

Also don't think t() should be used in tests unless the test is for translation.

rassoni’s picture

StatusFileSize
new2.58 MB
new19.8 KB
new18.08 KB
grep -rl "use[ ]*StringTranslationTrait" * |grep -v "^modules" |xargs grep -n "\<t\>(" | grep -v "\* *"  |  grep -v "this->t"

Found new files in D10. Fixed and create a patch. Please review.

rassoni’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

This looks much better!

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update
Parent issue: » #3113904: [META] Replace t() calls inside of classes

I checked the original issue and am adding credit for krishnan.n as suggested in the Issue Summary.

A reminder that when reviewing a patch or setting it to RTBC it is very helpful to describe in detail what one did to review the patch. There is Review a patch or merge request which should provide some guidance.

I applied that patch and ran the grep command, which I added to the IS, and there were more usages. The reason these are not being changed should be stated in the Issue Summary and/or the review comment.

The issue title status 'round 2'. Is there a round 3? Is there more to do? If these are indeed the last ones to fix, then a title change is in order.

Oh wait, there are many other issues about replacing t(). Yes, here is the Meta, #3113904: [META] Replace t() calls inside of classes . I am adding this to that Meta.

So, now we need to make sure that the scope is correct here and not conflicting with that issue or other children of that issue.

Setting to NW for scoping, title change, issue summary update.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Title: Replace t() with $this->t() where possible. ( Round 2 ) » Replace t() with $this->t() where StringTranslationTrait is used
Status: Needs work » Needs review
Issue tags: -Needs issue summary update

Running the grep command now results in the following. The remaining occurrences are in comments and non-object context.

)$ grep -rl "use[ ]*StringTranslationTrait" * |grep -v "^modules" |xargs grep -n "\<t\>(" | grep -v "\* *"  |  grep -v "this->t"
core/lib/Drupal/Core/Entity/EntityFieldManager.php:668:    // hook_entity_extra_field_info_alter() might contain t() calls, we cache
core/lib/Drupal/Core/Config/ConfigManager.php:178:      // Cast the result of t() to a string, as the diff engine doesn't know
core/lib/Drupal/Core/Config/ConfigManager.php:184:      // Cast the result of t() to a string, as the diff engine doesn't know
core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php:104:        // rather than use t() since we are in the middle of error handling, and
core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php:105:        // we don't want t() to cause further errors.
core/modules/media/src/Entity/Media.php:483:      ->setLabel(t('Name'))
core/modules/media/src/Entity/Media.php:497:      ->setLabel(t('Thumbnail'))
core/modules/media/src/Entity/Media.php:498:      ->setDescription(t('The thumbnail of the media item.'))
core/modules/media/src/Entity/Media.php:513:      ->setLabel(t('Authored by'))
core/modules/media/src/Entity/Media.php:514:      ->setDescription(t('The user ID of the author.'))
core/modules/media/src/Entity/Media.php:545:      ->setLabel(t('Authored on'))
core/modules/media/src/Entity/Media.php:546:      ->setDescription(t('The time the media item was created.'))
core/modules/media/src/Entity/Media.php:563:      ->setLabel(t('Changed'))
core/modules/media/src/Entity/Media.php:564:      ->setDescription(t('The time the media item was last edited.'))
core/modules/media_library/src/MediaLibraryUiBuilder.php:95:      'title' => t('Add or select media'),
quietone’s picture

Issue summary: View changes
quietone’s picture

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

I got a different result then #42 but after removing all the translation items believe I got the same actually

grep -rl "use[ ]*StringTranslationTrait" * |grep -v "^modules" |xargs grep -n "\<t\>(" | grep -v "\* *" | grep -v "this->t" was what I ran and I got

smustgrave@drupal-11x-web:/var/www/html/web/core$ grep -rl "use[ ]*StringTranslationTrait" * |grep -v "^modules" |xargs grep -n "\<t\>(" | grep -v "\* *"  |  grep -v "this->t"
lib/Drupal/Core/Config/ConfigManager.php:178:      // Cast the result of t() to a string, as the diff engine doesn't know
lib/Drupal/Core/Config/ConfigManager.php:184:      // Cast the result of t() to a string, as the diff engine doesn't know
lib/Drupal/Core/Entity/EntityFieldManager.php:668:    // hook_entity_extra_field_info_alter() might contain t() calls, we cache
lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php:104:        // rather than use t() since we are in the middle of error handling, and
lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php:105:        // we don't want t() to cause further errors.
smustgrave@drupal-11x-web:/var/www/html/web/core$ grep -rl "use[ ]*StringTranslationTrait" * |grep -v "^modules" |xargs grep -n "\<t\>(" | grep -v "\* *"  |  grep -v "this->t"
lib/Drupal/Core/Config/ConfigImporter.php:630:      $context['message'] = t('Synchronizing extensions: @op @name.', ['@op' => $operation['op'], '@name' => $operation['name']]);
lib/Drupal/Core/Config/ConfigImporter.php:732:    $context['message'] = t('Finalizing configuration synchronization.');
lib/Drupal/Core/Config/ConfigManager.php:178:      // Cast the result of t() to a string, as the diff engine doesn't know
lib/Drupal/Core/Config/ConfigManager.php:184:      // Cast the result of t() to a string, as the diff engine doesn't know
lib/Drupal/Core/Entity/EntityDisplayRepository.php:190:    $options = ['default' => t('Default')];
lib/Drupal/Core/Entity/EntityFieldManager.php:668:    // hook_entity_extra_field_info_alter() might contain t() calls, we cache
lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php:104:        // rather than use t() since we are in the middle of error handling, and
lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php:105:        // we don't want t() to cause further errors.
smustgrave@drupal-11x-web:/var/www/html/web/core$ cd ..
smustgrave@drupal-11x-web:/var/www/html/web$ grep -rl "use[ ]*StringTranslationTrait" * |grep -v "^modules" |xargs grep -n "\<t\>(" | grep -v "\* *"  |  grep -v "this->t"
core/lib/Drupal/Core/Config/ConfigImporter.php:630:      $context['message'] = t('Synchronizing extensions: @op @name.', ['@op' => $operation['op'], '@name' => $operation['name']]);
core/lib/Drupal/Core/Config/ConfigImporter.php:732:    $context['message'] = t('Finalizing configuration synchronization.');
core/lib/Drupal/Core/Config/ConfigManager.php:178:      // Cast the result of t() to a string, as the diff engine doesn't know
core/lib/Drupal/Core/Config/ConfigManager.php:184:      // Cast the result of t() to a string, as the diff engine doesn't know
core/lib/Drupal/Core/Entity/EntityDisplayRepository.php:190:    $options = ['default' => t('Default')];
core/lib/Drupal/Core/Entity/EntityFieldManager.php:668:    // hook_entity_extra_field_info_alter() might contain t() calls, we cache
core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php:104:        // rather than use t() since we are in the middle of error handling, and
core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php:105:        // we don't want t() to cause further errors.
core/modules/content_moderation/src/ViewsData.php:62:        'title' => t('Moderation state'),
core/modules/content_moderation/src/ViewsData.php:74:        'title' => t('Moderation state'),
core/modules/content_moderation/src/EntityTypeInfo.php:266:      ->setLabel(t('Moderation state'))
core/modules/content_moderation/src/EntityTypeInfo.php:267:      ->setDescription(t('The moderation state of this piece of content.'))
core/modules/file/src/Controller/FileWidgetAjaxController.php:33:        $progress['message'] = t('Uploading... (@current of @total)', [
core/modules/language/tests/language_test/src/Controller/LanguageTestController.php:80:        '#title' => t('Link to the current path with no langcode provided.'),
core/modules/language/tests/language_test/src/Controller/LanguageTestController.php:91:        '#title' => t('Link to a French version of the current path.'),
core/modules/language/tests/language_test/src/Controller/LanguageTestController.php:103:        '#title' => t('Link to an English version of the current path.'),
core/modules/user/src/ModulePermissionsLinkHelper.php:52:          'title' => t('Configure @module permissions', ['@module' => $name]),
core/modules/workspaces/src/WorkspacesLazyBuilders.php:41:        'title' => t('Switch workspace'),
core/modules/media_library/src/MediaLibraryUiBuilder.php:95:      'title' => t('Add or select media'),
core/modules/node/src/NodePermissions.php:60:        'description' => t('To view a revision, you also need permission to view the content item.'),
core/modules/node/src/NodePermissions.php:64:        'description' => t('To revert a revision, you also need permission to edit the content item.'),
core/modules/content_translation/src/ContentTranslationHandler.php:122:      ->setLabel(t('Translation source'))
core/modules/content_translation/src/ContentTranslationHandler.php:123:      ->setDescription(t('The source language from which this translation was created.'))
core/modules/content_translation/src/ContentTranslationHandler.php:130:      ->setLabel(t('Translation outdated'))
core/modules/content_translation/src/ContentTranslationHandler.php:131:      ->setDescription(t('A boolean indicating whether this translation needs to be updated.'))
core/modules/content_translation/src/ContentTranslationHandler.php:139:        ->setLabel(t('Translation author'))
core/modules/content_translation/src/ContentTranslationHandler.php:140:        ->setDescription(t('The author of this translation.'))
core/modules/content_translation/src/ContentTranslationHandler.php:150:        ->setLabel(t('Translation status'))
core/modules/content_translation/src/ContentTranslationHandler.php:151:        ->setDescription(t('A boolean indicating whether the translation is visible to non-translators.'))
core/modules/content_translation/src/ContentTranslationHandler.php:160:        ->setLabel(t('Translation created time'))
core/modules/content_translation/src/ContentTranslationHandler.php:161:        ->setDescription(t('The Unix timestamp when the translation was created.'))
core/modules/content_translation/src/ContentTranslationHandler.php:168:        ->setLabel(t('Translation changed time'))
core/modules/content_translation/src/ContentTranslationHandler.php:169:        ->setDescription(t('The Unix timestamp when the translation was most recently saved.'))
core/modules/content_translation/src/ContentTranslationHandler.php:306:        $title = $new_translation ? t('Create %language translation of %title', $t_args) : t('@title [%language translation]', $t_args);
core/modules/content_translation/src/ContentTranslationHandler.php:317:        '#title' => t('Source language: @language', ['@language' => $languages[$source_langcode]->getName()]),
core/modules/content_translation/src/ContentTranslationHandler.php:322:          '#title' => t('Select source language'),
core/modules/content_translation/src/ContentTranslationHandler.php:330:          '#value' => t('Change'),
core/modules/content_translation/src/ContentTranslationHandler.php:406:        '#title' => t('Translation'),
core/modules/content_translation/src/ContentTranslationHandler.php:437:        t('An unpublished translation will not be visible without translation permissions.') :
core/modules/content_translation/src/ContentTranslationHandler.php:438:        t('Only this translation is published. You must publish at least one more translation to unpublish this one.');
core/modules/content_translation/src/ContentTranslationHandler.php:442:        '#title' => t('This translation is published'),
core/modules/content_translation/src/ContentTranslationHandler.php:458:          '#title' => t('Flag other translations as outdated'),
core/modules/content_translation/src/ContentTranslationHandler.php:460:          '#description' => t('If you made a significant change, which means the other translations should be updated, you can flag all translations of this content as outdated. This will not change any other property of them, like whether they are published or not.'),
core/modules/content_translation/src/ContentTranslationHandler.php:467:          '#title' => t('This translation needs to be updated'),
core/modules/content_translation/src/ContentTranslationHandler.php:469:          '#description' => t('When this option is checked, this translation needs to be updated. Uncheck when the translation is up to date again.'),
core/modules/content_translation/src/ContentTranslationHandler.php:485:        '#title' => t('Authored by'),
core/modules/content_translation/src/ContentTranslationHandler.php:491:        '#description' => t('Leave blank for %anonymous.', ['%anonymous' => \Drupal::config('user.settings')->get('anonymous')]),
core/modules/content_translation/src/ContentTranslationHandler.php:497:        '#title' => t('Authored on'),
core/modules/content_translation/src/ContentTranslationHandler.php:499:        '#description' => t('Leave blank to use the time of form submission.'),
core/modules/content_translation/src/ContentTranslationHandler.php:618:      $suffix = ' <span class="translation-entity-all-languages">(' . t('all languages') . ')</span>';
core/modules/content_translation/src/ContentTranslationHandler.php:682:        $form_state->setErrorByName('content_translation][uid', t('The translation authoring username %name does not exist.', ['%name' => $account->getAccountName()]));
core/modules/content_translation/src/ContentTranslationHandler.php:686:        $form_state->setErrorByName('content_translation][created', t('You have to specify a valid translation authoring date.'));
core/modules/content_translation/src/ContentTranslationHandler.php:732:    $this->messenger->addStatus(t('Source language set to: %language', ['%language' => $languages[$source]->getName()]));
core/modules/content_translation/src/ContentTranslationHandler.php:744:      $this->messenger->addWarning(t('This will delete all the translations of %label.', ['%label' => $entity->label() ?? $entity->id()]));
core/modules/media/src/Entity/Media.php:483:      ->setLabel(t('Name'))
core/modules/media/src/Entity/Media.php:497:      ->setLabel(t('Thumbnail'))
core/modules/media/src/Entity/Media.php:498:      ->setDescription(t('The thumbnail of the media item.'))
core/modules/media/src/Entity/Media.php:513:      ->setLabel(t('Authored by'))
core/modules/media/src/Entity/Media.php:514:      ->setDescription(t('The user ID of the author.'))
core/modules/media/src/Entity/Media.php:545:      ->setLabel(t('Authored on'))
core/modules/media/src/Entity/Media.php:546:      ->setDescription(t('The time the media item was created.'))
core/modules/media/src/Entity/Media.php:563:      ->setLabel(t('Changed'))
core/modules/media/src/Entity/Media.php:564:      ->setDescription(t('The time the media item was last edited.'))

Either way definitely a net improvement.

  • catch committed 515180af on 11.x
    Issue #2629508 by krishnan.n, martin107, quietone, Rassoni, akishankar,...

catch’s picture

Status: Reviewed & tested by the community » Fixed

Looks good. Committed/pushed to 11.x, thanks!

Status: Fixed » Closed (fixed)

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