The Translation Component
The Translation component provides tools to internationalize yourapplication.
Installation
- $ 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:
- use Symfony\Component\Translation\Translator;
- $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 Translator
class. 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:
ArrayLoader
- to loadcatalogs from PHP arrays.CsvFileLoader
- to loadcatalogs from CSV files.IcuDatFileLoader
- to loadcatalogs from resource bundles.IcuResFileLoader
- to loadcatalogs from resource bundles.IniFileLoader
- to loadcatalogs from ini files.MoFileLoader
- to loadcatalogs from gettext files.PhpFileLoader
- to loadcatalogs from PHP files.PoFileLoader
- to loadcatalogs from gettext files.QtFileLoader
- to loadcatalogs from QT XML files.XliffFileLoader
- to loadcatalogs from Xliff files.JsonFileLoader
- to loadcatalogs from JSON files.YamlFileLoader
- to loadcatalogs 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
:
- // ...
- $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:
- // ...
- $translator->addResource('array', [
- 'Hello World!' => 'Bonjour',
- ], '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:
- // ...
- $translator->addLoader('yaml', new YamlFileLoader());
- $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 calling
trans()
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 calling
setFallbackLocales()
:
- // ...
- $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:
- // ...
- $translator->addLoader('xlf', new XliffFileLoader());
- $translator->addResource('xlf', 'messages.fr.xlf', 'fr_FR');
- $translator->addResource('xlf', 'admin.fr.xlf', 'fr_FR', 'admin');
- $translator->addResource(
- 'xlf',
- 'navigation.fr.xlf',
- 'fr_FR',
- 'navigation'
- );
When translating strings that are not in the default domain (messages
),you must specify the domain as the third argument of trans()
:
- $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
- Adding Custom Format Support
- Create a Custom Message Formatter
- Using the Translator
- Translations
- How to Find Missing or Unused Translation Messages
- How to Find Errors in Translation Files
- How to Work with the User's Locale
- How to Translate Messages using the ICU MessageFormat
- Using Translation in Templates
- How to Translate Validation Constraint Messages