src/Listener/LanguageSetter.php
public function onRoute(MvcEvent $e)
    {
        /* @var $request \Zend\Http\PhpEnvironment\Request */
        $request = $e->getRequest();

        /* Detect language */
        $lang = $request->getQuery('lang');

        if (!$lang) {
            $headers = $request->getHeaders();
            if ($headers->has('Accept-Language')) {
                /* @var $acceptLangs \Zend\Http\Header\AcceptLanguage */
                $acceptLangs = $headers->get('Accept-Language');
                $locales = $acceptLangs->getPrioritized();
                $locale  = $locales[0];
                $lang    = $locale->type;
            } else {
                $lang = 'en';
            }
        }

        /* Set locale */
        $translator = $e->getApplication()->getServiceManager()->get('mvctranslator');
        $locale     = $lang . '_' . strtoupper($lang);

        setlocale(
            LC_ALL,
            array(
                            $locale . ".utf8",
                            $locale . ".iso88591",
                            $locale,
                            substr($locale, 0, 2),
                            'de_DE.utf8',
                            'de_DE',
                            'de'
                        )
        );
        \Locale::setDefault($locale);
        $translator->setLocale($locale);
        $routeMatch = $e->getRouteMatch();
        if ($routeMatch && $routeMatch->getParam('lang') === null) {
            $routeMatch->setParam('lang', $lang);
        }

        /* @var $router \Zend\Mvc\Router\SimpleRouteStack */
        $router = $e->getRouter();
        $router->setDefaultParam('lang', $lang);
    }
public function onRoute(MvcEvent $e)
    {
        /* @var $request \Zend\Http\PhpEnvironment\Request */
        $request = $e->getRequest();

        /* Detect language */
        $lang = $request->getQuery('lang');

        if (!$lang) {
            $headers = $request->getHeaders();
            if ($headers->has('Accept-Language')) {
                /* @var $acceptLangs \Zend\Http\Header\AcceptLanguage */
                $acceptLangs = $headers->get('Accept-Language');
                $locales = $acceptLangs->getPrioritized();
                $locale  = $locales[0];
                $lang    = $locale->type;
            } else {
                $lang = 'en';
            }
        }

        /* Set locale */
        $translator = $e->getApplication()->getServiceManager()->get('mvctranslator');
        $locale     = $lang . '_' . strtoupper($lang);

        setlocale(
            LC_ALL,
            array(
                            $locale . ".utf8",
                            $locale . ".iso88591",
                            $locale,
                            substr($locale, 0, 2),
                            'de_DE.utf8',
                            'de_DE',
                            'de'
                        )
        );
        \Locale::setDefault($locale);
        $translator->setLocale($locale);
        $routeMatch = $e->getRouteMatch();
        if ($routeMatch && $routeMatch->getParam('lang') === null) {
            $routeMatch->setParam('lang', $lang);
        }

        /* @var $router \Zend\Mvc\Router\SimpleRouteStack */
        $router = $e->getRouter();
        $router->setDefaultParam('lang', $lang);
    }
public function onRoute ( MvcEvent $ e ) { /* @var $request \Zend\Http\PhpEnvironment\Request */ $ request = $ e -> getRequest ( ) ; /* Detect language */ $ lang = $ request -> getQuery ( 'lang' ) ; if ( ! $ lang ) { $ headers = $ request -> getHeaders ( ) ; if ( $ headers -> has ( 'Accept-Language' ) ) { /* @var $acceptLangs \Zend\Http\Header\AcceptLanguage */ $ acceptLangs = $ headers -> get ( 'Accept-Language' ) ; $ locales = $ acceptLangs -> getPrioritized ( ) ; $ locale = $ locales [ 0 ] ; $ lang = $ locale -> type ; } else { $ lang = 'en' ; } } /* Set locale */ $ translator = $ e -> getApplication ( ) -> getServiceManager ( ) -> get ( 'mvctranslator' ) ; $ locale = $ lang . '_' . strtoupper ( $ lang ) ; setlocale ( LC_ALL , array ( $ locale . ".utf8" , $ locale . ".iso88591" , $ locale , substr ( $ locale , 0 , 2 ) , 'de_DE.utf8' , 'de_DE' , 'de' ) ) ; \ Locale :: setDefault ( $ locale ) ; $ translator -> setLocale ( $ locale ) ; $ routeMatch = $ e -> getRouteMatch ( ) ; if ( $ routeMatch && $ routeMatch -> getParam ( 'lang' ) === null ) { $ routeMatch -> setParam ( 'lang' , $ lang ) ; } /* @var $router \Zend\Mvc\Router\SimpleRouteStack */ $ router = $ e -> getRouter ( ) ; $ router -> setDefaultParam ( 'lang' , $ lang ) ; }
Listens to the route event.

Detects the language to use and sets translator locale.
The language is detected either via query parameter "lang" or
browser setting (ACCEPT-LANGUAGE header)

@param MvcEvent $e
Listens to the route event .