Replace t() with $this->t() where StringTranslationTrait is used

Created on 5 December 2015, about 9 years ago
Updated 31 July 2024, 5 months ago

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

📌 Task
Status

Fixed

Version

11.0 🔥

Component
Configuration  →

Last updated 3 days ago

Created by

🇬🇧United Kingdom martin107

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India rassoni Bangalore
    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.

  • Status changed to Needs review almost 2 years ago
  • 🇮🇳India rassoni Bangalore
  • Status changed to RTBC almost 2 years ago
  • 🇺🇸United States smustgrave

    This looks much better!

  • last update over 1 year ago
    Patch Failed to Apply
  • Status changed to Needs work over 1 year ago
  • 🇳🇿New Zealand quietone

    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, 📌 [META] Replace t() calls inside of classes Active . 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.

  • Pipeline finished with Success
    6 months ago
    #210607
  • Status changed to Needs review 6 months ago
  • 🇳🇿New Zealand quietone

    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'),
  • 🇳🇿New Zealand quietone
  • 🇳🇿New Zealand quietone
  • Status changed to RTBC 6 months ago
  • 🇺🇸United States smustgrave

    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,...
  • Status changed to Fixed 5 months ago
  • 🇬🇧United Kingdom catch

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

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

Production build 0.71.5 2024