Chris@0: defaultLangcode = $default_language->get()->getId(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Appends a translation system to the translation chain. Chris@0: * Chris@0: * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translator Chris@0: * The translation interface to be appended to the translation chain. Chris@0: * @param int $priority Chris@0: * The priority of the logger being added. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function addTranslator(TranslatorInterface $translator, $priority = 0) { Chris@0: $this->translators[$priority][] = $translator; Chris@0: // Reset sorted translators property to trigger rebuild. Chris@0: $this->sortedTranslators = NULL; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sorts translators according to priority. Chris@0: * Chris@0: * @return \Drupal\Core\StringTranslation\Translator\TranslatorInterface[] Chris@0: * A sorted array of translator objects. Chris@0: */ Chris@0: protected function sortTranslators() { Chris@0: $sorted = []; Chris@0: krsort($this->translators); Chris@0: Chris@0: foreach ($this->translators as $translators) { Chris@0: $sorted = array_merge($sorted, $translators); Chris@0: } Chris@0: return $sorted; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getStringTranslation($langcode, $string, $context) { Chris@0: if ($this->sortedTranslators === NULL) { Chris@0: $this->sortedTranslators = $this->sortTranslators(); Chris@0: } Chris@0: foreach ($this->sortedTranslators as $translator) { Chris@0: $translation = $translator->getStringTranslation($langcode, $string, $context); Chris@0: if ($translation !== FALSE) { Chris@0: return $translation; Chris@0: } Chris@0: } Chris@0: // No translator got a translation. Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function translate($string, array $args = [], array $options = []) { Chris@0: return new TranslatableMarkup($string, $args, $options, $this); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function translateString(TranslatableMarkup $translated_string) { Chris@0: return $this->doTranslate($translated_string->getUntranslatedString(), $translated_string->getOptions()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Translates a string to the current language or to a given language. Chris@0: * Chris@0: * @param string $string Chris@0: * A string containing the English text to translate. Chris@0: * @param array $options Chris@0: * An associative array of additional options, with the following elements: Chris@0: * - 'langcode': The language code to translate to a language other than Chris@0: * what is used to display the page. Chris@0: * - 'context': The context the source string belongs to. Chris@0: * Chris@0: * @return string Chris@0: * The translated string. Chris@0: */ Chris@0: protected function doTranslate($string, array $options = []) { Chris@0: // If a NULL langcode has been provided, unset it. Chris@0: if (!isset($options['langcode']) && array_key_exists('langcode', $options)) { Chris@0: unset($options['langcode']); Chris@0: } Chris@0: Chris@0: // Merge in options defaults. Chris@0: $options = $options + [ Chris@0: 'langcode' => $this->defaultLangcode, Chris@0: 'context' => '', Chris@0: ]; Chris@0: $translation = $this->getStringTranslation($options['langcode'], $string, $options['context']); Chris@0: return $translation === FALSE ? $string : $translation; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function formatPlural($count, $singular, $plural, array $args = [], array $options = []) { Chris@0: return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the default langcode. Chris@0: * Chris@0: * @param string $langcode Chris@0: * A language code. Chris@0: */ Chris@0: public function setDefaultLangcode($langcode) { Chris@0: $this->defaultLangcode = $langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function reset() { Chris@0: if ($this->sortedTranslators === NULL) { Chris@0: $this->sortedTranslators = $this->sortTranslators(); Chris@0: } Chris@0: foreach ($this->sortedTranslators as $translator) { Chris@0: $translator->reset(); Chris@0: } Chris@0: } Chris@0: Chris@0: }