Zend framework 2 : How to set locale globaly?

sgleser87 picture sgleser87 · Apr 11, 2013 · Viewed 11k times · Source

I have to change the locale dynamically depending which language the user wants.

I can set the locale in the Application/Module.php like this :

public function onBootstrap(MvcEvent $e)
{
    $translator = $e->getApplication()->getServiceManager()->get('translator');
    $translator->setLocale('hu_HU');
}

But, how can I do this in the controller, if I want to change languages ? I tried this, but after this I can change the locale only for this one request and not global.

$translator = $this->getServiceLocator()->get('translator');
$translator->setLocale('srb_SRB');

Answer

Sam picture Sam · Apr 11, 2013

Set up the default locale at configuration level! See #61 of module.config.php from ZendSkeletonApplications Application Module

'translator' => array(
    'locale' => 'en_US',
)