简体   繁体   English

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

[英]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. 我正在开发一个Zend Framework 2应用程序,现在我想实现一个语言切换器,客户/注册用户可以从中选择他们想要的语言,我无法理解的是如何在Zend Framework 2中使用存储(不是来自网址),我想在客户选择一个时保留客户的优先语言,对于注册用户,我可以从cookie /数据库中检索优先级,并将其重新用于存储。 But where and how should I start/implement this? 但是我应该在哪里以及如何开始/实施这个? Thank you in advance. 先感谢您。

Setup your Locales in your global.config.php : global.config.php设置您的Locales

'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 : 因此,在Application\\Module.php您可以添加一个设置默认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. 所以现在默认的Locale是en_US,因为会话没有可用的Locale。 For changing the locale you need to catch the users input and validate the available locales you support, provided in your global.config.php . 要更改区域设置,您需要捕获用户输入并验证您支持的可用区域设置,在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. 希望这对您有所帮助,并可以帮助您编写一些代码,以便为您的应用程序上的注册用户保存。

I am not sure, my approach will work or not. 我不确定,我的方法是否有效。 Please try: 请试试:

We can have 3 params for translate method. translate方法我们可以有3个参数。

$translator->translate($message, $textDomain, $locale);

The $locale parameter is taken from the locale, set in the translator and that's why we usually not set manually in the code. $ locale参数取自语言环境,在转换器中设置,这就是我们通常不在代码中手动设置的原因。 So, you can use like below : 所以,您可以使用如下:

$localeVar = 'de_DE';  OR  $localeVar = 'en_US'; // according to user's selection
echo $this->translate("Translate me", $textDomain, $localeVar);

You can have a key value pair - key can be user selected language and value can be any one of the language. 您可以拥有一个键值对 - 键可以是用户选择的语言,值可以是任何一种语言。

array(
    'english' => 'en_US',
    'deutch' => 'de_DE',
    'frecnh' => 'fr_FR',
    // other language
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM