I have two modules - Application and StickyNotes. I need to use translation on all pages.
What I do:
1) In view: <?=$this->translate('Home');?>
2) In Application\Module.php:
public function onBootstrap(MvcEvent $e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$app = $e->getParam('application');
$app->getEventManager()->attach('render', array($this, 'setLayoutTitle'));
$translator->setLocale('ru_RU');
echo $translator->getLocale(); //ru_RU
}
3) In StickyNotes\Module.php:
public function onBootstrap(MvcEvent $e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator->setLocale('ru_RU');
echo $translator->getLocale(); //ru_RU
}
4) Application\..\module.config.php:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
5) StickyNotes\..\module.config.php same:
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'aliases' => array(
'translator' => 'MvcTranslator',
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
If i try $translator->getLocale();
output 'ru_RU', but translation don`t work.
Also, if I manually change 'locale' => 'en_US',
to 'locale' => 'ru_RU',
translation work fine. Thanks for the answers!