我正试图在“用户会话期间”Symfony教程中遵循“制作语言环境”,但是,我遇到了错误,我不知道为什么......
当我点击一个标志来更改语言时,会在会话中设置新语言,在重定向之后,我在“$ request-> getLocale()”中选择了语言但不是翻译 ......你对这个问题有所了解吗?非常感谢!
编辑:我只想按会话使用区域设置
LocaleListener.php
<?php
// src/AppBundle/EventListener/LocaleListener.php
namespace AppBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LocaleListener implements EventSubscriberInterface
{
private $defaultLocale;
public function __construct($defaultLocale = 'fr')
{
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
if ($locale = $request->attributes->get('_locale')) {
$request->getSession()->set('_locale', $locale);
} else {
// Si aucune locale n'a été fixée explicitement dans la requête, on utilise celle de la session
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
}
public static function getSubscribedEvents()
{
return array(
// must be registered after the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
change_language.html.twig(更改语言)
<a href="{{ path('app_change_language', {'language': key}) }}" >key</a>
LanguageController.php
<?php
namespace AppBundle\Controller;
use AppBundle\Form\Model\Language\Language;
use AppBundle\Form\Type\LanguageType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* Class LanguageController.
*/
class LanguageController extends AbstractController
{
/**
* @Route("/changelanguage/{language}", name="app_change_language", options={"expose"=true})
*
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function setLanguageAction(Request $request, $language = null)
{
$defaultLanguage = $request->getLocale();
// pour test
$languages = [
'fr' => 'French',
'gb' => 'English',
'es' => 'Spanish',
];
if(isset($language))
{
$this->get('session')->set("_locale", $language);
$url = $this->generateUrl('app_homepage');
return $this->redirect($url);
}
return $this->render('AppBundle:Language:change_language.html.twig', array(
'languages' => $languages,
'defaultLanguage' => $defaultLanguage)
);
}
}
应用程序/配置/ services.xml中
app.eventListeners.locale_listener:
class: AppBundle\EventListener\LocaleListener
arguments: ["%kernel.default_locale%"]
tags:
- { name: kernel.event_subscriber }
答案 0 :(得分:0)
按照Fracsi的回答:
DefaultController:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Cookie;
/**
* Class DefaultController.
*/
class DefaultController extends AbstractController
{
/**
* @Route("/", name="app_homepage")
*
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
$locale = $request->getLocale();
// locale = "gb" (after click on the flag and redirect)
$translator = $this->get('translator');
var_dump($translator->getLocale()); // fr
$this->get('translator')->setLocale('gb');
var_dump($this->get('translator')->getLocale()); // display "gb"
var_dump($translator->trans('menu.home')); // display translation in french...
}
答案 1 :(得分:0)
答案如下:
最后,我找到了原因......译者获得了良好的语言环境,但每次都用一个外部包(lexik翻译)取代它......现在它可以了!