After long battle, here is how I have achieved this. Special thanks to @Kwido and @Wilt for sending me to right direction (I have voted both of them up).
Application/config/module.config.php
return array(
'controllers' => array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController'
),
),
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
'language' => array(
'type' => 'Segment',
'options' => array(
//'route' => '/[:language]',
'route' => '/en',
'constraints' => array(
'language' => 'en',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'changeLocaleEnglish'
)
)
),
'languageChinese' => array(
'type' => 'Segment',
'options' => array(
//'route' => '/[:language]',
'route' => '/cn',
'constraints' => array(
'language' => 'cn',
),
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'changeLocaleChinese'
)
)
),
////
// other stuff
//////////// like child routes etc
),
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
),
'factories' => array(
'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'zh_CN', //default is english which is set in module.php
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'view_helpers' => array(
'invokables'=> array(
'PaginationHelper' => 'Application\View\Helper\PaginationHelper'
)
),
'view_manager' => array(
//....... view stuff
),
// Placeholder for console routes
'console' => array(
'router' => array(
'routes' => array(
),
),
),
);
Application/module.php
public function onBootstrap(MvcEvent $e)
{
$application = $e->getApplication();
$serviceManager = $application->getServiceManager();
// Just a call to the translator, nothing special!
$serviceManager->get('translator');
$this->initTranslator($e);
}
protected function initTranslator(MvcEvent $event)
{
$serviceManager = $event->getApplication()->getServiceManager();
// use Zend\Session\Container add this to top
$session = New Container('language');
$translator = $serviceManager->get('translator');
if($session['language'] != 'zh_CN'){ //if session doesn't have zh_CN then set it as english
$translator
->setLocale($session->language)
->setFallbackLocale('en_US')
;
}
}
now in Application/src/Application/Controller/IndexController.php I have added two action to catch the session and set the language:
public function changeLocaleChineseAction()
{
$result = new ViewModel();
$result->setTerminal(true);
$response = $this->getResponse();
// New Container will get he Language Session if the SessionManager already knows the language session.
$session = new Container('language');
$request = $this->getRequest();
$config = $this->serviceLocator->get('config');
$language = $config['translator']['locale']; //default locale from Application/config/module.config.php
if (isset($config['translator']['locale'])) {
$session->language = $language;
$this->serviceLocator->get('translator')->setLocale('zh_CN')
->setFallbackLocale('zh_CN')
;
}
return $this->redirect()->toRoute('home');
}
public function changeLocaleEnglishAction()
{
// New Container will get he Language Session if the SessionManager already knows the language session.
$session = new Container('language');
//just clear the language session
$session->getManager()->getStorage()->clear('language');
$language = 'en_US'; //set new language
$request = $this->getRequest();
$config = $this->serviceLocator->get('config');
$session->language = $language;
$this->serviceLocator->get('translator')->setLocale('en_US')
->setFallbackLocale('en_US')
;
return $this->redirect()->toRoute('home');
}
Now just add the link in layout.phtml
to have language switcher:
<a href="<?php echo $this->url('home')."cn";?>"><?php echo $this->translate("Chinese");?></a>
<a href="<?php echo $this->url('home')."en";?>"><?php echo $this->translate("English");?></a>
Hope this helps others in future.