Translation.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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'] : [];
  72. $options = [];
  73. if (!empty($values['context'])) {
  74. $options = [
  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. }