diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php index b465983..fce0311 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php @@ -19,11 +19,24 @@ class TranslationManager implements TranslatorInterface { * An array of active translators keyed by priority. * * @var array - * Array of Drupal\Core\Translation\Translator\TranslatorInterface objects + * Array of \Drupal\Core\Translation\Translator\TranslatorInterface objects */ protected $translators = array(); /** + * Holds the array of translators sorted by priority. + * + * If this is NULL a rebuild will be triggered. + * + * @var array + * An array of path processor objects. + * + * @see \Drupal\Core\StringTranslation\TranslationManager::addTranslator() + * @see \Drupal\Core\StringTranslation\TranslationManager::sortTranslators() + */ + protected $sortedTranslators = NULL; + + /** * The default langcode used in translations. * * @var string @@ -52,7 +65,8 @@ public function __construct() { */ public function addTranslator(TranslatorInterface $translator, $priority = 0) { $this->translators[$priority][] = $translator; - + // Reset sorted translators property to trigger rebuild. + $this->sortedTranslators = NULL; return $this; } @@ -76,7 +90,10 @@ protected function sortTranslators() { * {@inheritdoc} */ public function getStringTranslation($langcode, $string, $context) { - foreach ($this->sortTranslators() as $translator) { + if ($this->sortedTranslators === NULL) { + $this->sortedTranslators = $this->sortTranslators(); + } + foreach ($this->sortedTranslators as $translator) { $translation = $translator->getStringTranslation($langcode, $string, $context); if ($translation !== FALSE) { return $translation; @@ -140,7 +157,10 @@ public function setDefaultLangcode($langcode) { * {@inheritdoc} */ public function reset() { - foreach ($this->sortTranslators() as $translator) { + if ($this->sortedTranslators === NULL) { + $this->sortedTranslators = $this->sortTranslators(); + } + foreach ($this->sortedTranslators as $translator) { $translator->reset(); } }