$this->t() should be used for translation.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chaitanya17 created an issue. See original summary.

chaitanya17’s picture

FileSize
9.41 KB

Please check attached working patch.

chaitanya17’s picture

Status: Active » Needs review
Erik Seifert’s picture

FileSize
13.42 KB
Erik Seifert’s picture

Use StringTranslation trait

  • Erik Seifert committed 2dcdbc9 on 8.x-1.x
    Issue #2845309 by chaitanya17, Erik Seifert: $this->t() should be used...
Hydra’s picture

This commit is causing trouble, when you call the static method outside the object context.

+++ b/src/Plugin/Field/FieldType/VotingApiField.php
@@ -57,13 +60,13 @@ class VotingApiField extends FieldItemBase {
-    // Prevent early t() calls by using the TranslatableMarkup.
+    // Prevent early $this->t() calls by using the TranslatableMarkup.
...
-      ->setLabel(t('Vote status'))
+      ->setLabel($this->t('Vote status'))
...
-      ->setLabel(t('Vote initial'))
+      ->setLabel($this->t('Vote initial'))

Their is an issue and a patch https://www.drupal.org/node/2849226

Hydra’s picture

  1. +++ b/src/Plugin/Field/FieldFormatter/VotingApiFormatter.php
    @@ -5,6 +5,7 @@ namespace Drupal\votingapi_widgets\Plugin\Field\FieldFormatter;
    +use Drupal\Core\StringTranslation\StringTranslationTrait;
    
    @@ -19,16 +20,18 @@ use Drupal\Core\Form\FormStateInterface;
    +  use StringTranslationTrait;
    +
    

    No need to use this twice here as well, its already used by PluginBase.

  2. +++ b/src/Plugin/Field/FieldType/VotingApiField.php
    @@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
    +use Drupal\Core\StringTranslation\StringTranslationTrait;
    
    @@ -23,6 +24,8 @@ use Drupal\votingapi\Entity\VoteType;
    +  use StringTranslationTrait;
    +
    

    On top of that, the StringTranslationTrait is already in use by the parent class "TypedData" which will cause a strict php warning. I would consider not using this twice.

The provided patch also fixes 2849226.

Erik Seifert’s picture

We need tests ;- )

  • Erik Seifert committed 1a88258 on 8.x-1.x authored by Hydra
    Issue #2845309 by chaitanya17, Erik Seifert, Hydra: $this->t() should be...
Hydra’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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