Repository: symfony/symfony
File: src/Symfony/Component/Translation/Translator.php (PHP)

Code before:
        $this->setLocale($locale);

        if (null === $formatter) {
            $formatter = new MessageFormatter();
        }

        $this->formatter = $formatter;
        $this->cacheDir = $cacheDir;
        $this->debug = $debug;
        $this->cacheVary = $cacheVary;
        $this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface;
        $this->fallbackLocaleProvider = $fallbackLocaleProvider ?? new FallbackLocaleProvider();
    }

    public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
    {
        $this->configCacheFactory = $configCacheFactory;
    }

    public function setFallbackLocaleProvider(FallbackLocaleProviderInterface $fallbackLocaleProvider)
    {
        $this->fallbackLocaleProvider = $fallbackLocaleProvider;

        // needed as the fallback locales are linked to the already loaded catalogues
        $this->catalogues = [];
        $this->cacheVary['fallback_locales'] = $fallbackLocaleProvider->getUltimateFallbackLocales();
    }

    public function getFallbackLocaleProvider(): FallbackLocaleProviderInterface
    {
        return $this->fallbackLocaleProvider;
    }

    /**
     * Adds a Loader.
     *
     * @param string $format The name of the loader (@see addResource())
     */
    public function addLoader(string $format, LoaderInterface $loader)
    {
        $this->loaders[$format] = $loader;
    }

    /**
     * Adds a Resource.
     *
     * @param string $format   The name of the loader (@see addLoader())
     * @param mixed  $resource The resource name
     *
     * @throws InvalidArgumentException If the locale contains invalid characters
     */

Review comment (performance):
I don't like this for the following reasons:

When, in general, a `FallbackLocaleProvider` is responsible for giving the sequence of locales to try for a given starting locale, we should not assume it has this list of "last resort" locales, and we should also not assume that this configurable setting is the only thing affecting the computation results.

It might not be relevant in practice, but what if the JSON file with the locale hierarchy changes?

If the `FallbackLocaleProvider` just provides something like a cache key/suffix or similar (not exposing the `ultimateFallbackLocales` list), what would that mean for the "fallback locale" information gathered in the `DataCollectorTranslator`?

Code after:
     * @var array<string, string|int|float>
     */
    private array $globalTranslatedParameters = [];

    /**
     * @throws InvalidArgumentException If a locale contains invalid characters
     */
    public function __construct(
        string $locale,
        ?MessageFormatterInterface $formatter = null,
        private ?string $cacheDir = null,
        private bool $debug = false,
        private array $cacheVary = [],
    ) {
        $this->setLocale($locale);

        $this->formatter = $formatter ??= new MessageFormatter();
        $this->hasIntlFormatter = $formatter instanceof IntlFormatterInterface;
        $this->localeFallbackProvider = new LocaleFallbackProvider();
    }

    public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory): void
    {
        $this->configCacheFactory = $configCacheFactory;
    }

    /**
     * Adds a Loader.
     *
     * @param string $format The name of the loader (@see addResource())
     */
    public function addLoader(string $format, LoaderInterface $loader): void
    {
        $this->loaders[$format] = $loader;
    }

    /**
     * Adds a Resource.
     *
     * @param string $format   The name of the loader (@see addLoader())
     * @param mixed  $resource The resource name
     *
     * @throws InvalidArgumentException If the locale contains invalid characters
     */
    public function addResource(string $format, mixed $resource, string $locale, ?string $domain = null): void
    {
        $domain ??= 'messages';

        $this->assertValidLocale($locale);
        $locale ?: $locale = class_exists(\Locale::class) ? \Locale::getDefault() : 'en';