Translation.php 2.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Definition of Drupal\Core\Annotation\Translation.
 */

namespace Drupal\Core\Annotation;

10
use Drupal\Component\Annotation\AnnotationInterface;
11

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * @defgroup plugin_translatable Translatable plugin metadata
 *
 * @{
 * 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.
 *
 * You will also need to make sure that your class file includes the line:
 * @code
 *   use Drupal\Core\Annotation\Translation;
 * @endcode
31
32
33
34
35
36
37
 *
 * 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.
38
39
40
 * @}
 */

41
42
43
44
45
/**
 * 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
46
47
 * specified, a context for that string. The string (with optional context)
 * is passed into t().
48
49
 *
 * @Annotation
50
51
 *
 * @ingroup plugin_translatable
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 */
class Translation implements AnnotationInterface {

  /**
   * The translation of the value passed to the constructor of the class.
   *
   * @var string
   */
  protected $translation;

  /**
   * Constructs a Translation object.
   *
   * Parses values passed into this class through the t() function in Drupal and
   * handles an optional context for the string.
   */
  public function __construct($values) {
    $string = $values['value'];
    $options = array();
    if (!empty($values['context'])) {
      $options = array(
        'context' => $values['context'],
      );
    }
    $this->translation = t($string, array(), $options);
  }

  /**
   * Implements Drupal\Core\Annotation\AnnotationInterface::get().
   */
  public function get() {
    return $this->translation;
  }

}