I'm trying to register a custom database translator loader.
For that i was inspired by: Feeding Zend Translator
I have the following facotry code in (module.config.php):
'service_manager' => array(
'factories' => array(
'translator' => function($sm){
$translator = new \V1\Service\DatabaseTranslationService();
return $translator->createService($sm);
},
),
),
The DatabaseTranslationService looks like that:
$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 seems like "setInvokableClass" isn't used: I got this error:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for database
Does anybody know how to registering the Translator correctly
After two days of search I've found a solution.
I don't know if it's 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.
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',
),
),
)
The YourNamespace\I18n\Translator\Loader\YourCustomFormat
must implement the Zend\I18n\Translator\Loader\FileLoaderInterface
interface; the load($locale, $filename)
method must return a Zend\I18n\Translator\TextDomain
instance.
This worked for me.
(of course the autoloader must find the class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With