|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Definition of Drupal\Core\Annotation\Translation. |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Drupal\Core\Annotation; |
| 9 | + |
| 10 | +use Drupal\Component\Annotation\AnnotationBase; |
| 11 | +use Drupal\Core\StringTranslation\TranslationWrapper; |
| 12 | + |
| 13 | +/** |
| 14 | + * @defgroup plugin_translatable Annotation for translatable text |
| 15 | + * @{ |
| 16 | + * Describes how to put translatable UI text into annotations. |
| 17 | + * |
| 18 | + * When providing plugin annotation, properties whose values are displayed in |
| 19 | + * the user interface should be made translatable. Much the same as how user |
| 20 | + * interface text elsewhere is wrapped in t() to make it translatable, in plugin |
| 21 | + * annotation, wrap translatable strings in the @ Translation() annotation. |
| 22 | + * For example: |
| 23 | + * @code |
| 24 | + * title = @ Translation("Title of the plugin"), |
| 25 | + * @endcode |
| 26 | + * Remove spaces after @ in your actual plugin - these are put into this sample |
| 27 | + * code so that it is not recognized as annotation. |
| 28 | + * |
| 29 | + * To provide replacement values for placeholders, use the "arguments" array: |
| 30 | + * @code |
| 31 | + * title = @ Translation("Bundle !title", arguments = {"!title" = "Foo"}), |
| 32 | + * @endcode |
| 33 | + * |
| 34 | + * It is also possible to provide a context with the text, similar to t(): |
| 35 | + * @code |
| 36 | + * title = @ Translation("Bundle", context = "Validation"), |
| 37 | + * @endcode |
| 38 | + * Other t() arguments like language code are not valid to pass in. Only |
| 39 | + * context is supported. |
| 40 | + * |
| 41 | + * @see i18n |
| 42 | + * @see annotation |
| 43 | + * @} |
| 44 | + */ |
| 45 | + |
| 46 | +/** |
| 47 | + * Defines a translatable annotation object. |
| 48 | + * |
| 49 | + * Some metadata within an annotation needs to be translatable. This class |
| 50 | + * supports that need by allowing both the translatable string and, if |
| 51 | + * specified, a context for that string. The string (with optional context) |
| 52 | + * is passed into t(). |
| 53 | + * |
| 54 | + * @ingroup plugin_translatable |
| 55 | + * |
| 56 | + * @Annotation |
| 57 | + */ |
| 58 | +class Translation extends AnnotationBase { |
| 59 | + |
| 60 | + /** |
| 61 | + * The string translation object. |
| 62 | + * |
| 63 | + * @var \Drupal\Core\StringTranslation\TranslationWrapper |
| 64 | + */ |
| 65 | + protected $translation; |
| 66 | + |
| 67 | + /** |
| 68 | + * Constructs a new class instance. |
| 69 | + * |
| 70 | + * Parses values passed into this class through the t() function in Drupal and |
| 71 | + * handles an optional context for the string. |
| 72 | + * |
| 73 | + * @param array $values |
| 74 | + * Possible array keys: |
| 75 | + * - value (required): the string that is to be translated. |
| 76 | + * - arguments (optional): an array with placeholder replacements, keyed by |
| 77 | + * placeholder. |
| 78 | + * - context (optional): a string that describes the context of "value"; |
| 79 | + */ |
| 80 | + public function __construct(array $values) { |
| 81 | + $string = $values['value']; |
| 82 | + $arguments = isset($values['arguments']) ? $values['arguments'] : array(); |
| 83 | + $options = array(); |
| 84 | + if (!empty($values['context'])) { |
| 85 | + $options = array( |
| 86 | + 'context' => $values['context'], |
| 87 | + ); |
| 88 | + } |
| 89 | + $this->translation = new TranslationWrapper($string, $arguments, $options); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * {@inheritdoc} |
| 94 | + */ |
| 95 | + public function get() { |
| 96 | + return $this->translation; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments