Translations

The exceptions of derafu/container can be translated. This page explains what is translatable, what ships out of the box and how to activate it in your application.

What’s Translatable

Every exception the package throws is translatable (see derafu/translation):

  • MissingValueException and UnexpectedTypeException, from the typed getters.
  • The errors of a Store (data that does not comply with the JSON schema, data or schema that can not be converted to JSON).
  • The errors of the schema of a Vault.
  • A value that can not be used as the key of a container.

The errors of the data of a Vault are the exceptions of symfony/options-resolver, which are not translatable.

Messages are written in ICU MessageFormat, with the English text as the translation id and named placeholders:

use Derafu\Container\Bag;

try {
    (new Bag(['port' => 'abc']))->requireInt('port');
} catch (\Throwable $e) {
    echo $e->getMessage();
    // "The value of port must be of type int; string given."
}

Without a translator configured, getMessage() already returns the formatted English text, so the package works as before whether or not you set up translation.

What Ships

  • resources/translations/errors+intl-icu.es.php: a Spanish translation for every message of the package.
  • Derafu\Container\Translation\ContainerTranslationResourceProvider: a TranslationResourceProviderInterface pointing at that directory.
  • resources/config/container-services.yaml: a recipe for applications that use symfony/dependency-injection.

None of them does anything by itself: derafu/container is a library, it does not build a Translator.

Activating It (Plain PHP)

use Derafu\Container\Translation\ContainerTranslationResourceProvider;
use Derafu\Translation\TranslatorFactory;

$translator = TranslatorFactory::create(
    defaultLocale: 'es',
    fallbackLocales: ['es', 'en'],
    resourceProviders: [new ContainerTranslationResourceProvider()],
);

echo $exception->trans($translator);
// "El valor de port debe ser de tipo int; se recibió string."

Activating It (Dependency Injection)

imports:
    - { resource: '../vendor/derafu/translation/resources/config/translation-services.yaml' }
    - { resource: '../vendor/derafu/container/resources/config/container-services.yaml' }

services:
    Symfony\Component\Translation\Translator:
        factory: ['Derafu\Translation\TranslatorFactory', 'create']
        arguments:
            $defaultLocale: 'es'
            $fallbackLocales: ['es', 'en']
            $resourceProviders: !tagged_iterator derafu_translation.resource_provider

container-services.yaml tags ContainerTranslationResourceProvider with derafu_translation.resource_provider, so it is picked up automatically.

On this page

Last updated on 20/10/2026 by Anonymous