Translations from DB in ZF2

人盡茶涼 提交于 2019-12-11 12:33:57

问题


I have problem with creating custom translator from database in ZF2. I have a DB like this

and files:

1)Application/module.config.php

'service_manager' => array(
    'abstract_factories' => array(),
    'factories' => array(
        'translator' => function($sm){
                $translator = new \Zend\I18n\Translator\DatabaseTranslationLoaderFactory();
                return $translator->createService($sm);
            },
    ),
),

'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;


class Database implements RemoteLoaderInterface {

protected $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['plural_index'] => $textDomain[$message['name']]
                );
            }
            $textDomain[$message['name']][$message['plural_index']] = $message['value'];
        } else {
            $textDomain[$message['name']] = $message['value'];
        }
    }
    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(
        'DatabaseTranslationLoader',
        'text-domain',
        'text-domain'
    );
}

But translation doesn`t work, because db adapter not find in loader:

Catchable fatal error: Argument 1 passed to Zend\I18n\Translator\Loader\Database::__construct() must be an instance of Zend\Db\Adapter\Adapter, none given

Thanks for your answers!

来源:https://stackoverflow.com/questions/22824423/translations-from-db-in-zf2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!