在我的ZF2系统中安装语言切换器的最佳选择是什么? [英] What's the best option to have language switcher in my ZF2 system?

查看:66
本文介绍了在我的ZF2系统中安装语言切换器的最佳选择是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的一位客户开发了小型ZF2学说系统.到目前为止,一切都很好,但是它们需要使用2种语言的系统.

I have developed small ZF2 doctrine system for one of my client. All is good so far, but they require the system in 2 languages.

我可以在module/Application/config/module.config.php

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

我也可以使用此方法在module.php

And I also can use this method to set default in module.php

public function onBootstrap(MvcEvent $e)
{
    $application = $e->getApplication();
    $serviceManager = $application->getServiceManager();
    // Just a call to the translator, nothing special!
    $serviceManager->get('translator');
    $this->initTranslator($e);

    // Etc, more of your bootstrap function.
}

protected function initTranslator(MvcEvent $event)
{
    $serviceManager = $event->getApplication()->getServiceManager();

    // Zend\Session\Container
    $session = New Container('language');

    $translator = $serviceManager->get('translator');
    $translator
        ->setLocale($session->language)
        ->setFallbackLocale('zh_CN')
        ;


}

这很好,现在我的系统将默认语言显示为中文.但是,我想让用户选择.

This is good, now my system shows default language as Chinese. However, I would like to give option to users to choose from.

我该怎么做?

我找到了这个链接,但是我无法正常工作.

I had found this link but I couldn't get it work.

当我在Application/IndexController.php中添加以下函数时,它不执行任何操作,而是http://myurl.com/changeLocal抛出404 error.我是否还需要在module.config.php中添加任何内容?

When I add following function in Application/IndexController.php it doesn't do anything instead http://myurl.com/changeLocal throw 404 error. Do I need to add anything in module.config.php too?

public function changeLocaleAction() 
{
    // New Container will get he Language Session if the SessionManager already knows the language session.
    $session = new Container('language');
    $language = $request->getPost()->language;
    $config = $this->serviceLocator->get('config');
    if (isset($config['locale']['available'][$language])) {
        $session->language = $language;
        $this->serviceLocator->get('translator')->setLocale($session->language);
    }
}

推荐答案

经过漫长的战斗,这是我实现这一目标的方式.特别感谢 @Kwido @ Wilt 致使我朝正确的方向前进(我对他们两个人都投了赞成票).

After long battle, here is how I have achieved this. Special thanks to @Kwido and @Wilt for sending me to right direction (I have voted both of them up).

Application/config/module.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController'
        ),
    ),
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),

            'language' => array(
                'type' => 'Segment',
                'options' => array(
                    //'route' => '/[:language]',
                    'route' => '/en',
                    'constraints' => array(
                        'language' => 'en',
                    ),
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'changeLocaleEnglish'
                    )
                )
            ),
            'languageChinese' => array(
                'type' => 'Segment',
                'options' => array(
                    //'route' => '/[:language]',
                    'route' => '/cn',
                    'constraints' => array(
                        'language' => 'cn',
                    ),
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'changeLocaleChinese'
                    )
                )
            ),
            ////
            // other stuff
            //////////// like child routes etc
        ),
    ),
    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'factories' => array(
            'translator' => 'Zend\Mvc\Service\TranslatorServiceFactory',
        ),
    ),

    'translator' => array(
        'locale' => 'zh_CN', //default is english which is set in module.php
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),

    'view_helpers' => array(
        'invokables'=> array(
            'PaginationHelper' => 'Application\View\Helper\PaginationHelper'
        )
    ),

    'view_manager' => array(
        //....... view stuff
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
            ),
        ),
    ),
);

Application/module.php

public function onBootstrap(MvcEvent $e)
{
    $application = $e->getApplication();
    $serviceManager = $application->getServiceManager();
    // Just a call to the translator, nothing special!
    $serviceManager->get('translator');
    $this->initTranslator($e);
}

protected function initTranslator(MvcEvent $event)
{
    $serviceManager = $event->getApplication()->getServiceManager();

    // use Zend\Session\Container add this to top
    $session = New Container('language');

    $translator = $serviceManager->get('translator');
    if($session['language'] != 'zh_CN'){ //if session doesn't have zh_CN then set it as english
    $translator
        ->setLocale($session->language)
        ->setFallbackLocale('en_US')
        ;
    }
}

现在在 Application/src/Application/Controller/IndexController.php 中,我添加了两个操作来捕获会话并设置语言:

now in Application/src/Application/Controller/IndexController.php I have added two action to catch the session and set the language:

public function changeLocaleChineseAction() 
    {
        $result = new ViewModel();
        $result->setTerminal(true);
        $response = $this->getResponse();


        // New Container will get he Language Session if the SessionManager already knows the language session.
        $session = new Container('language');
        $request = $this->getRequest();
        $config = $this->serviceLocator->get('config');

        $language = $config['translator']['locale']; //default locale from Application/config/module.config.php
        if (isset($config['translator']['locale'])) {
            $session->language = $language;
            $this->serviceLocator->get('translator')->setLocale('zh_CN')
                ->setFallbackLocale('zh_CN')
                ;

        }

        return $this->redirect()->toRoute('home'); 

    }

    public function changeLocaleEnglishAction() 
    {
        // New Container will get he Language Session if the SessionManager already knows the language session.
        $session = new Container('language');

        //just clear the language session
        $session->getManager()->getStorage()->clear('language');
        $language = 'en_US'; //set new language
        $request = $this->getRequest();
        $config = $this->serviceLocator->get('config');
        $session->language = $language;
        $this->serviceLocator->get('translator')->setLocale('en_US')
            ->setFallbackLocale('en_US')
            ;
        return $this->redirect()->toRoute('home');

    }

现在只需在layout.phtml中添加链接即可使用语言切换器:

Now just add the link in layout.phtml to have language switcher:

<a href="<?php echo $this->url('home')."cn";?>"><?php echo $this->translate("Chinese");?></a>
<a href="<?php echo $this->url('home')."en";?>"><?php echo $this->translate("English");?></a>

希望这对以后的人有帮助.

Hope this helps others in future.

这篇关于在我的ZF2系统中安装语言切换器的最佳选择是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆