Get local value in layout or view in Zend Framework 2
Asked Answered
M

2

6

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

Magbie answered 20/11, 2013 at 5:24 Comment(2)
An easy option available hereMagbie
Solution: $this->plugin('translate')->getTranslator()->getLocale();Magbie
T
9

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();
Trotskyite answered 20/11, 2013 at 5:34 Comment(4)
Misunderstood! I've updated question details. We never have access to google server to get it's 'language' query variable. and google is in zf2?Magbie
@Magbie I see what you mean and added it to my answer.Trotskyite
sorry im new in zf2 but i'll try $request = new Request(); $locale = $request->getQuery('locale'); var_dump($locale); but I get "null"Busoni
@icy2k you can't get it from "new Request", that object is basically empty. You should have this->request available when you're in controller.Trotskyite
V
1

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);
Vento answered 20/11, 2013 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.