PhpStorm 2018.2 Help

Working with 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.

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

    ps 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 (for Windows and Linux) or ⌘+Click (for macOS) 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 pop-up menu.

ps symfony translation navigate domain declaration

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.

ps symfony translation missing

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.

ps symfony add translation quick fix

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 simply select a word and create a translation out of it.

To extract a translation of a word

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

    ps symfony extract translation context menu
  2. In the Extract Translation Key dialog that opens, provide a meaningful name to the translation key.

    ps symfony 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.

    ps symfony extract translation template modified

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

Last modified: 18 September 2018