class Translation

Same name in other branches
  1. 9 core/lib/Drupal/Core/Annotation/Translation.php \Drupal\Core\Annotation\Translation
  2. 8.9.x core/lib/Drupal/Core/Annotation/Translation.php \Drupal\Core\Annotation\Translation
  3. 10 core/lib/Drupal/Core/Annotation/Translation.php \Drupal\Core\Annotation\Translation

Defines a translatable annotation object.

Some metadata within an annotation needs to be translatable. This class supports that need by allowing both the translatable string and, if specified, a context for that string. The string (with optional context) is passed into t().

Hierarchy

Expanded class hierarchy of Translation

Related topics

1 file declares its use of Translation
TranslationTest.php in core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php
60 string references to 'Translation'
BlockContentHooks::entityTypeAlter in core/modules/block_content/src/Hook/BlockContentHooks.php
Implements hook_entity_type_alter().
BlockTranslation::fields in core/modules/block/src/Plugin/migrate/source/d7/BlockTranslation.php
Returns available fields on the source.
CommentTestHooks::entityTypeAlter in core/modules/comment/tests/modules/comment_test/src/Hook/CommentTestHooks.php
Implements hook_entity_type_alter().
comment_test_entity_type_alter in core/modules/comment/tests/modules/comment_test/comment_test.module
Implements hook_entity_type_alter().
ConfigTranslationFormBase::submitForm in core/modules/config_translation/src/Form/ConfigTranslationFormBase.php
Form submission handler.

... See full list

File

core/lib/Drupal/Core/Annotation/Translation.php, line 53

Namespace

Drupal\Core\Annotation
View source
class Translation extends AnnotationBase {
    
    /**
     * The string translation object.
     *
     * @var \Drupal\Core\StringTranslation\TranslatableMarkup
     */
    protected $translation;
    
    /**
     * Constructs a new class instance.
     *
     * Parses values passed into this class through the t() function in Drupal and
     * handles an optional context for the string.
     *
     * @param array $values
     *   Possible array keys:
     *   - value (required): the string that is to be translated.
     *   - arguments (optional): an array with placeholder replacements, keyed by
     *     placeholder.
     *   - context (optional): a string that describes the context of "value";
     */
    public function __construct(array $values) {
        $string = $values['value'];
        $arguments = $values['arguments'] ?? [];
        $options = [];
        if (!empty($values['context'])) {
            $options = [
                'context' => $values['context'],
            ];
        }
        // phpcs:ignore Drupal.Semantics.FunctionT.NotLiteralString
        $this->translation = new TranslatableMarkup($string, $arguments, $options);
    }
    
    /**
     * {@inheritdoc}
     */
    public function get() {
        return $this->translation;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AnnotationBase::$class protected property The class used for this annotated class.
AnnotationBase::$id public property The annotated class ID. 1
AnnotationBase::$provider protected property The provider of the annotated class.
AnnotationBase::getClass public function Gets the class of the annotated class. Overrides AnnotationInterface::getClass
AnnotationBase::getId public function Gets the unique ID for this annotated class. Overrides AnnotationInterface::getId 1
AnnotationBase::getProvider public function Gets the name of the provider of the annotated class. Overrides AnnotationInterface::getProvider
AnnotationBase::setClass public function Sets the class of the annotated class. Overrides AnnotationInterface::setClass
AnnotationBase::setProvider public function Sets the name of the provider of the annotated class. Overrides AnnotationInterface::setProvider
Translation::$translation protected property The string translation object.
Translation::get public function Gets the value of an annotation. Overrides AnnotationInterface::get
Translation::__construct public function Constructs a new class instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.