The Translation Component

The Translation component provides tools to internationalize yourapplication.

Installation

  1. $ composer require symfony/translation

Note

If you install this component outside of a Symfony application, you mustrequire the vendor/autoload.php file in your code to enable the classautoloading mechanism provided by Composer. Readthis article for more details.

This article explains how to use the Translation features as an independentcomponent in any PHP application. Read the Translations article tolearn about how to internationalize and manage the user locale in Symfonyapplications.

Constructing the Translator

The main access point of the Translation component isTranslator. Before you can use it,you need to configure it and load the messages to translate (called messagecatalogs).

Configuration

The constructor of the Translator class needs one argument: The locale:

  1. use Symfony\Component\Translation\Translator;
  2.  
  3. $translator = new Translator('fr_FR');

Note

The locale set here is the default locale to use. You can override thislocale when translating strings.

Note

The term locale refers roughly to the user's language and country. Itcan be any string that your application uses to manage translations andother format differences (e.g. currency format). The ISO 639-1language code, an underscore (), then the ISO 3166-1 alpha-2_country code (e.g. fr_FR for French/France) is recommended.

Loading Message Catalogs

The messages are stored in message catalogs inside the Translatorclass. A message catalog is like a dictionary of translations for a specificlocale.

The Translation component uses Loader classes to load catalogs. You can loadmultiple resources for the same locale, which will then be combined into onecatalog.

The component comes with some default Loaders and you can create your ownLoader too. The default loaders are:

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:

  1. // ...
  2. $translator->addLoader('array', new ArrayLoader());

The first argument is the name to which you can refer the loader in thetranslator and the second argument is an instance of the loader itself. Afterthis, you can add your resources using the correct loader.

Loading Messages with the ArrayLoader

Loading messages can be done by callingaddResource(). The firstargument is the loader name (this was the first argument of the addLoader()method), the second is the resource and the third argument is the locale:

  1. // ...
  2. $translator->addResource('array', [
  3. 'Hello World!' => 'Bonjour',
  4. ], 'fr_FR');

Loading Messages with the File Loaders

If you use one of the file loaders, you should also use the addResource()method. The only difference is that you should put the file name to the resourcefile as the second argument, instead of an array:

  1. // ...
  2. $translator->addLoader('yaml', new YamlFileLoader());
  3. $translator->addResource('yaml', 'path/to/messages.fr.yaml', 'fr_FR');

The Translation Process

To actually translate the message, the Translator uses the following process:

  • A catalog of translated messages is loaded from translation resources definedfor the locale (e.g. fr_FR). Messages from theFallback Locales are also loaded and added to thecatalog, 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. Ifnot, the translator returns the original message.You start this process by callingtrans() ortransChoice(). Then, theTranslator looks for the exact string inside the appropriate message catalogand returns it (if it exists).

Fallback Locales

If the message is not located in the catalog of the specific locale, thetranslator will look into the catalog of one or more fallback locales. Forexample, assume you're trying to translate into the es_AR locale:

  • First, the translator looks for the translation in the es_AR(Argentinean Spanish) locale;
  • If it wasn't found, the translator looks for the translation in the parentlocale, which is automatically defined only for some locales. In thisexample, the parent locale is es_419 (Latin American Spanish);
  • If it wasn't found, the translator looks for the translation in the es(Spanish) locale;
  • If the translation still isn't found, the translator uses the one or morefallback locales set explicitly on the translator.For (3), the fallback locales can be set by callingsetFallbackLocales():
  1. // ...
  2. $translator->setFallbackLocales(['en']);

Using Message Domains

As you've seen, message files are organized into the different locales thatthey translate. The message files can also be organized further into "domains".

The domain is specified in the fourth argument of the addResource()method. The default domain is messages. For example, suppose that, fororganization, translations were split into three different domains:messages, admin and navigation. The French translation would beloaded like this:

  1. // ...
  2. $translator->addLoader('xlf', new XliffFileLoader());
  3.  
  4. $translator->addResource('xlf', 'messages.fr.xlf', 'fr_FR');
  5. $translator->addResource('xlf', 'admin.fr.xlf', 'fr_FR', 'admin');
  6. $translator->addResource(
  7. 'xlf',
  8. 'navigation.fr.xlf',
  9. 'fr_FR',
  10. 'navigation'
  11. );

When translating strings that are not in the default domain (messages),you must specify the domain as the third argument of trans():

  1. $translator->trans('Symfony is great', [], 'admin');

Symfony will now look for the message in the admin domain of thespecified locale.

Usage

Read how to use the Translation component in Using the Translator.

Learn More