6

When writing a (translatable) plugin in Drupal 8, you have to use Drupal\Core\Annotation\Translation;, and then wrap your value in @Translation("value"), however when implementing the FormInterface, you can use t() freely as you would in Drupal 7.

I found Annotation-based plugins on Drupal.org, but it just mentions the fact that you should use it, and the Doctrine reference doesn't seem to include @Translation

I assume it is because the plugin system is part of doctrine, and not part of core, whereas t() is -is this the correct assumption?

Basically, what is the difference between @Translation(); and t() - is it just because plugins are Doctrine based?

  • Isn't @ in @Translation("value") just an php error control operator? Just asking. – Mołot Aug 21 '13 at 10:24
  • 2
    @Mołot Not :) is it just because plugins are Doctrine based? not quite, it's because they're annotation based. You can't call a PHP function from inside annotation metadata, so a special annotation has been defined to handle the same work that t() does. php-and-symfony.matthiasnoback.nl/2011/12/… might be interesting reading as a grounding in custom annotations. I don't know the technical terms for these concepts inside Drupal so I won't put an answer in – Clive Aug 21 '13 at 10:32
  • 2
    @Mołot I think the simplest way to put it is that @Translation is a wrapper around t(), only practically accessible for use in the context of an annotation. The Translation plugin actually uses t() internally anyway. Chapabu, check out the comments in Drupal\Core\Annotation\Translation and Drupal\Core\Annotation\Plugin, they should go some way towards explaining this stuff :) – Clive Aug 21 '13 at 10:41
  • 1
    @Clive Awesome. I've just grokked the routing, FormInterface and SystemConfigFormBase (and ConfigFactory stuff) so custom entities/fields was my next port of call anyway :) – Chapabu Aug 21 '13 at 11:01
  • 2
    Aport from actually translating it (that would also be possible by simply defining which keys for each plugin should be translated), an important aspect of it is that translatable strings can be extracted just like they can for t() using potx.module. – Berdir Aug 21 '13 at 11:25
9

In short, @Translation() is a string used in annotations, meta data written in PHP comments and pertinent to a Drupal 8 class.

For example, this is the annotation for the PathItem entity field class.

/**
 * Defines the 'path_field' entity field item.
 *
 * @DataType(
 *   id = "path_field",
 *   label = @Translation("Path field item"),
 *   description = @Translation("An entity field containing a path alias and related data."),
 *   list_class = "\Drupal\Core\Entity\Field\Field"
 * )
 */

When Drupal loads the class annotations from a file, and it finds @Translation("Content") in the annotation, it creates an instance of the Translation class whose constructor receives the "Content" string; the instance is then used to get the value of t("Content").

As for the differences, they are the following ones:

  • @Translation() is a string used in class annotations: plain text that follows a specific format and it is written in PHP comments before the class that is being annotated. You cannot use it in code similar to the following one, except in the case a module defines a Translation() function. In that case, the @ before it would be understood from PHP as the error control operator.

    $form['salary'] = array(
      '#type' => 'textfield',
      '#title' => @Translation('Salary'),
      '#size' => 10,
      '#maxlength' => 10,
      '#description' => @Translation('Please enter a valid number'),
    );
    
  • t() is a Drupal function. It cannot be used inside class annotations like the following one, since it doesn't follow the syntax annotations follow. If it were used in an annotation as in the following, it would not cause Drupal to call t() and use its return value.

    /**
     * Defines the 'path_field' entity field item.
     *
     * @DataType(
     *   id = "path_field",
     *   label = t("Path field item"),
     *   description = t("An entity field containing a path alias and related data."),
     *   list_class = "\Drupal\Core\Entity\Field\Field"
     * )
     */
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.