Zend Framework 2-如何制作语言切换器 [英] Zend framework 2 - How to make a language switcher

查看:71
本文介绍了Zend Framework 2-如何制作语言切换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Zend Framework 2应用程序,现在我想实现一个语言切换器,来宾/注册用户可以从中选择他们想要的语言,我不明白的是它是如何使用Zend Framework 2制作的.存储(而不是来自url),我想在访客选择了宾语后将其偏好的语言保留在该存储中,对于注册用户,我可以从cookie/数据库中检索该偏好的语言并将其与存储重用.但是我应该在哪里以及如何开始/实现呢? 预先谢谢你.

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.

推荐答案

global.config.php中设置Locales:

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

因此,在您的Application\Module.php中,您可以添加一个设置默认Zend\Translator\Translator的方法:

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

所以现在默认的语言环境为 en_US ,因为该会话没有可用的语言环境.要更改语言环境,您需要捕获用户的输入并验证global.config.php中提供的支持的可用语言环境.因此,为了进行更改,您可能需要添加一个控制器操作,以捕获用户的输入并设置新的语言环境.无需任何形式使用的控制器操作示例!

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.

这篇关于Zend Framework 2-如何制作语言切换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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