4

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!

doydoy44
  • 5,720
  • 4
  • 29
  • 45
Walllter
  • 342
  • 8
  • 19
  • Did you try to remove 'locale' => 'en_US' in module.config.php? – Oyeme Mar 19 '14 at 17:12
  • No idea if it's related, but your `aliases` array should be inside the `service_manager` array – Crisp Mar 19 '14 at 18:06
  • If I remove 'locale', I have this exception `Fatal error: Uncaught exception 'Zend\I18n\Exception\ExtensionNotLoadedException' with message 'Zend\I18n\Translator component requires the intl PHP extension`. About `aliases` - if I add in `service_manager`, displayed error - aliases already included. – Walllter Mar 20 '14 at 08:19

3 Answers3

2

in Application\Module.php

public function onBootstrap(MvcEvent $e) {
        $translator = $e->getApplication()->getServiceManager()->get('translator');
            $lang = $e->getRequest()->getQuery('lang'); // new language
            $session = new Container('base');
            if($lang == null && $lang == ''){
                if ($session->offsetExists('lang')) {
                    $lang = $session->offsetGet('lang'); // current language
                }else{
                    $lang = Settings::DEFAULT_LANGUAGE; // default language
                }
            }
            $session->offsetSet('lang', $lang);
            $loc = Settings::$locations[$lang];
            $translator
            ->setLocale($loc)
            ->setFallbackLocale(Settings::DEFAULT_LANGUAGE .'_' . Settings::DEFAULT_LOCATION);
    }

and Settings class

class Settings{
   const DEFAULT_LOCATION =  'IR';
   const DEFAULT_LANGUAGE = 'fa';

   public static $locations = array(
        'fa'=>'fa_IR',
            'sa'=>'sa_SA',//Arabic (sa, sa-SA)
        'tr'=>'tr_TR',
        'en'=>'en_US'
    );
}
Amin Arab
  • 530
  • 4
  • 19
  • Solved, thank you. I removed `'factories' => array('translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',),` and all translations works fine. – Walllter Mar 20 '14 at 10:48
1

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

Amin Arab
  • 530
  • 4
  • 19
  • I tried to do everything that you said but the translation is still not working. I don`t know how well this solution but for me its working: In bootstrap `$viewModel = $e->getApplication()->getMvcEvent()->getViewModel(); $viewModel->language = $session->offsetGet('lang');` In view: `= $this->translate('Home', 'default', $language); ?>` – Walllter Mar 20 '14 at 09:24
  • what's mea that?!? $viewModel->language = $session->offsetGet('lang'); – Amin Arab Mar 20 '14 at 09:32
  • Put language view model from session. – Walllter Mar 20 '14 at 09:35
  • 3
    Actually as of php 5.4 = ... ?> is supported! http://stackoverflow.com/a/200666/1594076 – YRM Mar 20 '14 at 09:53
  • `=` works independently of php 5+ version. It works only if in php.ini enable otion `"Open_short_tag"`. Also, if i change to ` – Walllter Mar 20 '14 at 10:09
  • 2
    @Walllter for versions < 5.4 that is correct but since 5.4 `=` is supported everywhere regardless of php.ini open_short_tag setting. But `=` is NOT deprecated like @amin arab suggested, it is actually becoming fundamental. The comment was not about the solution, if you have php 5.4+ or open_short_tags on `=` and ` – YRM Mar 21 '14 at 10:19
1

This problem is due to the absence of intl extension

So just required these steps:

I had to install PHP Intl extension:

sudo apt-get install php5-intl

Apache Restart:

sudo service apache2 restart

Check what extension compiled:

php -m
Shal
  • 613
  • 6
  • 9