title: Symfony 3 Redirect All Routes To Current Locale Version
body: <p>I am working on a symfony application where my goal is no matter what page the user is on it will navigate to the locale version of the page.</p>
  
  <p>For example, if the user navigates to "/" the home page, it will redirect to "/en/"</p>
  
  <p><strong>If they are on "/admin" page it will redirect to "/en/admin"</strong>, in such a way that the <code>_locale</code> property is set from the route.</p>
  
  <p>Also it needs to determine the locale if they visit /admin from the users browser since no locale was determined so it knows which page to redirect to.</p>
  
  <p>Currently my default controller looks like below since I'm testing.  I'm using the dev mode &amp; profiler to test that translations are working in correct.</p>
  
  <p><a href="https://i.stack.imgur.com/w2nC6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w2nC6.png" alt="enter image description here"></a></p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Controller;
  
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  
  class DefaultController extends Controller
  {
      /**
       * @Route("/", name="homepage")
       * @Route("/{_locale}/", name="homepage_locale")
       */
      public function indexAction(Request $request)
      {
          $translated = $this-&gt;get('translator')-&gt;trans('Symfony is great');
  
          // replace this example code with whatever you need
          return $this-&gt;render('default/index.html.twig', [
              'base_dir' =&gt; realpath($this-&gt;container-&gt;getParameter('kernel.root_dir').'/..'),
              'translated' =&gt; $translated
          ]);
      }
  }
  </code></pre>
  
  <p>This current method will keep the user at "/" if they navigate there, but I want to have it redirect to "/en/".  This should work for other pages too, like /admin, or /somepath/pathagain/article1 (/en/admin , /en/somepath/pathagain/article1)</p>
  
  <p><strong>How would I do this?</strong></p>
  
  <p>References I've read that did not help:</p>
  
  <p><a href="https://stackoverflow.com/questions/22086755/symfony2-use-default-locale-in-routing-one-url-for-one-language">Symfony2 Use default locale in routing (one URL for one language)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7903477/symfony2-default-locale-in-routing">Symfony2 default locale in routing</a></p>
  
  <p><strong>::Update::</strong></p>
  
  <p>I have not solved my issue but I've come close as well as learned a few tricks to be more efficient.</p>
  
  <p><strong>DefaultController.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Controller;
  
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  
  class DefaultController extends Controller
  {
  
      /**
       * @Route("/", name="home", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
       * @Route("/{_locale}/", name="home_locale", requirements={"_locale" = "%app.locales%"})
       */
      public function indexAction(Request $request)
      {
          $translated = $this-&gt;get('translator')-&gt;trans('Symfony is great');
  
          // replace this example code with whatever you need
          return $this-&gt;render('default/index.html.twig', [
              'base_dir' =&gt; realpath($this-&gt;container-&gt;getParameter('kernel.root_dir').'/..'),
              'translated' =&gt; $translated
          ]);
      }
  
      /**
       * @Route("/admin", name="admin", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
       * @Route("/{_locale}/admin", name="admin_locale", requirements={"_locale" = "%app.locales%"})
       */
      public function adminAction(Request $request)
      {
          $translated = $this-&gt;get('translator')-&gt;trans('Symfony is great');
  
          // replace this example code with whatever you need
          return $this-&gt;render('default/index.html.twig', [
              'base_dir' =&gt; realpath($this-&gt;container-&gt;getParameter('kernel.root_dir').'/..'),
              'translated' =&gt; $translated
          ]);
      }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>Config.yml</strong></p>
  
  <pre><code>imports:
      - { resource: parameters.yml }
      - { resource: security.yml }
      - { resource: services.yml }
  
  # Put parameters here that don't need to change on each machine where the app is deployed
  # http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
  parameters:
      locale: en
      app.locales: en|es|zh
  
  framework:
      #esi:             ~
      translator:      { fallbacks: ["%locale%"] }
      secret:          "%secret%"
      router:
          resource: "%kernel.root_dir%/config/routing.yml"
          strict_requirements: ~
      form:            ~
      csrf_protection: ~
      validation:      { enable_annotations: true }
      #serializer:      { enable_annotations: true }
      templating:
          engines: ['twig']
          #assets_version: SomeVersionScheme
      default_locale:  "%locale%"
      trusted_hosts:   ~
      trusted_proxies: ~
      session:
          # handler_id set to null will use default session handler from php.ini
          handler_id:  ~
          save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"
      fragments:       ~
      http_method_override: true
      assets: ~
  
  # Twig Configuration
  twig:
      debug:            "%kernel.debug%"
      strict_variables: "%kernel.debug%"
  
  # Doctrine Configuration
  doctrine:
      dbal:
          driver:   pdo_mysql
          host:     "%database_host%"
          port:     "%database_port%"
          dbname:   "%database_name%"
          user:     "%database_user%"
          password: "%database_password%"
          charset:  UTF8
          # if using pdo_sqlite as your database driver:
          #   1. add the path in parameters.yml
          #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
          #   2. Uncomment database_path in parameters.yml.dist
          #   3. Uncomment next line:
          #     path:     "%database_path%"
  
      orm:
          auto_generate_proxy_classes: "%kernel.debug%"
          naming_strategy: doctrine.orm.naming_strategy.underscore
          auto_mapping: true
  
  # Swiftmailer Configuration
  swiftmailer:
      transport: "%mailer_transport%"
      host:      "%mailer_host%"
      username:  "%mailer_user%"
      password:  "%mailer_password%"
      spool:     { type: memory }
  </code></pre>
  
  <p>Notice under parameters the value <code>app.locales: en|es|zh</code>.  This is now a value I can reference whenever I create my routes if I plan to support more locales in the future which I do. Those routes are english, spanish, chinese in that order for those curious.  In the DefaultController in the annotations the <code>"%app.locales%"</code> is the part that references the config parameter.</p>
  
  <p>The problem with my current method is going to /admin for example does not redirect the user to /{browsers locale}/admin, which would be the more elegant solution to keep everything organized... but at least the routes work.  Still looking for better solution.</p>
  
  <p>****Update****</p>
  
  <p>I think I may have possibly found the answer here as the bottom answer given (<a href="https://stackoverflow.com/questions/17610234/add-locale-and-requirements-to-all-routes-symfony2">Add locale and requirements to all routes - Symfony2</a>), the answer by Athlan.  Just not sure how to implement this in symfony 3 as his directions were not clear enough to me.</p>
  
  <p>I think this article might help also (<a href="http://symfony.com/doc/current/components/event_dispatcher/introduction.html" rel="nofollow noreferrer">http://symfony.com/doc/current/components/event_dispatcher/introduction.html</a>)</p>