Sometimes in our Zend Framework 3 multi-language application, we could need to access to Zend Translator component directly from Controllers. For example, when we have to translate a string to return in a JsonModel.
So, let’s see how to write a very simple Controller Plugin that will help us to save a lot of code (and time).
First of all, we have to write our plugin that will take Zend Translator object in constructor and will wrap its translate() method. So let’s add following file module/Application/src/Controller/Plugin/TranslatePlugin.php:
Now, we need a Factory class to initialize our plugin and inject in it the Zend Translator component. That class will be placed in module/Application/src/Controller/Plugin/Factory/TranslatorPluginFactory.php and will be something like: