dan2k3k4
dan2k3k4

Reputation: 1409

Translations not working in ZF2

I know this has been posted multiple times but I've gone through each answer and found nothing worked for me...

In module.config file

'service_manager' => array(
    'abstract_factories' => array(
        'Zend\Db\Adapter\AdapterAbstractServiceFactory',
    ),
    'factories' => array(
        'translator'                    => 'Zend\I18n\Translator\TranslatorServiceFactory',
        // various app services, but only listing first one
        'Application\Service\Fetch'     => 'Application\Factory\Service\FetchServiceFactory',
    ),
),
'translator' => array(
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),

In module/Application/language/ folder, I have these .mo and .po files:

In one of the template view files:

<div id="l_activite" class="homeTitle">
    <?php echo $this->translate('Activités', __NAMESPACE__);?>
</div>

In Module.php

I've cut this down to show how the setProjectName is invoked (which handles language translations and other things...)

public function onBootstrap(MvcEvent $e)
{
    $application          = $e->getApplication();
    $eventManager         = $application->getEventManager();

    $moduleRouteListener  = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
    $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'setProjectName'));
}

I've cut the function down a bit to only show the lines concerning the language...

public function setProjectName(MvcEvent $e)
{
    // Session (lang & background)
    $session        = new Session();
    $request        = $e->getRequest();

    $lang = null;

    if (!$request instanceof \Zend\Console\Request) {
        $rm             = $request->getQuery();
        $lang           = $rm->get('lang');
    }

    $session->lang        = $lang        ?: $session->lang ?: 'fr';
    $lang                 = $session->lang;

    $session->langLocale  = strtolower($session->lang) . '_' . strtoupper($session->lang) . '.UTF-8';
    if ($lang == 'en') {
        $session->langLocale  = strtolower($session->lang) . '_' . strtoupper('US') . '.UTF-8';
    }

    $loc = setlocale(LC_ALL, $session->langLocale);
    // for Windows
    if (!$loc) {
        switch ($session->lang) {
            case 'en':
                $session->langLocale = 'eng';
            break;
            case 'de':
                $session->langLocale = 'deu';
            break;
            case 'fr':
                $session->langLocale = 'fra';
            break;
            case 'it':
                $session->langLocale = 'ita';
            break;
            case 'es':
                $session->langLocale = 'esn';
            break;
        }
        setlocale(LC_ALL, $session->langLocale);
    }

    \Locale::setDefault($session->langLocale);

    $translator          = $e->getApplication()->getServiceManager()->get('translator');
    $langLocale          = $translator->getLocale();


    $e->setParam('lang', $langLocale);
}

I'm guessing the things I do in Module.php needs fixing in some way? I have that block of Windows code since we plan to run the app locally on some "offline" machines, but also running the app on the server (which runs linux).

We use $lang as fr/de/en/it/es because the database (sqlite) file is stored as _fr.sqlite, _de.sqlite, etc.

Upvotes: 0

Views: 290

Answers (3)

Mubo
Mubo

Reputation: 1070

In module.config file You are using this translator

   'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',

I think the problem is that you are some how importing or using an old translator namespace (path) file.

You need to look for a file where you are importing the zend translator, like listener, that can be in Listener/ApplicationListener.php

You will probably see some thing like this.

you need to activate the first one and comment the second one

use Zend\I18n\Translator\Translator; //use this namespace, it fits your configs.

// use Zend\Mvc\I18n\Translator;

Hope that may give some guidance.

Upvotes: 0

ismaail E.G.
ismaail E.G.

Reputation: 429

Try to rename the .mo & .po files to be the same as 'eng', 'deu', 'fra', 'ita', 'esp'.

Upvotes: 1

Sybrand Bakker
Sybrand Bakker

Reputation: 19

Which version of ZF2. The translatorinterface changed in 2.3

Upvotes: 0

Related Questions