From ec7db5e8b4699fad6082ba32951185c198a59eeb Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 2 Nov 2015 23:22:00 +1030 Subject: [PATCH] Improve performance of translation reference parsing --- framework/core/src/Locale/Translator.php | 32 ++++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/framework/core/src/Locale/Translator.php b/framework/core/src/Locale/Translator.php index a6b936b91..22af2565c 100644 --- a/framework/core/src/Locale/Translator.php +++ b/framework/core/src/Locale/Translator.php @@ -15,22 +15,44 @@ use Symfony\Component\Translation\Translator as BaseTranslator; class Translator extends BaseTranslator { + const REFERENCE_REGEX = '/^=>\s*([a-z0-9_\-\.]+)$/i'; + /** * {@inheritdoc} */ public function getCatalogue($locale = null) { + if (null === $locale) { + $locale = $this->getLocale(); + } else { + $this->assertValidLocale($locale); + } + + $parse = !isset($this->catalogues[$locale]); + $catalogue = parent::getCatalogue($locale); - foreach ($catalogue->all() as $domain => $messages) { - foreach ($messages as $id => $translation) { - $catalogue->set($id, $this->getTranslation($catalogue, $id, $domain), $domain); - } + if ($parse) { + $this->parseCatalogue($catalogue); } return $catalogue; } + /** + * @param MessageCatalogueInterface $catalogue + */ + private function parseCatalogue(MessageCatalogueInterface $catalogue) + { + foreach ($catalogue->all() as $domain => $messages) { + foreach ($messages as $id => $translation) { + if (preg_match(self::REFERENCE_REGEX, $translation, $matches)) { + $catalogue->set($id, $this->getTranslation($catalogue, $id, $domain), $domain); + } + } + } + } + /** * @param MessageCatalogueInterface $messages * @param string $id @@ -41,7 +63,7 @@ class Translator extends BaseTranslator { $translation = $messages->get($id, $domain); - if (preg_match('/^=>\s*([a-z0-9_\-\.]+)$/i', $translation, $matches)) { + if (preg_match(self::REFERENCE_REGEX, $translation, $matches)) { return $this->getTranslation($messages, $matches[1], $domain); }