« Back to all articles

How to use native Symfony Validator translations in a Silex application

10 October 2015

One of the problems I’ve encountered using the Symfony Translation component in Silex is that when using a fully qualified locale (e.g. en_GB vs en) it doesn’t use the translations that come shipped with the Validation component.

This is because they are named validators.en.xlf and not validators.en_GB.xlf.

The way around this is to simply load them into the correct domain when initialising the TranslationServiceProvider

    $app['locale'] = 'en_GB';
    $app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
        // load SF validation messages
        $translator->addLoader('xlf', new Symfony\Component\Translation\Loader\XliffFileLoader());
        $translator->addResource('xlf', __DIR__.'/../vendor/symfony/validator/Resources/translations/validators.fi.xlf', 'fi_FI', 'validators');
        $translator->addResource('xlf', __DIR__.'/../vendor/symfony/validator/Resources/translations/validators.en.xlf', 'en_GB', 'validators');
        // now load your own
        $translator->addLoader('yaml', new Symfony\Component\Translation\Loader\YamlFileLoader());
        $translator->addResource('yaml', __DIR__.'/../translations/messages.en_GB.yml', 'en_GB', 'messages');
        $translator->addResource('yaml', __DIR__.'/../translations/messages.fi_FI.yml', 'fi_FI', 'messages');
        return $translator;

The path to the translations may vary depending on which version of Symfony you are using.