dpnru86024 2014-07-25 12:54
浏览 55
已采纳

Zend框架2 - 如何制作语言切换器

I am developing a Zend Framework 2 Application and now I want to implement a language switcher from where guest/registered user can choose the language they want, the thing I can't understand is how is it made in Zend Framework 2 using the storage ( not from urls ), I want to keep the preffered language of guest in the storage once he selects one, and for the registered users I can retrieve the preffered one from cookie/database and reuse it with storage. But where and how should I start/implement this? Thank you in advance.

  • 写回答

2条回答 默认 最新

  • duandun2136 2014-07-25 13:49
    关注

    Setup your Locales in your global.config.php:

    'locale' => array(
        'default' => 'en_US',
        'available'     => array(
            'de_DE' => 'Deutsch',
            'nl_NL' => 'Dutch',
            'en_US' => 'English',
            'fr_FR' => 'French',
        ),
    ),
    

    So in your Application\Module.php you can add a method which sets the default Zend\Translator\Translator:

    class Module {
    
        public function onBootstrap(MvcEvent $e)
        {
            $applicaton = $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('en_US');
        }
    }
    

    So now the default Locale is en_US as the session has no Locale available. For changing the locale you need to catch the users input and validate the available locales you support, provided in your global.config.php. So in order to change it you might need to add a controller action which catches the input of the user and sets the new locale. Example of the controller action without any form usage!

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

    The session allows the users to change their locale and remember it until the session ends, so they won't need to change it when they get back after a while. Hope this will help you and can help you to write some code to save it for your registered users on your application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀