Reading the documentation of symfony 3, I got very confused, and I'm not sure if I'm doing everything right. This is how one of my normal controller looked at the start:
class IndexController extends Controller
{
/**
* @Route("/", name="index")
*/
public function indexAction(Request $request)
{
$articles = $this->getDoctrine()
->getRepository(Article::class)->findAll();
return $this->render("index.html.twig", array(
'articles' => $articles
));
}
}
I wanted to add option for multy language. Before symfony I saw doing this with simple saving in session the language, and a button for changing it. In symfony I added a translations folder, and a file for each language.
//messages.en.yml
base.menu.1: Home
base.menu.2: Products
base.menu.3: Brands
//messages.bg.yml
base.menu.1: Начало
base.menu.2: Продукти
base.menu.3: Марки
After this I saw in some titorial that I can add my _locale, in my route like this:
@Route("{_locale}/", name="index")
And this actually worked. My I could of change my home page's language by going in bg/, or en/.
But the locale variabe wasn't saved anywhere. If I went to other page, it doesn't know what language was setted up before. So I read more, and was I can fix this, by saving all of my routes in routing.yml like this:
index:
path: /{_locale}/
defaults: { _controller: AppBundle:Index:index }
requirements:
_locale: '%app.locales%'
and then set up my config:
parameters:
locale: bg
app.locales: bg|en
framework:
translator: { fallbacks: ['%locale%'] }
All this is working, except I have to move my routing from their controllers to routing.yml. I want to ask if this is the right method, to do all of this, because I'm not sure, the documentation isn't 100% clear (most likely I just cant understand it), and can't find any good titorials.
use the 'make locale sticky' method, as stated above,
then set the locale using
$request->setLocale($locale);
$request->getSession()->set('_locale', $locale);
//now redirect as the locale change will take affect on the next pageload
(set both, and redirect)