New in Symfony 2.6: Improvements for the Translation component
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
The top languages used on Internet are English (800 million users), Chinese (650 million), Spanish (220 million) and Arabic (135 million). As companies become more global, odds are that you have to develop multilingual websites and applications. The good news is that Symfony 2.6 will include some improvements for the Translation component.
When executing the
translation:update command to generate the XLIFF file,
previous Symfony versions didn't set the correct
$ console translation:update --force --output-format=xlf fr AcmeBundle
The result of executing that command was a XLIFF file without the
<file source-language="fr" ... >
Symfony 2.6 will correctly set both the
source-language and the
<file source-language="en_US" target-language="fr_FR" ... >
In addition, these attributes will now use the full locale values (e.g.
en_US instead of
In case you want to use the same translation catalogue outside your application (e.g. use translation on a client side), Symfony 2.6 allows you to fetch the raw translation messages for the given locale.
$messages = $translator->getMessages('fr_FR');
The structure of the
$messages variable will be the following array:
1 2 3 4 5 6 7 8 9
array( 'messages' => array( 'Hello world' => 'Bonjour tout le monde', ), 'validators' => array( 'Value should not be empty' => 'Valeur ne doit pas être vide', 'Value is too long' => 'Valeur est trop long', ), );
Generally speaking, caching in Symfony is done within the components. However, the Translation component used the FrameworkBundle to cache its catalogues. This made cached translations hard to use outside Symfony, for instance in Silex projects.
In Symfony 2.6, the signature of the
Translator constructor has changed to
accept a third optional parameter that sets the directory where message catalogues
1 2 3 4 5 6 7 8 9 10 11
use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\MessageSelector; // Symfony 2.5 $translator = new Translator('fr_FR', new MessageSelector()); // Symfony 2.6 without caching enabled $translator = new Translator('fr_FR', new MessageSelector()); // Symfony 2.6 with caching enabled $translator = new Translator('fr_FR', new MessageSelector(), __DIR__.'/../cache');
One of the most important tasks of internationalizing applications is to detect
all the missing translations for any given locale. In Symfony 2.6 this task will
be much easier thanks to the new
logging option (its default value is
1 2 3 4 5
# app/config/config.yml translator: enabled: true fallback: en logging: true
When set to
true, whenever Symfony doesn't find a translation for the given
locale, the missing translation is added to the log file of the current environment.
translation:update command comes in very handy to extract translation
strings from the templates of a given bundle. It Symfony 2.6 this command will
also allow to extract strings from the global templates located in the
app/Resources/views/ directory. To do so, execute the command without
providing a bundle name:
# this only works in Symfony 2.6 $ console translation:update fr
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.