ZF2 Translation

谁说我不能喝 提交于 2019-12-04 13:46:21

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'
    );
}

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!