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