Warning: You are browsing the documentation for Symfony 4.2, which is no longer maintained.

Read the updated version of this page for Symfony 5.3 (the current stable version).

Adding Custom Format Support

Adding Custom Format Support

Sometimes, you need to deal with custom formats for translation files. The Translation component is flexible enough to support this. Create a loader (to load translations) and, optionally, a dumper (to dump translations).

Imagine that you have a custom format where translation messages are defined using one line for each translation and parentheses to wrap the key and the message. A translation file would look like this:

(goodbye)(au revoir)

Creating a Custom Loader

To define a custom loader that is able to read these kinds of files, you must create a new class that implements the Symfony\Component\Translation\Loader\LoaderInterface. The load() method will get a filename and parse it into an array. Then, it will create the catalog that will be returned:

use Symfony\Component\Translation\Loader\LoaderInterface;
use Symfony\Component\Translation\MessageCatalogue;

class MyFormatLoader implements LoaderInterface
    public function load($resource, $locale, $domain = 'messages')
        $messages = [];
        $lines = file($resource);

        foreach ($lines as $line) {
            if (preg_match('/\(([^\)]+)\)\(([^\)]+)\)/', $line, $matches)) {
                $messages[$matches[1]] = $matches[2];

        $messageCatalogue = new MessageCatalogue($locale);
        $messageCatalogue->add($messages, $domain);

        return $messageCatalogue;


Once created, it can be used as any other loader:

use Symfony\Component\Translation\Translator;

$translator = new Translator('fr_FR');
$translator->addLoader('my_format', new MyFormatLoader());

$translator->addResource('my_format', __DIR__.'/translations/messages.txt', 'fr_FR');


It will print “accueil”.

Creating a Custom Dumper

It is also possible to create a custom dumper for your format, which is useful when using the extraction commands. To do so, a new class implementing the Symfony\Component\Translation\Dumper\DumperInterface must be created. To write the dump contents into a file, extending the Symfony\Component\Translation\Dumper\FileDumper class will save a few lines:

use Symfony\Component\Translation\Dumper\FileDumper;
use Symfony\Component\Translation\MessageCatalogue;

class MyFormatDumper extends FileDumper
    public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = [])
        $output = '';

        foreach ($messages->all($domain) as $source => $target) {
            $output .= sprintf("(%s)(%s)\n", $source, $target);

        return $output;

    protected function getExtension()
        return 'txt';

The formatCatalogue() method creates the output string, that will be used by the dump() method of the FileDumper class to create the file. The dumper can be used like any other built-in dumper. In the following example, the translation messages defined in the YAML file are dumped into a text file with the custom format:

use Symfony\Component\Translation\Loader\YamlFileLoader;

$loader = new YamlFileLoader();
$translations = $loader->load(__DIR__ . '/translations/messages.fr_FR.yaml' , 'fr_FR');

$dumper = new MyFormatDumper();
$dumper->dump($translations, ['path' => __DIR__.'/dumps']);

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.