Working with Translations
Depending on the types of applications we build, we may have to work with Symfony2's Translation component to abstract strings and locale-specific pieces into resources/files that can provide the correct data for a specific locale.
The Symfony2 Plugin helps us with "internationalization" (or i18n) by providing completion for known translations and domains, and will warn us whenever we're using a translation that does not exist. And to make things really easy, we can extract translations from within our controllers and templates.
Completion and Navigation for Translation or Domain
We can query the Translator service for specific messages by key and domain. The key determines the translation to fetch; the domain determines which resource file to fetch from. PhpStorm provides completion (Ctrl+Space) for both, in PHP code as well as Twig templates.
Everywhere we are using translations, we can hover over the key or domain and Ctrl+Click (or CMD+Click on Mac OS X) to navigate, or place the cursor on the key or domain and press Ctrl+B (or CMD+B on Mac OS X). Applications that require translations typically have multiple resources for every locale. PhpStorm will show a popup that allows us to pick which declaration of a key or domain we want to navigate to.
Missing Translation or Domain Inspection and Quick Fix
When we try to use a translation key or domain that does not exist, we will get a warning from the Symfony2 Plugin's "Missing Translation" inspection. This way, we know when we have to supply a translation in one of our resource files. It also reduces the risk of typos.
For translation keys, a Quick-Fix is provided that can create the translation key for us in the resource file. Place the cursor on the key and press Alt+Enter to invoke it.
Once invoked, the translation key will show up in the resource file where we can provide the correct value for it.
Creating translations can be a tedious job: writing the code making use of the Translator service and then creating the translation in a resource file. Thanks to the Quick-Fix mentioned before, we can speed up this process a little. But what if we had a tool at hand where we could just select a word and create a translation out of it?
We can do this by placing our text cursor on a word in a template and using Extract Translation in the context menu:
This opens up a dialog where we can give a meaningful name to our translation that will be used as the translation key. We can also pick the resource file(s) in which we want to create the translation and optionally select the check-box to open the first resource file once extraction is complete. Click OK to continue.
Once completed, the tool will update the view with the correct syntax, making use of the correct translation key and domain.
The translation itself will also be added to the resource file(s) selected in the dialog.