Improve performance of translation reference parsing

This commit is contained in:
Toby Zerner 2015-11-02 23:22:00 +10:30
parent b4439dc6b3
commit 98ccfdcee5

View File

@ -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);
}