<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of Contao Manager.
 *
 * (c) Contao Association
 *
 * @license LGPL-3.0-or-later
 */

namespace Contao\ManagerApi\I18n;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Yaml\Yaml;

class Translator
{
    /**
     * @var RequestStack
     */
    private $requestStack;

    /**
     * @var array
     */
    private $labels = [];

    /**
     * Constructor.
     */
    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    /**
     * Gets label for given ID from translation files.
     */
    public function trans(string $id, array $params = []): string
    {
        $locales = ['en'];

        if (null !== ($request = $this->requestStack->getCurrentRequest())) {
            $locale = $request->getLocale();

            if (5 === \strlen($locale)) {
                array_unshift($locales, substr($locale, 0, 2));
            }

            array_unshift($locales, $locale);
        }

        return $this->replaceParameters($this->findLabel($id, $locales), $params);
    }

    /**
     * Searches for label by ID in the given locales.
     */
    private function findLabel(string $id, array $locales): string
    {
        foreach ($locales as $locale) {
            $this->load($locale);

            if (isset($this->labels[$locale][$id])) {
                return $this->labels[$locale][$id];
            }
        }

        return $id;
    }<commit_msg>Correctly fall back to english for empty translation<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of Contao Manager.
 *
 * (c) Contao Association
 *
 * @license LGPL-3.0-or-later
 */

namespace Contao\ManagerApi\I18n;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Yaml\Yaml;

class Translator
{
    /**
     * @var RequestStack
     */
    private $requestStack;

    /**
     * @var array
     */
    private $labels = [];

    /**
     * Constructor.
     */
    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    /**
     * Gets label for given ID from translation files.
     */
    public function trans(string $id, array $params = []): string
    {
        $locales = ['en'];

        if (null !== ($request = $this->requestStack->getCurrentRequest())) {
            $locale = $request->getLocale();

            if (5 === \strlen($locale)) {
                array_unshift($locales, substr($locale, 0, 2));
            }

            array_unshift($locales, $locale);
        }

        return $this->replaceParameters($this->findLabel($id, $locales), $params);
    }

    /**
     * Searches for label by ID in the given locales.
     */
    private function findLabel(string $id, array $locales): string
    {
        foreach ($locales as $locale) {
            $this->load($locale);

            if (isset($this->labels[$locale][$id]) && ('' !== $this->labels[$locale][$id] || 'en' === $locale)) {
                return $this->labels[$locale][$id];
            }
        }

        return $id;
    }
{'fraction_non_alphanumeric': 0.15131370686926243, 'fraction_numerical': 0.005064893953782843, 'mean_word_length': 2.1133004926108376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '3791692', 'n_tokens_mistral': 965, 'n_tokens_neox': 929, 'n_words': 210}