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.
Install the required plugins
This functionality relies on the PHP, Symfony Support, and PHP Annotations plugins that should be installed and enabled in your IDE.
The plugins are available only in IntelliJ IDEA Ultimate.
Press Control+Alt+S to open the IDE settings and select
.Switch to the Marketplace tab and use the search field to find the plugins.
Click Install next to each plugin and restart the IDE if prompted.
Code completion for translations or domains
You can query the Translator service for specific messages by key and domain. In the editor, press Control+Space to invoke code completion and do any of the following:
Provide a key, which determines the translation to fetch.
Provide a domain, which determines the resource file to fetch the translation from.
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 Control+B. Alternatively, Control+LeftClick 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.
Missing Translation or Domain inspection
When you try to use a translation key or domain that does not exist, IntelliJ IDEA 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.
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.
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
Right-click a word in a template and select Extract Translation from context menu:
In the Extract Translation Key dialog that opens, provide a meaningful name to the translation key.
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.
Click OK to apply your changes. The view will be updated with the correct syntax, making use of the correct translation key and domain.
The translation itself will be added to the selected resource file(s).