Symfony Translation Component

repository·8.2·Indexed 27 days ago

https://github.com/symfony/translation

A 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.

Tokens
1.6K
Snippets
3
Records
13
Agent score
92%

What's inside symfony/translation

  1. Quickstart using the Translator with an ArrayLoader

    8.2

    You 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 ! »
  2. Reference translation-status.php options and arguments

    8.2

    The translation-status.php tool accepts the following options and arguments:

    TypeOption/ArgumentDescription
    Option--incompleteFilters the output to only show locales that are incomplete or contain errors.
    Option-vEnables 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).
  3. Reference: translation:pull arguments and options

    8.2

    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.
  4. Lint translation files with lint:translations

    8.2
    The 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.
  5. Use the translation-status.php CLI tool

    8.2

    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 -v
  6. Pull translations with translation:pull

    8.2

    The 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.

  7. Lint XLIFF files with lint:xliff

    8.2

    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.

    Usage

    Validate a single file:

    php bin/console lint:xliff filename

    Validate a directory (recursively):

    php bin/console lint:xliff dirname

    Validate content from STDIN:

    cat filename | php bin/console lint:xliff -