Translations from Database in ZF2

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Translations from Database in ZF2

Walllter
This post has NOT been accepted by the mailing list yet.
Hello. I am novice in ZF2, and I need translation from db. I have a DB like this


and files:

1)Application/module.config.php:

'factories' => array(
            'translator' =>
                function  (\Zend\ServiceManager\ServiceManager $serviceManager)
                {
                    $pluginManager = new \Zend\I18n\Translator\LoaderPluginManager();
                    $pluginManager->setServiceLocator($serviceManager);
                    $pluginManager->setFactory('DatabaseTranslationLoaderFactory', function($serviceManager)
                    {
                        $translator = new \Zend\I18n\Translator\DatabaseTranslationLoaderFactory();
                        return $translator->createService($serviceManager);
                    });
                    $translator = new \Zend\I18n\Translator\Translator(array());
                    $translator->setFallbackLocale('en_US');
                    $translator->setPluginManager($pluginManager);
                    $translator->addRemoteTranslations('DatabaseTranslationLoaderFactory');
                    return $translator;
                },

        ),

'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'Zend\I18n\Translator\Loader\Database',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),

2) Zend/I18n/Translator/Loader/Database.php:

<?php

namespace Zend\I18n\Translator\Loader;

use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Zend\I18n\Translator\Plural\Rule as PluralRule;
use Zend\I18n\Translator\TextDomain;
use Zend\Mvc\Controller\AbstractActionController as AbstractActionController;

class Database implements RemoteLoaderInterface
{
    public $dbAdapter;


    public function __construct(Adapter $dbAdapter)
    {
        $this->dbAdapter = $dbAdapter;
    }


    public function load($locale, $textDomain)
    {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select('ic_var')->columns(array('value'))
                      ->where(array('language' => $locale, 'name' => $textDomain));

        $messages = $this->dbAdapter->query(
            $sql->getSqlStringForSqlObject($select),
            Adapter::QUERY_MODE_EXECUTE
        );

        $textDomain = new TextDomain();

        foreach ($messages as $message) {
            if (isset($textDomain[$message['name']])) {
                if (!is_array($textDomain[$message['name']])) {
                    $textDomain[$message['name']] = array(
                        $message['message_plural_index'] => $textDomain[$message['message_key']]
                    );
                }
                $textDomain[$message['name']][$message['message_plural_index']] = $message['value'];
            } else {
                $textDomain[$message['message_key']] = $message['message_translation'];
            }
        }
        return $textDomain;
    }
}


3) Zend/I18n/Translator/DatabaseTranslationLoaderFactory.php:

<?php

namespace Zend\I18n\Translator;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\I18n\Translator\Loader\Database;

class DatabaseTranslationLoaderFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new Database($serviceLocator->get('Zend\Db\Adapter\Adapter'));
    }
}

4) Application/Module.php:

public function onBootstrap(MvcEvent $e)
{
    $translator = $e->getApplication()->getServiceManager()->get('translator');
    $translator->addTranslationFile('DatabaseTranslationLoaderFactory','text-domain', 'text-domain');
}


And I have an error:

Catchable fatal error: Argument 1 passed to Zend\I18n\Translator\Loader\Database::__construct() must be an instance of Zend\Db\Adapter\Adapter, none given, called in /home/thebigch/www/onlinecontest/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php on line 170 and defined in /home/thebigch/www/onlinecontest/vendor/zendframework/zendframework/library/Zend/I18n/Translator/Loader/Database.php on line 24

Please, help me with this, I spent two weeks, but translations doesn`t work...