編輯:清除緩存
有時Symfony的具有太強的緩存機制。如果您先不使用語言環境,然後將控制器和樹枝修改爲多語言環境,並且還添加了新消息文件,則Symfony會加載所有新控制器/樹枝,但不知何故會錯過有新消息文件。簡單的緩存清除,在您添加消息文件後立即完成(!),解決了這個問題。因此,每當您更改翻譯時,您都不需要清除緩存。
請嘗試此作爲最後的手段。如果這沒有幫助我建議你仔細閱讀文檔(在這個答案的最後一行鏈接):
app/console cache:clear --env=dev
您可以嘗試一個或所有這些,這是不可能的(!)告訴發生了什麼問題。
{{ yearsCount|trans }}
{{ 'site.dates.years'|trans }}
看看這些翻譯。如果第二個,你設置變量錯了,不知何故。
如果沒有,通過_locale
沿着路線:
defaults: { _controller: Bundle:Controller:action, _locale: de }
或者將其作爲路由參數:
path: /a/path/{_locale}/whatever
defaults: { _controller: Bundle:Controller:action }
requirements:
_locale: en|de
打印的語言環境,以確保它在樹枝正確設置:
{{ app.request.locale }}
{{ app.request.getLocale() }}
如果語言環境打印良好,但翻譯仍不起作用,這是因爲S ymfony沒有找到標識符。轉至翻譯文件,並檢查它的正確寫入:
site:
dates:
years: Jahre
如果您在messages.de.yml
找到它,那麼也許它的文件錯了位置。裝載機這些文件搜索:
Symfony looks for message files (i.e. translations) in the following default locations:
- the app/Resources/translations directory;
- the app/Resources/MyAppBundle/translations directory;
- the Resources/translations/ directory inside of any bundle.
如果這些文件擺弄周圍不會產生結果,它在config.yml
配置錯誤,或者許多重疊錯誤(以上所有的?)對你的工作。
下面是關於翻譯的所有疼痛/信息的來源,但您現在可能已經記住了每個段落:Symfony translations。
來源
2017-06-29 06:08:52
pid
感謝Nicolae,但如果我這樣做,我得到另一個錯誤:https://stackoverflow.com/questions/7811162/symfony2twig-variable-in-translation-return-a-message-must-be-a-simple-text這是爲什麼我問這個問題...這裏有點腥 –
有點討厭,但是如果你這樣做(我沒有自己測試過): '{%trans {{from「messages」into { 「%} {{yearsText}} {%endtrans%}' – nicolaenichifor
剛剛對它進行了測試,上面顯然不適用於動態值。 – nicolaenichifor