Question

Good evening,

I want to add zend/translate to my project to show my webside in several languages. But there doesn't work anything. Here are the steps I done already:

In the module.config.php I looked if the translator is initialized:

...

'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',

...

'translator' => array(
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),

...

In the Module.php in the Bootstrap I set the DefaultTranslator:

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    \Locale::setDefault('de_DE');
    \Zend\Validator\AbstractValidator::setDefaultTranslator(
            $e->getApplication()
                    ->getServiceManager()
                    ->get('translator')
    );
}

But when I reload my webside there is a error like this:

 Catchable fatal error: Argument 1 passed to Zend\Validator\AbstractValidator::setDefaultTranslator() must be an instance of Zend\Validator\Translator\TranslatorInterface, instance of Zend\I18n\Translator\Translator given, called in C:\xampp\htdocs\pimp\module\Application\Module.php on line 28 and defined in C:\xampp\htdocs\pimp\vendor\zendframework\zendframework\library\Zend\Validator\AbstractValidator.php on line 472

I think there is something I've forgotten..

Can someone help me?

Thanks.

Was it helpful?

Solution

There is a odd part in Zend i18n that Zend\I18n\Translator\Translator is not compatible with other components. The Mvc Translator binds the Translator to the other parts. The Zend\Mvc\I18n\Translator extends the "normal" translator and then it implements the translator interface requested by the Zend\Validator component.

So, create the Mvc Translator by using the MVC translator factory. Replace Zend\I18n\Translator\TranslatorServiceFactory with Zend\Mvc\Service\TranslatorServiceFactory.

OTHER TIPS

this is simple tutorial
http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/
http://www.poedit.net/
but i have

   ,'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type' => 'translate',
                'base_dir' => __DIR__ . '/../language',
                'pattern' => '%s.mo',
            ),
        ),
    ),

in module.config.php and a php file that all keys exist on it for example

<?php 

    /*
     * General keys
     */
    $this->translate('projectname');
    $this->translate('login');
    $this->translate('logout');
    $this->translate('pages');
    $this->translate('contents');
    $this->translate('ourposition');            
    $this->translate('search_pleace');  
    $this->translate('contactform');
    $this->translate('pleaseuploadresume');

?>

in poedit i only set path of this file. (help to better management) and on Catalog properties-> sources keywords translate should be exist ( and of course be better one) . any where i want show message . i write

echo $this->translate('key');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top