Zend Framework multi-language integration steps

I am new to zend framework2 and I am working on a website with multi-language integration. Please give me an idea of ​​how to configure the built-in library and translation file and how it can be called from a view file

+4
source share
1 answer

ZF2 has already integrated the I18n tools.

How to integrate it

module.config.php

'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),

* .Mo files

Following the previous step, create a folder and add en_US.mo (for example) using Poedit (a simple and good application)

module.php

public function onBootstrap($e)
{
    $translator = $e->getApplication()->getServiceManager()->get('translator');
    $translator
      ->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
      ->setFallbackLocale('en_US');
}

rq: , , SEO

    // session container
    $sessionContainer = new Container('locale');

    // test if session language exists
    if(!$sessionContainer->offsetExists('mylocale')){
        // if not use the browser locale
        if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
            $sessionContainer->offsetSet('mylocale', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
        }else{
            $sessionContainer->offsetSet('mylocale', 'en_US');
        }

    }

    // translating system
    $translator = $serviceManager->get('translator');
    $translator ->setLocale($sessionContainer->mylocale)
                ->setFallbackLocale('en_US');

    $mylocale = $sessionContainer->mylocale;

:

 <?php echo $this->translate("Translate that!"); ?>

+13

All Articles