在module.php中添加/更新翻译区域设置

时间:2014-01-27 07:55:52

标签: php zend-framework2

如何在 Module.php 中更新翻译区域设置? 我试过这个:

$locale = 'de_DE';//will be generated dynamically
$translator = $e->getApplication()->getServiceManager()->get('translator');         
$translator->setLocale($locale)->setFallbackLocale('en_US');

在module.config.php中,代码为:

'service_manager' => array(
    'factories' => array(
        'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
    ),
),

'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type'     => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern'  => '%s.mo',
        ),
    ),
),

我试过评论翻译文件路径,但是它显示了en_US文本。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

要在调度操作之后但在呈现视图之前更改语言环境,请将函数附加到dispatch事件。在你的引导程序中你可以这样做:

    public function onBootstrap(MvcEvent $e) {

    $sm = $e->getApplication ()->getServiceManager ();

    $em = $e->getApplication ()->getEventManager ();

     $em->attach ( MvcEvent::EVENT_DISPATCH, function ($e) use($sm) {


                 $locale = 'de_DE';//will be generated dynamically
                 $translator = $sm->get('translator');         
                 $translator->setLocale($locale)->setFallbackLocale('en_US');



             }, - 100 );


     }