Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

php - Zend framework 2 - How to make a language switcher

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ApplicationModule.php you can add a method which sets the default ZendTranslatorTranslator:

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();

        // ZendSessionContainer
        $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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...