mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 07:42:48 +08:00
Improve performance of translation reference parsing
This commit is contained in:
parent
b4439dc6b3
commit
98ccfdcee5
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user