Registering the Translator Custom Loader in Zend Framework 2

I am trying to register a custom database loader.

For this, I was inspired by: Submission of the Zend Translator

I have the following factual code in (module.config.php):

'service_manager' => array( 'factories' => array( 'translator' => function($sm){ $translator = new \V1\Service\DatabaseTranslationService(); return $translator->createService($sm); }, ), ), 

DatabaseTranslationService looks like this:

 $config = $serviceLocator->get('Config'); $trConfig = isset($config['translator']) ? $config['translator'] : array(); $translator = new \Zend\I18n\Translator\Translator(); $translator->getPluginManager()->setInvokableClass('database', '\Foo\I18n\Translator\Loader\DatabaseTranslator', true); $translator->addTranslationFile('database', 'en_EN'); return $translator; 

But it looks like "setInvokableClass" is not used: I got this error:

 Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for database 

Does anyone know how to correctly register a translator

+4
source share
2 answers

After two days of searching, I found a solution.

I don't know if this is a good solution, but it works for me.

Replace the line:

 $translator->getPluginManager()->setInvokableClass('database', '\Foo\I18n\Translator\Loader\DatabaseTranslator', true); 

with

 $viewHelper = $serviceLocator->get('viewHelperManager'); $viewHelper->setInvokableClass('database', '\Foo\I18n\Translator\Loader\DatabaseTranslator', true); 

I hope this solution helps you.

+2
source

In the current version (zf2 2.2.4) you only need to change the type -config entry:

 'translator' => array( 'locale' => 'de_DE', 'translation_file_patterns' => array( array( 'type' => 'YourNamespace\I18n\Translator\Loader\YourCustomFormat', 'base_dir' => __DIR__ . '/../language', 'pattern' => '%s.whatever', ), ), ) 

YourNamespace\I18n\Translator\Loader\YourCustomFormat should implement the Zend\I18n\Translator\Loader\FileLoaderInterface ; The load($locale, $filename) method should return an instance of Zend\I18n\Translator\TextDomain .

It worked for me.

(of course, the autoloader must find the class)

+2
source

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


All Articles