Get local value in layout or view in Zend Framewor

2019-03-29 13:08发布

问题:

How can we get local value (i.e: 'en' or 'en_US', 'de' etc) in layout.phtml or views in Zend Framework 2?

My local setting are exactly same as explained here

<?php
namespace FileManager;

use Zend\Mvc\ModuleRouteListener;

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

    //...
}

I want to get local value something like this:

$locale = $this->translate()->getLocale(); // <-- It's not working anyway

I need to use '$locale' it while calling google map api url to get matched locale/language. I'm calling it throughtout the application in layout.phtml

$this->headScript()->appendFile('http://maps.googleapis.com/maps/api/js?language=' . $locale);

So I want to make language option dynamic while calling api.

PS: I don't have any query string parameter such as 'language', It's a google api thing which I need to set in script url (if you don't know) Please don't get confused. Not answered here

回答1:

Depends on where you want to get the Locale value from. In any case, you can do it in your controller, e.g.:

$locale = $this->request->getQuery('language');
$this->layout()->locale = $locale;

or

return new ViewModel(array('locale' => $locale));

Edit if you just want to get the locale from the translator, you can try this in view script:

$this->plugin('translate')->getTranslator()->getLocale();


回答2:

My version is like that

<?php
namespace FileManager;

use Zend\Mvc\ModuleRouteListener;
use Zend\Session\Container;

class Module
{
    public function onBootstrap($e)
    {
        $application     = $e->getTarget();
        $serviceManager  = $application->getServiceManager();
        $eventManager    = $application->getEventManager();
        $events = $eventManager->getSharedManager();

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

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

        }

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

        $mylocale = $sessionContainer->mylocale;

        $events->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) use ($mylocale) {
            $controller      = $e->getTarget();
            $controller->layout()->mylocale = $mylocale;
        }, 100);
    }

    //...
}

in your layout

$this->headScript()->appendFile('http://maps.googleapis.com/maps/api/js?language=' . $this->mylocale);