Symfony News

New in Symfony 2.8: Translator improvements

Added XLIFF 2.0 support

Contributed by
Abdellatif Ait boudad
in #15717.

XLIFF is the format recommended by Symfony to create the translation files used to internationalize your web sites and applications. Before Symfony 2.8, the only XLIFF version supported by Symfony was 1.2, a standard which was published back on February 2008.

Starting from Symfony 2.8, we also support XLIFF 2.0, the most recent version of the standard and which was published on August 2014. Given the complexity of the XLIFF standard, we only support a subset a features which allow to parse and dump XLIFF 2.0 files.

When dumping a translation catalogue, use the new xliff_version option to set the XLIFF version to 2.0. Otherwise, it will be dumped as XLIFF 1.2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Dumper\XliffFileDumper;

$catalogue = new MessageCatalogue('en_US');
$catalogue->add([
    'foo' => 'bar',
]);

// ...

$dumper = new XliffFileDumper();
$dumper->dump($catalogue, ['xliff_version' => '2.0', ...]);

When loading catalogues, Symfony detects the version automatically, so you just need to create the XLIFF file using the 2.0 format:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0"
       version="2.0" srcLang="en-US" trgLang="ja-JP">
    <file id="f1" original="Graphic Example.psd">
        <skeleton href="Graphic Example.psd.skl"/>
        <group id="1">
            <unit id="1">
                <segment>
                    <source>foo</source>
                    <target>XLIFF 文書を編集、または処理 するアプリケーションです。</target>
                </segment>
            </unit>
        </group>
    </file>
</xliff>

Allow to dump catalogues without writing them in files

Contributed by
Abdellatif Ait boudad
in #15786.

In Symfony 2.7 and previous versions, when you wanted to transform a translation catalogue into some format (for example Yaml), you needed to dump the contents into a file and read them back from that file.

In Symfony 2.8, FileDumper class includes a formatCatalogue() method which transforms the catalogue contents into the given format and returns the result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Dumper\YamlFileDumper;

$catalogue = new MessageCatalogue('en');
$catalogue->add(
    'foo.bar' => 'value',
));

// BEFORE
$dumper = new YamlFileDumper();
$tempDir = sys_get_temp_dir();
$dumper->dump($catalogue, ['path' => $tempDir, 'as_tree' => true, 'inline' => 999]);
$formattedCatalogue = file_get_contents($tempDir);

// AFTER
$formattedCatalogue = $dumper->formatCatalogue(
    $catalogue, 'messages', ['as_tree' => true, 'inline' => 999]
);

Add option to specify additional translation loading paths

Contributed by
Jordi Boggiano in #14561.

In Symfony applications, translation files names and locations follow a strict convention. If you need to load files from other locations, you may need to create a compiler pass to inject your files into the translator service paths.

In Symfony 2.8, the translator service defines a new option called paths which allows to define an array of paths were the component will look for translation files:

1
2
3
4
# app/config/config.yml
translator:
    fallback: "%locale%"
    paths: ['%kernel.root_dir%/../vendor/internal/package/translations']

Be trained by Symfony experts - 2016-03-07 Clichy - 2016-03-07 Clichy - 2016-03-07 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook