ZF2 translator does not work in the controller?

I am trying to translate a ServiceLocator into a controller, but this is not a translation, and I have tried many sulotions in stackoverflow, but without success. My system uses several languages, and my goal is to use transtor in sight, controller, shape and filter. In my opinion, Tranlator works. Any sugestion and help would be appreciated.

Does not work in the controller:

 $this->getServiceLocator()->get('translator')->translate('my text',$myLocale); 

My mudole.config.php application:

 'service_manager' => array( 'abstract_factories' => array( 'Zend\Cache\Service\StorageCacheAbstractServiceFactory', 'Zend\Log\LoggerAbstractServiceFactory', ), 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', ), ), 'translator' => array( 'locale' => 'en_US',// 'locale' => 'dk_DK', 'translation_file_patterns' => array( array( 'type' => 'gettext', 'base_dir' => __DIR__ . '/../language', 'pattern' => '%s.mo', ), ), ), 

I changed the local file in the mudole.config.php file to another language, but did not translate.

+5
source share
4 answers

It seems that the locale is not set directly in the text of the translation, but through $this->getServiceLocator()->get('translator')->setLocale($locale) , and now it translates my text.

My mudole.config.php application:

  'service_manager' => array( 'factories' => array( 'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory', ), ), 'translator' => array( 'locale' => 'en_US', 'translation_file_patterns' => array( array( 'type' => 'gettext', 'base_dir' => __DIR__ . '/../language', 'pattern' => '%s.mo', ), ), ), 

And in the controller:

 $this->getServiceLocator()->get('translator')->setLocale($locale); echo $c=$this->getServiceLocator()->get('translator')->translate('Book'); // Print(Danish): Bog 
+2
source

View Helper / Forms

ZF2 comes with an assistant like Zend\I18n\View\Helper\Translate ; so you can use the $this->translate($text) method in the view.

However, all auxiliary view classes that extend from Zend\I18n\View\Helper\AbstractTranslatorHelper (which includes all form view helpers) are also "translatable".

You will need to pass to the translator using $viewHelper->setTranslator($translator) and enable the translation through $viewHelper->setTranslatorEnabled(true) .

Controller plugin

Unfortunately, there is no default plugin (which I could find) for processing translators in controllers; I think you could argue that the text content should not be in the controller.

You can easily create one like the example below. The key is to transfer your new translator service as a dependency through the factory.

 namespace MyModule\Controller\Plugin; use Zend\Mvc\Controller\AbstractPlugin; use Zend\I18n\Translator\Translator as TranslatorService; class Translator extends AbstractPlugin { protected $translatorService; public function __construct(TranslatorService $translatorService) { $this->translatorService = $translatorService; } public function invoke($text = null, array $options = []) { if (null == $text) { return $this; } return $this->translate($text, $options); } public function translate($text, array $options = []) { return $this->translatorService->translate($text); } } 

And create a factory class.

 namespace MyModule\Controller\Plugin; use MyModule\Controller\Plugin\Translator; use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\FactoryInterface; class TranslatorFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $controllerPluginManager) { $serviceManager = $controllerPluginManager->getServiceLocator(); return new Translator($serviceManager->get('translator')); } } 

Register the service in module.config.php .

 return [ 'controller_plugins' => [ 'factories' => [ 'translate' => 'MyModule\\Controller\\Plugin\\TranslateFactory', ] ], ]; 

Then you can just call it in the controller class.

 // Directly $this->translate($text, $options); // Or fetch the plugin first $this->translate()->translate($text, $options); 
+3
source

You can use the ZfTranslate controller plugin .

Installation

composer require mikica/zf2-translate-plugin

You need to register a new module. Add to config / application.config.php file:

 'modules' => array( '...', 'ZfTranslate' ), 

Use in the controller

 <?php $this->translate('translate word'); $this->translate('translate word', 'locale'); 
+1
source

AlexP's answer is the best way to do this.

But the question remains, why is your method not working?

It should work. But this is not because you are in different namespaces, so you use different domains among the files. You are doing something like this:

 namespace MyModule\Controller; class MyController { public function someAction() { $this->getServiceLocator()->get('translator')->translate('my text',__NAMESPACE__,$myLocale); } } 

In your `module.config.php ', you probably use this namespace:

 namespace MyModule; return array( //... 'translator' => array( //... ), ); 

Note that in the controller example, __NAMESPACE__ is equal to MyModule\Controller . Although in the configuration file, __NAMESPACE__ is equal to MyModule . You need to fix this by passing the same value in both cases.

In other words, there are several approaches to solving this problem, for example, AlexP. But, if you configure it, any of them must have a translator domain (the key value is 'text_domain' ), if you configure it, it is equal to the domain parameter (second parameter) of the translate method when you call it.

A faster solution changes the $ domain parameter to a line in the controller file:

 $this->getServiceLocator()->get('translator')->translate('my text','MyModule',$myLocale); 

Another solution should be to create a constant and use it in files (controllers, views and settings).

0
source

Source: https://habr.com/ru/post/1235702/


All Articles