2017-06-26 54 views
0

我嘗試用用戶語言翻譯網站。Symfony翻譯只顯示英文

在我config.yml:

parameters: 
    locale: en 

framework: 
    #esi: ~ 
    translator: { fallbacks: [en] } 

我加入messges.en.yml下的應用程序/資源/翻譯messages.it.yml。

當我訪問與意大利瀏覽器的頁面,我看到英文翻譯。

繼symfony的書,我加入這個活動的用戶:

namespace AppBundle\Service; 

use AppBundle\Controller\TokenAuthenticatedController; 
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpKernel\KernelEvents; 

class LanguageListener implements EventSubscriberInterface 
{ 

    public function onKernelController(FilterControllerEvent $event) 
    { 
     $request = $event->getRequest(); 
     $user_lang = $request->getPreferredLanguage(); 
     $request->setLocale($user_lang); 
    } 

    public static function getSubscribedEvents() 
    { 
     return array(
      KernelEvents::CONTROLLER => 'onKernelController', 
     ); 
    } 
} 

,但我仍然只能看到英文...

一些提示?

+0

清理緩存? 'rm -rf app/cache/*' – ste

+0

你確定'$ user_lang'有合適的值嗎?也許'getPreferredLanguage'沒有按預期工作,並且你正在擊中你的「後備」邏輯? –

回答

0

你需要創建一個定義路線_locale這樣

@Route("/{_locale}/post/list", name="post_index", defaults={"_locale": "en"}, requirements={"_locale": "en|fr|nl" }) 

不要忘了導入路由類註釋和清除緩存。