ZF2 Translation

2019-04-11 00:27发布

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!

3条回答
放荡不羁爱自由
2楼-- · 2019-04-11 00:48

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
查看更多
放荡不羁爱自由
3楼-- · 2019-04-11 01:00

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'
    );
}
查看更多
贼婆χ
4楼-- · 2019-04-11 01:00

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

查看更多
登录 后发表回答