StringTranslationTrait.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Drupal\Core\StringTranslation;
  3. /**
  4. * Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
  5. *
  6. * Using this trait will add t() and formatPlural() methods to the class. These
  7. * must be used for every translatable string, similar to how procedural code
  8. * must use the global functions t() and \Drupal::translation()->formatPlural().
  9. * This allows string extractor tools to find translatable strings.
  10. *
  11. * If the class is capable of injecting services from the container, it should
  12. * inject the 'string_translation' service and assign it to
  13. * $this->stringTranslation.
  14. *
  15. * @see \Drupal\Core\StringTranslation\TranslationInterface
  16. * @see container
  17. *
  18. * @ingroup i18n
  19. */
  20. trait StringTranslationTrait {
  21. /**
  22. * The string translation service.
  23. *
  24. * @var \Drupal\Core\StringTranslation\TranslationInterface
  25. */
  26. protected $stringTranslation;
  27. /**
  28. * Translates a string to the current language or to a given language.
  29. *
  30. * See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for
  31. * important security information and usage guidelines.
  32. *
  33. * In order for strings to be localized, make them available in one of the
  34. * ways supported by the
  35. * @link https://www.drupal.org/node/322729 Localization API @endlink. When
  36. * possible, use the \Drupal\Core\StringTranslation\StringTranslationTrait
  37. * $this->t(). Otherwise create a new
  38. * \Drupal\Core\StringTranslation\TranslatableMarkup object.
  39. *
  40. * @param string $string
  41. * A string containing the English text to translate.
  42. * @param array $args
  43. * (optional) An associative array of replacements to make after
  44. * translation. Based on the first character of the key, the value is
  45. * escaped and/or themed. See
  46. * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for
  47. * details.
  48. * @param array $options
  49. * (optional) An associative array of additional options, with the following
  50. * elements:
  51. * - 'langcode' (defaults to the current language): A language code, to
  52. * translate to a language other than what is used to display the page.
  53. * - 'context' (defaults to the empty context): The context the source
  54. * string belongs to. See the
  55. * @link i18n Internationalization topic @endlink for more information
  56. * about string contexts.
  57. *
  58. * @return \Drupal\Core\StringTranslation\TranslatableMarkup
  59. * An object that, when cast to a string, returns the translated string.
  60. *
  61. * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
  62. * @see \Drupal\Core\StringTranslation\TranslatableMarkup::__construct()
  63. *
  64. * @ingroup sanitization
  65. */
  66. protected function t($string, array $args = [], array $options = []) {
  67. return new TranslatableMarkup($string, $args, $options, $this->getStringTranslation());
  68. }
  69. /**
  70. * Formats a string containing a count of items.
  71. *
  72. * @see \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
  73. */
  74. protected function formatPlural($count, $singular, $plural, array $args = [], array $options = []) {
  75. return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this->getStringTranslation());
  76. }
  77. /**
  78. * Returns the number of plurals supported by a given language.
  79. *
  80. * @see \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals()
  81. */
  82. protected function getNumberOfPlurals($langcode = NULL) {
  83. if (\Drupal::hasService('locale.plural.formula')) {
  84. return \Drupal::service('locale.plural.formula')->getNumberOfPlurals($langcode);
  85. }
  86. // We assume 2 plurals if Locale's services are not available.
  87. return 2;
  88. }
  89. /**
  90. * Gets the string translation service.
  91. *
  92. * @return \Drupal\Core\StringTranslation\TranslationInterface
  93. * The string translation service.
  94. */
  95. protected function getStringTranslation() {
  96. if (!$this->stringTranslation) {
  97. $this->stringTranslation = \Drupal::service('string_translation');
  98. }
  99. return $this->stringTranslation;
  100. }
  101. /**
  102. * Sets the string translation service to use.
  103. *
  104. * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
  105. * The string translation service.
  106. *
  107. * @return $this
  108. */
  109. public function setStringTranslation(TranslationInterface $translation) {
  110. $this->stringTranslation = $translation;
  111. return $this;
  112. }
  113. }