ZF2翻译

时间:2014-03-19 17:05:50

标签: zend-framework2 translation

我有两个模块--Application和StickyNotes。我需要在所有页面上使用翻译。

我的所作所为:

1)在视图中:<?=$this->translate('Home');?>

2)在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)在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相同:

'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',
        ),
    ),
),

如果我尝试$translator->getLocale();输出'ru_RU',但翻译不起作用。

另外,如果我手动将'locale' => 'en_US',更改为'locale' => 'ru_RU', 翻译工作很好。谢谢你的答案!

3 个答案:

答案 0 :(得分:2)

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

和设置类

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

答案 1 :(得分:1)

只在Application \ module.config.php中放置翻译器配置,并确保在应用程序模块中有语言文件夹,并在其上放置* .mo和* .po文件。

事实上,您不需要在每个模块中设置区域设置。仅放入Application \ Module.php

in poedit Catalog-&gt; properties-&gt; Sources keywords-&gt;检查“翻译”一词是否存在,最好先行。

最后,<?=$this->translate('Home');?>已弃用。使用  <?php echo $this->translate('Home');?>

更新1:
抱歉,此代码<?=$this->translate('Home');?>未弃用,但PHP手册建议为<?php echo $this->translate('Home');?>

http://php.net/manual/en/language.basic-syntax.phpmode.php

答案 2 :(得分:1)

此问题是由于缺少intl扩展名

所以只需要这些步骤:

我必须安装PHP Intl扩展名:

sudo apt-get install php5-intl

Apache重启:

sudo service apache2 restart

检查编译的扩展名:

php -m