2015-01-12 89 views
3

工作,我必須與位於FooBundle/Resources/translations/messages.fr.xlf翻譯不Symfony2的

例法語翻譯文件:

<?xml version="1.0"?> 
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> 
    <file source-language="en" datatype="plaintext" original="file.ext"> 
     <body> 
      <trans-unit id="1"> 
       <source>Foo</source> 
       <target>Bar</target> 
      </trans-unit> 
     </body> 
    </file> 
</xliff> 

,但我似乎可以不作任何翻譯工作,無論是在控制器:

// FooBundle/Controller/DefaultController.php 
/** 
* @Route("/") 
* @Template() 
*/ 
public function indexAction(Request $request) 
{ 
    $request->setLocale("fr"); 
    $translatedMessage = $this->get('translator')->trans('Foo'); 

    echo $translatedMessage; 

    return array(); 
} 

或者樹枝模板:

// FooBundle/Resources/views/Default/index.html.twig 
{{ 'Foo'|trans }} or {% trans %}Foo{% endtrans %} 

它始終顯示Foo(原始字符串)。

作爲我的默認語言環境,我使用英語('en')。我從config.yml區域配置:

framework: 
    translator: { fallback: "%locale%" } 
    default_locale: "%locale%" 
    ... 

我試圖清理緩存,但它並沒有任何區別。

如果我嘗試調試的翻譯,它表明正在使用它們:

$ php app/console debug:translation fr FooBundle 

+----------+---------------+----------------------+ 
| State(s) | Id   | Message Preview (fr) | 
+----------+---------------+----------------------+ 
|   | Foo   | Bar     | 
+----------+---------------+----------------------+ 

任何想法是怎麼回事錯在這裏?

+2

剛剛測試您的代碼沒有修改(Symfony 2.6.3),它的工作沒有問題。嘗試輸出框架包的設置,並檢查與翻譯器服務相關的所有內容是否正確。 – COil

回答

0

看來問題是我安裝了另一個包(LuneticsLocaleBundle)。

不知何故,它必須重寫$request->setLocale("fr");我在控制器中做的事情,結果我使用en_US作爲語言環境。這就是爲什麼它沒有顯示翻譯。