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!
This problem is due to the absence of intl extension
So just required these steps:
I had to install PHP Intl extension:
Apache Restart:
Check what extension compiled:
in Application\Module.php
and Settings class
put translator config only in Application\module.config.php and be sure you have language folder in Application module and put *.mo and *.po file on that .
in the fact , you don't need to set locale in each module . only put in Application\Module.php
in poedit Catalog->properties->Sources keywords-> check "translate" word exist and it's better that be first.
at the end ,
<?=$this->translate('Home');?>
deprecated . use<?php echo $this->translate('Home');?>
update 1:
sorry , this code
<?=$this->translate('Home');?>
not deprecated but PHP manual recommendation is<?php echo $this->translate('Home');?>
http://php.net/manual/en/language.basic-syntax.phpmode.php