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 child-container creation through getContainer() / requireContainer().
  • 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(['transport' => 'abc']))->requireContainer('transport');
} catch (\Throwable $e) {
    $exception = $e;
    echo $e->getMessage();
    // "The value of transport must be of type array; 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.

Reader and Selector Errors

Reads performed through getReader() execute in Derafu Array and use its translatable exceptions. Register Array’s resource provider to translate those messages; see Array Translations. Direct Value operations use Value’s provider, described in Value Translations.

Container retains its own exception classes for child creation. If the selected child value is not an array, UnexpectedTypeException preserves the Value mismatch as its previous exception. The outer Container message remains translated with Container’s provider. Selector failures remain Selector exceptions.

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 transport debe ser de tipo array; 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 09/11/2026 by Anonymous