2
我想从菜单中更改语言(设置区域设置)。翻译模块已配置。因此,前者的代码。 :$this->translate('Some Text', 'default', 'de_DE') ?>
正在工作。但我需要从菜单中更改语言。我使用Zend/I18n。从菜单中设置语言
我想从菜单中更改语言(设置区域设置)。翻译模块已配置。因此,前者的代码。 :$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');
}
我试图安装MvcTranslator但我得到'模块(Zend的\的mvc \ I18N)无法initialized.'错误消息 我添加了''Zend \ Mvc \ I18n','到modules.config.php 如你所说的改变Module.php和IndexController 我是否应该在module.config.php中更改smth? –
你需要将它添加到composer.json并运行作曲家更新然后作曲家安装 – code9monkey
和最后一件事我的路由是这样的: lang'=> [ 'type'=> Segment :: class, 'options '=> [ 'route'=>'/:lang', 'defaults'=> [ 'controller'=> Controller \ IndexController :: class, 'action'=>'index', 'lang' =>'en', ], ], ],' 但据我的理解,我需要改变它以及吗? –