title: Symfony 4 How to change language Step by Step
body: <p>I can not understand the page translation mechanism. Translations for a specific language I keep in the yml file Translation can be obtained in two ways:</p>
  
  <ol>
  <li>Holding the currently selected translation in the session</li>
  </ol>
  
  <p>How can I change my language? The documentation even gives you a ready code <a href="https://symfony.com/doc/current/session/locale_sticky_session.html" rel="nofollow noreferrer">https://symfony.com/doc/current/session/locale_sticky_session.html</a></p>
  
  <p>Only at what point does the language change? I create a separate controller to which I pass a parameter using GET and execute the code in the controller?</p>
  
  <pre><code>$lang = $request-&gt;query-&gt;get('lang', null);
  if(!is_null($lang)) { 
      request-&gt;setLocale($lang);
  }
  return $this-&gt;redirect($this-&gt;generateUrl('index.index'));
  </code></pre>
  
  <p>In order to not write the same code in any controller, I should have one controller that only changes the language and returns the page to the previous one. Only returning to the previous page via redirect does not pass the Request with a new language only the class given in the documentation uses the default language.</p>
  
  <ol start="2">
  <li>Each time providing a translation for the page in the link</li>
  </ol>
  
  <p>The first problem that I encounter is having the default routing for the @Route ("/") home page will not pass me localhost:8000/en/ because Symfony will look for the en controller, which is the choice of language, not the controller. The solution is to provide the path localhost:8000/index/en But at this moment I'm blocking the possibility of displaying the page by giving only the address of the page. The @Route option ("/{_ locale}", defaults = {"_locale" = "en"}) does not work and I do not understand why, because I always have to specify pl / en etc. to display the language.</p>
  
  <p>When reading the documentation, I should do the following:</p>
  
  <ol>
  <li>Configuration in the file config / packages / translation.yaml</li>
  </ol>
  
  <p>default_locale: 'en' &lt;- specifies the default language
  translator: ~ 
  fallbacks: ['en'] &lt;- defines which language will be loaded if you provide an incorrect language via _locale</p>
  
  <ol start="2">
  <li>File: config / services.yaml parameters: locale: 'en'</li>
  <li><p>The config / translation.yaml file</p>
  
  <p>framework:
  default_locale: 'en'
  translator: ~</p></li>
  </ol>