2015-11-08 67 views
0

我無法弄清楚爲什麼當我用Symfony進行拼寫時,它返回的是關鍵字,而不是被傳遞的信息。我使用的symfony 2.7,這是我的配置: 我讓翻譯在我config.yml文件:翻譯symfony 2返回鍵不是翻譯

translator:  { fallback: ["%locale%"] } 

這是我的控制器內部功能:

/** 
* @Route("/consult", name="showConsult") 
* @Template("AppBundle:Admin:consult.html.twig") 
*/ 
public function showConsult(Request $request) 
{ 
    $request->setLocale('fr'); 
    var_dump($this->get('translator')->trans("login.version")); 
    return array(); 
} 

這是我的在翻譯應用程序/資源/文件翻譯

#messages.fr.yml 
login.version: Version APP 

我與樹枝功能也試過:

{{"login.version"|trans}} 
{%trans%}'login.version'{%endtrans%} 

編輯:我也有明確的chache EDIT2:好的,我有發現$請求 - >的setLocale()不工作。如果我強制config.yml中的語言環境,它會起作用。你知道我該如何解決這個問題嗎?我已閱讀this,但他們沒有爲我工作。我正在使用Windows 10 輸出始終是關鍵。我得到「login.version」而不是「版本APP」 任何人都可以幫助我嗎?在此先感謝

+0

該文件位於何處?確保它匹配所需的結構http://symfony.com/doc/current/book/translation.html#translation-resource-file-names-and-locations;嘗試刪除yml的所有部分,並確保其中沒有yml解析錯誤;也嘗試設置「翻譯:〜」,所以它使用默認的 – nixoschu

+0

看起來很明顯,但以防萬一:你是否清理了緩存('php app/console cache:clean --env = dev')? –

+0

什麼文件?我已經寫了translations.yml文件的路徑。我也試過在mybundlefolder/Resources/translations中。 config.yml文件是app/Resources/config。我曾與譯者一起嘗試過:〜並沒有工作。 – user5539797

回答

0

你的YAML文件應該是這樣......

#messages.fr.yml 
login: 
    version: Version APP 
+1

實際上並不重要,如果login.version或像你這樣的子元素。你實際上可以使用完整的字符串,如「登錄版本」:「我的翻譯」以及。這只是「乾淨」的方式;) – nixoschu

+0

非常真實..我的壞。 – qooplmao

+0

是的,仍然無法正常工作。 – user5539797

0

我知道這是一個很長的時間,但我張貼它作爲一個參考,因爲我只是今天拿到了類似的問題。

我必須實現監聽器設置語言環境值,如http://symfony.com/doc/current/session/locale_sticky_session.html

基本上是說,你必須:

1)創建一個Listener

// 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 = 'en') 
    { 
     $this->defaultLocale = $defaultLocale; 
    } 

    public function onKernelRequest(GetResponseEvent $event) 
    { 
     $request = $event->getRequest(); 
     if (!$request->hasPreviousSession()) { 
      return; 
     } 

     // try to see if the locale has been set as a _locale routing parameter 
     if ($locale = $request->attributes->get('_locale')) { 
      $request->getSession()->set('_locale', $locale); 
     } else { 
      // if no explicit locale has been set on this request, use one from the 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', 15)), 
     ); 
    } 
} 

2)註冊那Listenersrc/AppBundle/Resources/config/config-yml

services: 
    app.locale_listener: 
     class: AppBundle\EventListener\LocaleListener 
     arguments: ['%kernel.default_locale%'] 
     tags: 
      - { name: kernel.event_subscriber } 

3)在您的任何請求中,Symply都會添加_locale參數,並且該區域設置對於所有用戶的工作會話都將變得粘稠。