PhpStorm 2021.2 Help

Translations

Depending on the types of an application being developed, you may have to work with the Symfony's Translation component to abstract strings and locale-specific pieces into resources/files that provide the correct data for a specific locale.

The Symfony Plugin assists with internationalization (commonly abbreviated as i18n) by providing completion for known translations and domains. It warns whenever a non-existing translation is used. Additionally, it lets extract translations from within controllers and templates.

Note that for these features to work, the Translator service for the Symfony application needs to be enabled.

Code completion for translations or domains

You can query the Translator service for specific messages by key and domain. In the editor, press Ctrl+Space to invoke code completion and do any of the following:

  • Provide a key, which determines the translation to fetch.

    Symfony translation key completion
  • Provide a domain, which determines the resource file to fetch the translation from.

    Symfony translation domain completion

Completion for keys and domains is available in both PHP code and Twig templates.

Code navigation for translations or domains

To navigate to the declaration of a key or a domain, position the caret at its usage and press Ctrl+B. Alternatively, Ctrl+Click the usage.

Applications that require translations typically have multiple resources for every locale. In this case, choose a specific declaration to navigate to from the popup menu.

Navigate to Symfony translation domain

Missing Translation or Domain inspection

When you try to use a translation key or domain that does not exist, PhpStorm displays the corresponding warning from the Missing Translation inspection. This way, you know when a translation needs to be supplied in one of the resource files.

Symfony Missing translation inspection

For translation keys, a quick-fix is available, which can create the corresponding translation key in the resource file. To apply it, position the caret at the key, press Alt+Enter, and choose Symfony: Add translations.

Add Symfony translation via the quickfix

The translation key will be added to resource file where you can provide the correct value for it.

Extracting translations

Creating translations requires writing the code making use of the Translator service and then creating the translation in a resource file. To speed up this process, the Symfony Plugin provides the Extract Translation action, which lets you select a word and create a translation out of it.

Extract a word translation

  1. Right-click a word in a template and select Extract Translation from context menu:

    Extract Symfony translation
  2. In the Extract Translation Key dialog that opens, provide a meaningful name to the translation key.

    the Extract Translation dialog

    You can also pick the resource file(s) in which the translation needs to be creates and optionally select the checkbox to open the resource file once extraction is complete.

  3. Click OK to apply your changes. The view will be updated with the correct syntax, making use of the correct translation key and domain.

    Template with the translation extracted

    The translation itself will be added to the selected resource file(s).

Last modified: 08 March 2021