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