2015-04-05 58 views
3

我剛開始使用symfony2並面臨一些困難,需要像你們這樣的專家提供幫助。Symfony2:如何設置默認的語言環境和不區分大小寫的翻譯

問題1:如何設置默認語言環境和備用語言環境,以便人們嘗試點擊諸如www.example.com/home或www.example.com/fr/home(不支持的語言環境)之類的內容將重定向到www.example.com/cn/home?

我讀過很多symfony2文檔(http://symfony.com/doc/current/book/translation.html),也搜索谷歌,但我仍然無法使它的作品。不僅缺省語言環境不工作,而且備用語言環境也不起作用。例如,當用戶嘗試輸入我的應用不支持的www.example.com/fr/home時,它會假定將用戶重定向到www.example.com/cn/home,但未能這樣做。

我也嘗試清除緩存,但它似乎不工作。

問題2:如何在不區分大小寫的情況下匹配翻譯鍵?

例如,在我的翻譯文件( 「message.cn.yml」)包含

Welcome: 歡迎 

所以在我的樹枝模板,我使用{{ '歡迎' | trans}}它會幫助我轉換爲歡迎,但是當我使用{{'歡迎'| trans}}它不會轉換。有沒有什麼辦法可以使它不區分大小寫,所以我不需要在message.cn.yml文件中特意添加「welcome」:歡迎「。

非常感謝您的幫助和建議。謝謝。

下面是我的應用程序文件,供您參考:

翻譯文件:

enter image description here

參數文件:

enter image description here

配置文件:

enter image description here

嫩枝文件:

在特定束( 「Acme公司/ DemoBundle」)

enter image description here

路由文件enter image description here

這裏是我打

  1. 本地主機/現場/網絡/ app_dev結果。PHP/

enter image description here

  • 本地主機/區域/網絡/ app_dev.php/EN
  • enter image description here

  • localhost/locale/web/app_dev.php/cn
  • enter image description here

  • 本地主機/區域/網絡/ app_dev.php/FR
  • enter image description here

    回答

    0

    可以爲一個請求設置的地點:

    $this->container->get('request_stack')->getCurrentRequest()->setLocale($locale); 
    

    並且您可以使用請求偵聽程序設置每個請求的區域設置: http://symfony.com/doc/current/cookbook/service_container/event_listener.html#request-events-checking-types

    關於第二個問題,您可以添加轉換爲小寫:

    {{ 'WELCOME'|lower | translate}} 
    

    ,或者您可以創建在做翻譯和其他的東西之間的你自己定製的樹枝延伸: http://symfony.com/doc/current/cookbook/templating/twig_extension.html