Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\Core\Annotation\Translation.
Dries Buytaert
committed
*/
namespace Drupal\Core\Annotation;
Alex Pott
committed
use Drupal\Component\Annotation\AnnotationBase;
Alex Bronstein
committed
use Drupal\Core\StringTranslation\TranslatableMarkup;
Dries Buytaert
committed
Jennifer Hodgdon
committed
/**
* @defgroup plugin_translatable Annotation for translatable text
Jennifer Hodgdon
committed
* @{
* Describes how to put translatable UI text into annotations.
*
Jennifer Hodgdon
committed
* When providing plugin annotation, properties whose values are displayed in
* the user interface should be made translatable. Much the same as how user
* interface text elsewhere is wrapped in t() to make it translatable, in plugin
* annotation, wrap translatable strings in the @ Translation() annotation.
* For example:
* @code
* title = @ Translation("Title of the plugin"),
* @endcode
* Remove spaces after @ in your actual plugin - these are put into this sample
* code so that it is not recognized as annotation.
*
Angie Byron
committed
* To provide replacement values for placeholders, use the "arguments" array:
* @code
* title = @ Translation("Bundle !title", arguments = {"!title" = "Foo"}),
* @endcode
*
Jennifer Hodgdon
committed
* It is also possible to provide a context with the text, similar to t():
* @code
* title = @ Translation("Bundle", context = "Validation"),
* @endcode
* Other t() arguments like language code are not valid to pass in. Only
* context is supported.
*
* @see i18n
* @see annotation
Jennifer Hodgdon
committed
* @}
*/
Dries Buytaert
committed
/**
* 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
Jennifer Hodgdon
committed
* specified, a context for that string. The string (with optional context)
* is passed into t().
Dries Buytaert
committed
*
Jennifer Hodgdon
committed
* @ingroup plugin_translatable
*
* @Annotation
Dries Buytaert
committed
*/
Alex Pott
committed
class Translation extends AnnotationBase {
Dries Buytaert
committed
/**
* The string translation object.
Dries Buytaert
committed
*
Alex Bronstein
committed
* @var \Drupal\Core\StringTranslation\TranslatableMarkup
Dries Buytaert
committed
*/
protected $translation;
Angie Byron
committed
/**
* Constructs a new class instance.
Dries Buytaert
committed
*
* Parses values passed into this class through the t() function in Drupal and
* handles an optional context for the string.
Angie Byron
committed
*
* @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";
Dries Buytaert
committed
*/
Angie Byron
committed
public function __construct(array $values) {
Dries Buytaert
committed
$string = $values['value'];
Angie Byron
committed
$arguments = isset($values['arguments']) ? $values['arguments'] : array();
Dries Buytaert
committed
$options = array();
if (!empty($values['context'])) {
$options = array(
'context' => $values['context'],
);
}
Alex Bronstein
committed
$this->translation = new TranslatableMarkup($string, $arguments, $options);
Dries Buytaert
committed
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function get() {
return $this->translation;
}
}