mirror of
https://github.com/flarum/core.git
synced 2025-07-23 09:41:26 +02:00
Improve performance of translation reference parsing
This commit is contained in:
@@ -15,22 +15,44 @@ use Symfony\Component\Translation\Translator as BaseTranslator;
|
|||||||
|
|
||||||
class Translator extends BaseTranslator
|
class Translator extends BaseTranslator
|
||||||
{
|
{
|
||||||
|
const REFERENCE_REGEX = '/^=>\s*([a-z0-9_\-\.]+)$/i';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getCatalogue($locale = null)
|
public function getCatalogue($locale = null)
|
||||||
{
|
{
|
||||||
|
if (null === $locale) {
|
||||||
|
$locale = $this->getLocale();
|
||||||
|
} else {
|
||||||
|
$this->assertValidLocale($locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
$parse = !isset($this->catalogues[$locale]);
|
||||||
|
|
||||||
$catalogue = parent::getCatalogue($locale);
|
$catalogue = parent::getCatalogue($locale);
|
||||||
|
|
||||||
foreach ($catalogue->all() as $domain => $messages) {
|
if ($parse) {
|
||||||
foreach ($messages as $id => $translation) {
|
$this->parseCatalogue($catalogue);
|
||||||
$catalogue->set($id, $this->getTranslation($catalogue, $id, $domain), $domain);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $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 MessageCatalogueInterface $messages
|
||||||
* @param string $id
|
* @param string $id
|
||||||
@@ -41,7 +63,7 @@ class Translator extends BaseTranslator
|
|||||||
{
|
{
|
||||||
$translation = $messages->get($id, $domain);
|
$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);
|
return $this->getTranslation($messages, $matches[1], $domain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user