设置symfony2翻译

时间:2015-06-25 17:36:01

标签: php symfony frameworks translation

我想在sympfony 2中为我的应用创建翻译。我创建了这样的选择器:

<li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Language <b class="caret"></b></a>
            <ul class="dropdown-menu alert-dropdown">
                <li><a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'sr'})) }}">Serbian</a></li>
                <li><a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale': 'en'})) }}">English</a></li>
            </ul>
        </li>

现在在我的网址中我得到了... ?_locale=en,我的文件夹结构也是如此:

MyAppBudnle/
-Resources
--translations
---messages.sr.yml - File with translations.

而且在mu App/config/config.yml我有这一行:

framework:
    #esi:             ~
    translator:      { fallback: en }

...

你现在能告诉我,我还需要创造什么,翻译才能开始运作。我的应用程序是英文版,当我将其更改为SR时,它应该是塞尔维亚语。

谢谢。

1 个答案:

答案 0 :(得分:0)

You should handle locale change in event like onKernelRequest and pass locale to session (if needed - you can prepare your routes to handle _locale part, router should handle it automatically) and pass it to Request. Every text written in app should be translated of course with {{ "lorem ispum"|trans }}. Good luck!