Get local value in layout or view in Zend Framework 2

旧时模样 提交于 2019-12-04 23:59:51

问题


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


来源:https://stackoverflow.com/questions/20088050/get-local-value-in-layout-or-view-in-zend-framework-2

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