2

I'm implementing translation on my Symfony3 app. I read the book and implemented the _locale var at the beginning of my route. I've also set a redirection from / to /en, and define the translator fallbacks.

This is working as intend, except for FOSUserBundle route. Since they aren't define on my own routing, they don't take care of the {_locale}, and they reset to the default language when I visit them. (/de/mypage => /profile => /en/mypage)

My translation files are stored in app/ressources/translations, and all my translation are done twig side.

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

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

I saw in the book it was advised to set the translation in controller, but in my case, I got some, and don't think it's valuable to repeat in each of them the language check.

I have also thinking of store the _locale var in session, and put a globale listener, but seems a bit too much, so i think i am probably missing something.

So, how to handle globale language throught URL, including on FOSUserBundle route, without repeat code in each controller?

Actually, my FOSRout are like that. fos_js_routing: resource: "@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%'
Bart Bartoman
  • 756
  • 4
  • 14

1 Answers1

0

Did you set these parameters in the framework section of the config.yml file :

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

And you need to prefix the FOSUserBundle routes too in the routing.yml file like this :

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
fgamess
  • 1,276
  • 2
  • 13
  • 25
  • Yes I implemented the fallback and default_locale params. About route, I tried, but I still got problems. Same apply when I try to change the current language – Bart Bartoman Jun 23 '16 at 06:48
  • Is it possible to see all your routing.yml with FOSUser route ? – fgamess Jun 23 '16 at 17:57
  • Ok I see. Try to use values like en|fr|de (for example) instead of %app_locales%. Maybe your routing file cannot handle vars from your parameters.yml file – fgamess Jun 24 '16 at 11:51
  • `%app_locales%` = `en|fr|de`. Working properly, except the routing. I managed to solve the issue using a LocaleListener, but still, imo, a bit overcoded for that :) – Bart Bartoman Jun 24 '16 at 12:33