Search code examples
phpsymfonyrouteslocalemultilingual

Symfony 2.3 different locales on different paths


I need to achieve different locales on different paths. Now exists web page under path / and admin page under path /admin.

Admin user to have EN or FR locale (depends on what he chosen) on web page and only EN in admin page. Locale cannot be contained in a URL path.

The issue is that when logged admin checks web page with FR and goes on admin page which is on EN, the web page has to be on FR when the admin comes back.

I am thinking about storing separate locales for web and admin pages in session and read them depending on path.

Any suggestions without hardcoding or a better approach?


Solution

  • You can add requirements for the route to define list of possible locales.

    See Adding Requirements section for Routing

    # app/config/routing.yml
    homepage:
        path:      /{_locale}
        defaults:  { _controller: AppBundle:Main:homepage, _locale: en }
        requirements:
            _locale:  en|fr