Use standards Drupal 8 translation.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mgoncalves created an issue. See original summary.

mgoncalves’s picture

Assigned: mgoncalves » Unassigned
Status: Active » Needs review
FileSize
2.72 KB

Status: Needs review » Needs work

The last submitted patch, 2: feeds-change_translation_function-2865882-2-8x.patch, failed testing.

MegaChriz’s picture

Using t() isn't strictly wrong, this function is not deprecated, but it is encouraged to not use it when possible. The reason behind replacing t() with something else is to make it unit testable.

  1. public static function validateUrl(&$element, FormStateInterface $form_state, &$complete_form) {
    -      $form_state->setError($element, t('The URI %url is not valid.', ['%url' => $value]));
    +      $form_state->setError($element, $this->t('The URI %url is not valid.', ['%url' => $value]));
    

    $this->t() cannot be used in static functions. Instead a new TranslatableMarkup object should be created.

  2. --- a/src/EventSubscriber/PubSubHubbub.php
    +++ b/src/EventSubscriber/PubSubHubbub.php
    @@ -113,13 +113,13 @@ class PubSubHubbub implements EventSubscriberInterface {
         $subscription->subscribe();
     
         $batch = [
    -      'title' => t('Subscribing to: %title', ['%title' => $feed->label()]),
    -      'init_message' => t('Subscribing to: %title', ['%title' => $feed->label()]),
    +      'title' => $this->t('Subscribing to: %title', ['%title' => $feed->label()]),
    +      'init_message' => $this->t('Subscribing to: %title', ['%title' => $feed->label()]),
           'operations' => [
             ['Drupal\feeds\EventSubscriber\PubSubHubbub::runSubscribeBatch', [$subscription]],
           ],
    -      'progress_message' => t('Subscribing: %title', ['%title' => $feed->label()]),
    -      'error_message' => t('An error occored while subscribing to %title.', ['%title' => $feed->label()]),
    +      'progress_message' => $this->t('Subscribing: %title', ['%title' => $feed->label()]),
    +      'error_message' => $this->t('An error occored while subscribing to %title.', ['%title' => $feed->label()]),
         ];
     
         batch_set($batch);
    @@ -133,13 +133,13 @@ class PubSubHubbub implements EventSubscriberInterface {
         $subscription->unsubscribe();
     
         $batch = [
    -      'title' => t('Unsubscribing from: %title', ['%title' => $feed->label()]),
    -      'init_message' => t('Unsubscribing from: %title', ['%title' => $feed->label()]),
    +      'title' => $this->t('Unsubscribing from: %title', ['%title' => $feed->label()]),
    +      'init_message' => $this->t('Unsubscribing from: %title', ['%title' => $feed->label()]),
           'operations' => [
             ['Drupal\feeds\EventSubscriber\PubSubHubbub::runSubscribeBatch', [$subscription]],
           ],
    -      'progress_message' => t('Unsubscribing: %title', ['%title' => $feed->label()]),
    -      'error_message' => t('An error occored while unsubscribing from %title.', ['%title' => $feed->label()]),
    +      'progress_message' => $this->t('Unsubscribing: %title', ['%title' => $feed->label()]),
    +      'error_message' => $this->t('An error occored while unsubscribing from %title.', ['%title' => $feed->label()]),
         ];
    

    The PubSubHubbub class does not have a t() method. In order to get one, it should use the StringTranslationTrait.

MegaChriz’s picture

If applying the best practises anyway, the PubSubHubbub class should also get the string translation service injected. This service can be passed by specifying it in feeds.services.yml: add an argument for the service 'feeds.pubsubhubbub'.

MegaChriz’s picture

Title: Use "$this->t()" instead of "t()" function. » Inject translation service into feeds.pubsubhubbub service
Category: Bug report » Task
Issue tags: -ciandt-contrib +Novice
MegaChriz’s picture

Priority: Normal » Minor
MegaChriz’s picture

Status: Needs work » Closed (duplicate)