Chris@0: formatPlural(). Chris@0: * This allows string extractor tools to find translatable strings. Chris@0: * Chris@0: * If the class is capable of injecting services from the container, it should Chris@0: * inject the 'string_translation' service and assign it to Chris@0: * $this->stringTranslation. Chris@0: * Chris@0: * @see \Drupal\Core\StringTranslation\TranslationInterface Chris@0: * @see container Chris@0: * Chris@0: * @ingroup i18n Chris@0: */ Chris@0: trait StringTranslationTrait { Chris@0: Chris@0: /** Chris@0: * The string translation service. Chris@0: * Chris@0: * @var \Drupal\Core\StringTranslation\TranslationInterface Chris@0: */ Chris@0: protected $stringTranslation; Chris@0: Chris@0: /** Chris@0: * Translates a string to the current language or to a given language. Chris@0: * Chris@0: * See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for Chris@0: * important security information and usage guidelines. Chris@0: * Chris@0: * In order for strings to be localized, make them available in one of the Chris@0: * ways supported by the Chris@0: * @link https://www.drupal.org/node/322729 Localization API @endlink. When Chris@0: * possible, use the \Drupal\Core\StringTranslation\StringTranslationTrait Chris@0: * $this->t(). Otherwise create a new Chris@0: * \Drupal\Core\StringTranslation\TranslatableMarkup object. Chris@0: * Chris@0: * @param string $string Chris@0: * A string containing the English text to translate. Chris@0: * @param array $args Chris@0: * (optional) An associative array of replacements to make after Chris@0: * translation. Based on the first character of the key, the value is Chris@0: * escaped and/or themed. See Chris@0: * \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for Chris@0: * details. Chris@0: * @param array $options Chris@0: * (optional) An associative array of additional options, with the following Chris@0: * elements: Chris@0: * - 'langcode' (defaults to the current language): A language code, to Chris@0: * translate to a language other than what is used to display the page. Chris@0: * - 'context' (defaults to the empty context): The context the source Chris@0: * string belongs to. See the Chris@0: * @link i18n Internationalization topic @endlink for more information Chris@0: * about string contexts. Chris@0: * Chris@0: * @return \Drupal\Core\StringTranslation\TranslatableMarkup Chris@0: * An object that, when cast to a string, returns the translated string. Chris@0: * Chris@0: * @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat() Chris@0: * @see \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() Chris@0: * Chris@0: * @ingroup sanitization Chris@0: */ Chris@0: protected function t($string, array $args = [], array $options = []) { Chris@0: return new TranslatableMarkup($string, $args, $options, $this->getStringTranslation()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Formats a string containing a count of items. Chris@0: * Chris@0: * @see \Drupal\Core\StringTranslation\TranslationInterface::formatPlural() Chris@0: */ Chris@0: protected function formatPlural($count, $singular, $plural, array $args = [], array $options = []) { Chris@0: return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this->getStringTranslation()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the number of plurals supported by a given language. Chris@0: * Chris@0: * @see \Drupal\locale\PluralFormulaInterface::getNumberOfPlurals() Chris@0: */ Chris@0: protected function getNumberOfPlurals($langcode = NULL) { Chris@0: if (\Drupal::hasService('locale.plural.formula')) { Chris@0: return \Drupal::service('locale.plural.formula')->getNumberOfPlurals($langcode); Chris@0: } Chris@0: // We assume 2 plurals if Locale's services are not available. Chris@0: return 2; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the string translation service. Chris@0: * Chris@0: * @return \Drupal\Core\StringTranslation\TranslationInterface Chris@0: * The string translation service. Chris@0: */ Chris@0: protected function getStringTranslation() { Chris@0: if (!$this->stringTranslation) { Chris@0: $this->stringTranslation = \Drupal::service('string_translation'); Chris@0: } Chris@0: Chris@0: return $this->stringTranslation; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the string translation service to use. Chris@0: * Chris@0: * @param \Drupal\Core\StringTranslation\TranslationInterface $translation Chris@0: * The string translation service. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setStringTranslation(TranslationInterface $translation) { Chris@0: $this->stringTranslation = $translation; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: }