<commit_before><?php
/*
 * This file is part of Flarum.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Flarum\Locale;

use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\Translator as BaseTranslator;

class Translator extends BaseTranslator
{
    /**
     * {@inheritdoc}
     */
    public function getCatalogue($locale = null)
    {
        $catalogue = parent::getCatalogue($locale);

        foreach ($catalogue->all() as $domain => $messages) {
            foreach ($messages as $id => $translation) {
                $catalogue->set($id, $this->getTranslation($catalogue, $id, $domain), $domain);
            }
        }

        return $catalogue;
    }

    /**
     * @param MessageCatalogueInterface $messages
     * @param string $id
     * @param string $domain
     * @return string
     */
    private function getTranslation(MessageCatalogueInterface $messages, $id, $domain)
    {
        $translation = $messages->get($id, $domain);

        if (preg_match('/^=>\s*([a-z0-9_\-\.]+)$/i', $translation, $matches)) {
            return $this->getTranslation($messages, $matches[1], $domain);<commit_msg>Improve performance of translation reference parsing<commit_after><?php
/*
 * This file is part of Flarum.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Flarum\Locale;

use Symfony\Component\Translation\MessageCatalogueInterface;
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);

        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
     * @param string $domain
     * @return string
     */
    private function getTranslation(MessageCatalogueInterface $messages, $id, $domain)
    {
        $translation = $messages->get($id, $domain);

        if (preg_match(self::REFERENCE_REGEX, $translation, $matches)) {
            return $this->getTranslation($messages, $matches[1], $domain);
{'fraction_non_alphanumeric': 0.13803872579637727, 'fraction_numerical': 0.0018738288569643974, 'mean_word_length': 2.6943483275663205, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4791557', 'n_tokens_mistral': 924, 'n_tokens_neox': 903, 'n_words': 216}