从菜单设置语言

Set language from menu

我想从菜单更改语言(设置语言环境)。翻译模块已配置。所以前代码。 : $this->translate('Some Text', 'default', 'de_DE') ?> 正在工作。但我需要从菜单更改语言。我用了 Zend/I18n.

这就是我实现从菜单更改语言的方法。您将需要安装 Zend MvcTranslator 组件

public function changeLanguageAction()
{
    $language = $this->params()->fromRoute('lang', 'en');
    //CAN USE PHP setcookie() instead of this
    $this->cookieService->createCookie('xuage', $language, $this->getResponse()->getHeaders());
    //redirect to homepage
    $this->redirect()->toRoute('home');
}

Module.php

public function onBootstrap(Event $e)
{
    $app = $e->getParam('application');
    $em = $e->getApplication()->getEventManager();

    //Translation
    $this->initTranslator($e);
}

protected function initTranslator($event)
{
    $serviceManager = $event->getApplication()->getServiceManager();

    $lang = @$event->getRequest()->getCookie()->xuage;

    //if language is not set in the cookie, set the default language to english
    if (!$lang) {
        $lang = 'en';
    }

    $translator = $serviceManager->get('MvcTranslator');
    $translator
        ->setLocale($lang)
        ->setFallbackLocale('en');
}