dpnru86024 2014-07-25 04: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 05:49
    关注

    Setup your Locales in your global.config.php:

    1. 'locale' => array(
    2. 'default' => 'en_US',
    3. 'available' => array(
    4. 'de_DE' => 'Deutsch',
    5. 'nl_NL' => 'Dutch',
    6. 'en_US' => 'English',
    7. 'fr_FR' => 'French',
    8. ),
    9. ),

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

    1. class Module {
    2. public function onBootstrap(MvcEvent $e)
    3. {
    4. $applicaton = $e->getApplication();
    5. $serviceManager = $application->getServiceManager();
    6. // Just a call to the translator, nothing special!
    7. $serviceManager->get('translator');
    8. $this->initTranslator($e);
    9. // Etc, more of your bootstrap function.
    10. }
    11. protected function initTranslator(MvcEvent $event)
    12. {
    13. $serviceManager = $event->getApplication()->getServiceManager();
    14. // Zend\Session\Container
    15. $session = New Container('language');
    16. $translator = $serviceManager->get('translator');
    17. $translator
    18. ->setLocale($session->language)
    19. ->setFallbackLocale('en_US');
    20. }
    21. }

    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!

    1. public function changeLocaleAction()
    2. {
    3. // New Container will get he Language Session if the SessionManager already knows the language session.
    4. $session = new Container('language');
    5. $language = $this->getRequest()->getPost()->language;
    6. $config = $this->serviceLocator->get('config');
    7. if (isset($config['locale']['available'][$language]) {
    8. $session->language = $language;
    9. $this->serviceLocator->get('translator')->setLocale($session->language);
    10. }
    11. }

    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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部