1. 8.3.x core/lib/Drupal/Core/Annotation/Translation.php
  2. 8.0.x core/lib/Drupal/Core/Annotation/Translation.php
  3. 8.1.x core/lib/Drupal/Core/Annotation/Translation.php
  4. 8.2.x core/lib/Drupal/Core/Annotation/Translation.php
  5. 8.4.x core/lib/Drupal/Core/Annotation/Translation.php

Namespace

Drupal\Core\Annotation

File

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

Classes

Namesort descending Description
Translation Defines a translatable annotation object.