Warning: You are browsing the documentation for Symfony 2.4, which is no longer maintained.
Read the updated version of this page for Symfony 6.0 (the current stable version).
Table of Contents
The Translation component provides tools to internationalize your application.
You can install the component in 2 different ways:
The main access point of the Translation component is Translator. Before you can use it, you need to configure it and load the messages to translate (called message catalogs).
The constructor of the
Translator class needs one argument: The locale.
1 2 3 4
use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\MessageSelector; $translator = new Translator('fr_FR', new MessageSelector());
The locale set here is the default locale to use. You can override this locale when translating strings.
The term locale refers roughly to the user's language and country. It
can be any string that your application uses to manage translations and
other format differences (e.g. currency format). The ISO 639-1
language code, an underscore (
_), then the ISO 3166-1 alpha-2
country code (e.g.
fr_FR for French/France) is recommended.
The messages are stored in message catalogs inside the
class. A message catalog is like a dictionary of translations for a specific
The Translation component uses Loader classes to load catalogs. You can load multiple resources for the same locale, which will then be combined into one catalog.
New in version 2.4
JsonFileLoader was introduced in Symfony 2.4.
The component comes with some default Loaders and you can create your own Loader too. The default loaders are:
- ArrayLoader - to load catalogs from PHP arrays.
- CsvFileLoader - to load catalogs from CSV files.
- IcuDatFileLoader - to load catalogs from resource bundles.
- IcuResFileLoader - to load catalogs from resource bundles.
- IniFileLoader - to load catalogs from ini files.
- MoFileLoader - to load catalogs from gettext files.
- PhpFileLoader - to load catalogs from PHP files.
- PoFileLoader - to load catalogs from gettext files.
- QtFileLoader - to load catalogs from QT XML files.
- XliffFileLoader - to load catalogs from Xliff files.
- JsonFileLoader - to load catalogs from JSON files.
- YamlFileLoader - to load catalogs from Yaml files (requires the Yaml component).
All file loaders require the Config component.
You can also create your own Loader, in case the format is not already supported by one of the default loaders.
At first, you should add one or more loaders to the
// ... $translator->addLoader('array', new ArrayLoader());
The first argument is the name to which you can refer the loader in the translator and the second argument is an instance of the loader itself. After this, you can add your resources using the correct loader.
Loading messages can be done by calling
addResource(). The first
argument is the loader name (this was the first argument of the
method), the second is the resource and the third argument is the locale:
1 2 3 4
// ... $translator->addResource('array', array( 'Hello World!' => 'Bonjour', ), 'fr_FR');
If you use one of the file loaders, you should also use the
method. The only difference is that you should put the file name to the resource
file as the second argument, instead of an array:
1 2 3
// ... $translator->addLoader('yaml', new YamlFileLoader()); $translator->addResource('yaml', 'path/to/messages.fr.yml', 'fr_FR');
To actually translate the message, the Translator uses a simple process:
- A catalog of translated messages is loaded from translation resources defined
fr_FR). Messages from the The Translation Component are also loaded and added to the catalog, if they don't already exist. The end result is a large "dictionary" of translations;
- If the message is located in the catalog, the translation is returned. If not, the translator returns the original message.
If the message is not located in the catalog of the specific locale, the
translator will look into the catalog of one or more fallback locales. For
example, assume you're trying to translate into the
- First, the translator looks for the translation in the
- If it wasn't found, the translator looks for the translation in the
- If the translation still isn't found, the translator uses the one or more fallback locales set explicitly on the translator.
For (3), the fallback locales can be set by calling setFallbackLocale():
// ... $translator->setFallbackLocale(array('en'));
As you've seen, message files are organized into the different locales that they translate. The message files can also be organized further into "domains".
The domain is specified in the fourth argument of the
method. The default domain is
messages. For example, suppose that, for
organization, translations were split into three different domains:
navigation. The French translation would be
loaded like this:
1 2 3 4 5 6 7 8 9 10 11
// ... $translator->addLoader('xliff', new XliffLoader()); $translator->addResource('xliff', 'messages.fr.xliff', 'fr_FR'); $translator->addResource('xliff', 'admin.fr.xliff', 'fr_FR', 'admin'); $translator->addResource( 'xliff', 'navigation.fr.xliff', 'fr_FR', 'navigation' );
When translating strings that are not in the default domain (
you must specify the domain as the third argument of
$translator->trans('Symfony is great', array(), 'admin');
Symfony will now look for the message in the
admin domain of the