Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 translator is not working in controller?

I am trying to translate in the controller by ServiceLocator, but this is not translating and I have tried many sulotions in stackoverflow but with out success. My system uses multiple languages and my goal is to use transtor in view, controller, form and filter. Tranlator in my view is working. Any sugestion and help will be appreciated.

Not working in controller:

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

My Application mudole.config.php:

'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 in mudole.config.php to another language but still not translating.

like image 976
Oskar Avatar asked Nov 11 '15 13:11

Oskar


3 Answers

View Helper/Forms

ZF2 ships with the view helper Zend\I18n\View\Helper\Translate; this is why you can already use the method $this->translate($text) in the view.

However all view helper classes that extend from Zend\I18n\View\Helper\AbstractTranslatorHelper (which includes all form view helpers) are also 'translation capable'.

You would need to pass in the translator using $viewHelper->setTranslator($translator) and enabling translation via $viewHelper->setTranslatorEnabled(true).

Controller Plugin

Unfortunately there is no default plugin (that I could find) to handle translators in controllers; I guess you could argue that text content shouldn't be in the controller anyway.

You could easily create one such as the example below. The key is to pass your new translator service as a dependency via a 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 the 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 within a controller class.

// Directly
$this->translate($text, $options); 

// Or fetch the plugin first
$this->translate()->translate($text, $options);
like image 67
AlexP Avatar answered Nov 18 '22 17:11

AlexP


It seems that the locale is sets not directly in the translating text, but by $this->getServiceLocator()->get('translator')->setLocale($locale), and now it is translating my text.

My Application mudole.config.php:

   '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
like image 40
Oskar Avatar answered Nov 18 '22 15:11

Oskar


You can use my ZfTranslate controller plugin.

installation

composer require mikica/zf2-translate-plugin

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

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

Usage in controller

<?php
$this->translate('translate word');
$this->translate('translate word', 'locale');
like image 25
mikicaivosevic Avatar answered Nov 18 '22 15:11

mikicaivosevic