Install the Translation component
8.2To use the Translation component in your project, install it via Composer:
composer require symfony/translationrepository·8.2·Indexed 27 days ago
https://github.com/symfony/translationA component for internationalizing applications by managing and retrieving translated strings across different locales. It includes a Translator with support for various loaders, as well as CLI tools for linting translation and XLIFF files, pulling and pushing translations to providers, and checking translation status.
To use the Translation component in your project, install it via Composer:
composer require symfony/translationYou can initialize a Translator with a default locale, register a loader (such as ArrayLoader), and add translation resources. Use the trans() method to retrieve translated strings.
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\ArrayLoader;
$translator = new Translator('fr_FR');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', [
'Hello World!' => 'Bonjour !',
], 'fr_FR');
echo $translator->trans('Hello World!'); // outputs « Bonjour ! »The translation-status.php tool accepts the following options and arguments:
| Type | Option/Argument | Description |
|---|---|---|
| Option | --incomplete | Filters the output to only show locales that are incomplete or contain errors. |
| Option | -v | Enables verbose output, displaying the full list of missing translation IDs/content and specific mismatches between trans-unit IDs and source. |
| Argument | <locale> | A positional argument to specify a single locale to analyze (e.g., fr, en). |
translation:push command:The lint:xliff command accepts the following arguments and options:
Arguments
filename (array): A file, a directory, or - to read from STDIN.Options
--format (required): The output format. Supported values are txt, json, and github.The translation:pull command accepts the following arguments and options:
Arguments
provider: The provider to pull translations from. (Required if multiple providers exist, otherwise optional).Options
--force: Override existing translations with provider ones (this will delete messages not synchronized with the provider).--intl-icu: Used with --force. It writes messages in %domain%+intl-icu.%locale%.xlf files.--domains=VALUE: Specify the domains to pull (can be used multiple times).--locales=VALUE: Specify the locales to pull (can be used multiple times).--format=VALUE: Override the default output format. Supported formats: php, xlf, xlf12, xlf20, po, mo, yml, yaml, ts, csv, json, ini, res. (Default: xlf12).--as-tree=VALUE: Write messages as a tree-like structure. Requires --format=yaml. The value defines the level where to switch to inline YAML.The lint:translations command supports the following option:
--locale <locale>: Specify one or more locales to lint. This option accepts multiple values (array format).lint:translations command checks the syntax and validity of your translation files. It iterates through the provided locales and domains, attempting to translate each key to ensure no errors are encountered. If errors are found, it outputs a table showing the status of each locale and provides detailed error messages for specific invalid translation keys.The translation:push command pushes local translations to a configured remote provider. By default, only new translations are pushed and existing ones are not overwritten.
To perform more advanced synchronization, you can use the --force and --delete-missing flags.
The translation-status.php script is a CLI tool used to check the translation status of locales by comparing them against reference (original) files. It identifies missing translations and mismatches between trans-unit IDs and their source content.
Note: You must execute this command from the root directory of the Symfony code repository for it to locate the required reference files.
# show the translation status of all locales
$ php translation-status.php
# only show the translation status of incomplete or erroneous locales
$ php translation-status.php --incomplete
# show the translation status of all locales, all their missing translations and mismatches between trans-unit id and source
$ php translation-status.php -v
# show the status of a single locale
$ php translation-status.php fr
# show the status of a single locale, missing translations and mismatches between trans-unit id and source
$ php translation-status.php fr -vThe translation:pull command pulls translations from a specified provider. By default, it only pulls new translations and appends them to your local files without overwriting existing ones.
To overwrite existing local translations and remove messages that are no longer present in the provider, use the --force flag.
The lint:xliff command validates the syntax of XLIFF translation files and outputs any encountered errors. It can validate a single file, a whole directory, or read content from STDIN.
Validate a single file:
php bin/console lint:xliff filenameValidate a directory (recursively):
php bin/console lint:xliff dirnameValidate content from STDIN:
cat filename | php bin/console lint:xliff -