5

I'm trying to evaluate if it's already the right time to start moving to ZF3 (or keep developing my application with ZF2). Therefore, I installed the mvc-skeleton application and walked through the MVC tutorial (here) which worked perfectly until I got to the internationalization part.

I installed the components (i.e. zend-i18n and zend-mvc-i18n component), created the translation files (i.e. en_US.mo and en_US.po) and copied them into my module/Application/language/ folder and added the configuration in the application configuration file.

// in a module's module.config.php:
'translator' => [
    'locale' => 'en_US',
    'translation_file_patterns' => [
        [
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ],
    ],
],

And of course, added the text to translate in the layout.phtml file

<p>&copy; 2016 by Examples Ltd. <?= $this->translate('All rights reserved') ?></p>

However, for some reason it's not working

Zend\ServiceManager\Exception\ServiceNotFoundException    

File:
my_install_path/zend-mvc-skeleton-application/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:133

Message:
A plugin by the name "translate" was not found in the plugin manager Zend\View\HelperPluginManager

Stack trace:
#0 /my-own-install-path/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(373): Zend\ServiceManager\AbstractPluginManager->get('translate', NULL)
#1 /my-own-install-path/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(391): Zend\View\Renderer\PhpRenderer->plugin('translate')
#2 /my-own-install-path/module/Application/view/application/index/index.phtml(1): Zend\View\Renderer\PhpRenderer->__call('translate', Array)
#3 /my-own-install-path/module/Application/view/application/index/index.phtml(1): Zend\View\Renderer\PhpRenderer->translate('Dr Job')
#4 /my-own-install-path/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(502): include('/Applications/M...')
#5 /my-own-install-path/vendor/zendframework/zend-view/src/View.php(207): Zend\View\Renderer\PhpRenderer->render(Object(Zend\View\Model\ViewModel))
#6 /my-own-install-path/vendor/zendframework/zend-view/src/View.php(236): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#7 /my-own-install-path/vendor/zendframework/zend-view/src/View.php(200): Zend\View\View->renderChildren(Object(Zend\View\Model\ViewModel))
#8 /my-own-install-path/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrategy.php(105): Zend\View\View->render(Object(Zend\View\Model\ViewModel))
#9 /my-own-install-path/vendor/zendframework/zend-eventmanager/src/EventManager.php(271): Zend\Mvc\View\Http\DefaultRenderingStrategy->render(Object(Zend\Mvc\MvcEvent))
#10 /my-own-install-path/vendor/zendframework/zend-eventmanager/src/EventManager.php(143): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent))
#11 /my-own-install-path/vendor/zendframework/zend-mvc/src/Application.php(369): Zend\EventManager\EventManager->triggerEvent(Object(Zend\Mvc\MvcEvent))
#12 /my-own-install-path/vendor/zendframework/zend-mvc/src/Application.php(348): Zend\Mvc\Application->completeRequest(Object(Zend\Mvc\MvcEvent))
#13 /my-own-install-path/public/index.php(43): Zend\Mvc\Application->run()
#14 {main}

My question is simple. Did you succeed in this or do you have any idea of what's wrong with it? Any help will be appreciated. Many thanks.

l_r
  • 1,060
  • 12
  • 23

3 Answers3

13

I got the "plain vanilla" solution from samsonasik here. The solution is to require:

$ composer require zendframework/zend-mvc-i18n

then register as module:

'modules' => [
     'Zend\I18n',
     'Zend\Mvc\I18n',
     // ...
],
l_r
  • 1,060
  • 12
  • 23
  • 2
    This is the correct answer. Additionally, if you are using zend-component-installer, it should *ask* you if you want to install these modules, and then do it for you. If you are not using zend-component-installer yet, add it to your application via `composer require zendframework/zend-component-installer`, as it automates this. – weierophinney Aug 10 '16 at 19:14
  • this is the correct answer, an installer does not include 'Zend\I18n' to the array of modules, only 'Zend\Mvc\I18n', so its config is not included to the application initially – Zippp Jul 24 '18 at 11:17
7

It looks like it can't find the translate view helper. Try adding this in your config file:

'view_helpers' => [
    'invokables' => [
        'translate' => \Zend\I18n\View\Helper\Translate::class
    ]
]

I also found I needed to register the service:

'service_manager' => [
    'factories' => [
        \Zend\I18n\Translator\TranslatorInterface::class => \Zend\I18n\Translator\TranslatorServiceFactory::class,
    ]
]
avy
  • 644
  • 6
  • 16
  • Great, the view_helpers array did the job. – l_r Jul 11 '16 at 12:07
  • How to do the same to work translator on controller? – rafaelphp Sep 01 '16 at 15:23
  • First inject the translate view helper into the controller using a factory (code: `$serviceManager->get('ViewHelperManager')->get('translate')`). Then in the controller use the translator's `__invoke` method i.e. `$translatedString = $translateHelper("Here is a string")`. – avy Sep 01 '16 at 15:42
0

It work me with this configuration:

'translator' => [
    'locale' => 'cs_CZ',
    'translation_file_patterns' => [
        [
            'type' => 'gettext',
            'base_dir' => APPLICATION_MODULE_ROOT . '/language',
            'pattern' => '%s.mo',
        ],
    ],
],

With which modules you have installed zf3? zend-servicemanager do you have installed?

  • The modules installed are: 'Zend\Mvc\I18n', 'Zend\Log', 'Zend\Form', 'Zend\Db', 'Zend\Router', 'Zend\Validator', 'Application' – l_r Jul 11 '16 at 06:31