在FOSUserBundle路由中未使用的Symfony3本地语言

时间:2016-06-22 07:26:43

标签: translation fosuserbundle symfony

我正在使用我的Symfony3应用程序实现翻译。我读了the book并在路线的开头实施了_locale var。我还设置了从//en的重定向,并定义了翻译后备。

除了FOSUserBundle路由之外,这是按预期工作的。由于它们没有在我自己的路由中定义,因此它们不会处理{_locale},并且当我访问它们时它们会重置为默认语言。 (/de/mypage => /profile => /en/mypage

我的翻译文件存储在app/ressources/translations中,我的所有翻译都在twig方面完成。

app_localized:
    resource: "@AppBundle/Controller/"
    type:     annotation
    prefix:   /{_locale}

app:
    path: /
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /en
        permanent: true

我在书中看到建议在控制器中设置翻译,但在我的情况下,我得到了一些,并且不认为在每个语言检查中重复这些是有价值的。

我还考虑在会话中存储_locale var,并设置一个全局监听器,但看起来有点太多了,所以我想我可能会遗漏一些东西。

那么,如何通过URL处理全局语言,包括在FOSUserBundle路由上,而不需要在每个控制器中重复代码?

实际上,我的FOSRout就是这样。 fos_js_routing:     资源:“@ FOSJsRoutingBundle / Resources / config / routing / routing.xml”

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"
    prefix: /{_locale}
    requirements:
        _locale: '%app_locales%'

fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /{_locale}/profile
    requirements:
        _locale: '%app_locales%'

fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /{_locale}/register
    requirements:
        _locale: '%app_locales%'

fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /{_locale}/resetting
    requirements:
        _locale: '%app_locales%'

fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /{_locale}/profile
    requirements:
        _locale: '%app_locales%'

1 个答案:

答案 0 :(得分:0)

您是否在config.yml文件的framework部分设置了这些参数:

framework:
    translator:      { fallbacks: ["%locale%"] }
    default_locale:  "%locale%"

你需要在routing.yml文件中为FOSUserBundle路由添加前缀,如下所示:

fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"
    prefix: /{_locale}/
    requirements:
        _locale: en|fr
fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /{_locale}/profile
fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /{_locale}/register
fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /{_locale}/resetting
fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /{_locale}/profile