3 namespace Drupal\Core\StringTranslation;
6 * Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.
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.
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.
17 * @see \Drupal\Core\StringTranslation\TranslationInterface
22 trait StringTranslationTrait {
25 * The string translation service.
27 * @var \Drupal\Core\StringTranslation\TranslationInterface
29 protected $stringTranslation;
32 * Translates a string to the current language or to a given language.
34 * See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for
35 * important security information and usage guidelines.
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.
44 * @param string $string
45 * A string containing the English text to translate.
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
52 * @param array $options
53 * (optional) An associative array of additional options, with the following
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.
62 * @return \Drupal\Core\StringTranslation\TranslatableMarkup
63 * An object that, when cast to a string, returns the translated string.
65 * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
66 * @see \Drupal\Core\StringTranslation\TranslatableMarkup::__construct()
68 * @ingroup sanitization
70 protected function t($string, array $args = [], array $options = []) {
71 return new TranslatableMarkup($string, $args, $options, $this->getStringTranslation());
75 * Formats a string containing a count of items.
77 * @see \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
79 protected function formatPlural($count, $singular, $plural, array $args = [], array $options = []) {
80 return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this->getStringTranslation());
84 * Returns the number of plurals supported by a given language.
86 * @see \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals()
88 protected function getNumberOfPlurals($langcode = NULL) {
89 if (\Drupal::hasService('locale.plural.formula')) {
90 return \Drupal::service('locale.plural.formula')->getNumberOfPlurals($langcode);
92 // We assume 2 plurals if Locale's services are not available.
97 * Gets the string translation service.
99 * @return \Drupal\Core\StringTranslation\TranslationInterface
100 * The string translation service.
102 protected function getStringTranslation() {
103 if (!$this->stringTranslation) {
104 $this->stringTranslation = \Drupal::service('string_translation');
107 return $this->stringTranslation;
111 * Sets the string translation service to use.
113 * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
114 * The string translation service.
118 public function setStringTranslation(TranslationInterface $translation) {
119 $this->stringTranslation = $translation;