how to get the current locale in symfony 2.3?
32,760
Solution 1
You can get current locale by this
$request = $this->get('request');
echo $request->getLocale();
Solution 2
Simply use $request->getLocale();
in Symfony 4 since this seems to the top link when you search in Google for this topic.
Related videos on Youtube
Author by
Seriescw Esxille
Updated on December 11, 2020Comments
-
Seriescw Esxille over 3 years
How can I get the curent locale in Symfony 2.3 ?
I have a url like this:
/{_locale}/blog/article
with FR as a default parameter. I try the following:$this->get('request')->getLocale()
but it always give me the default parameter.And i am aware that i can use
$this->get('session')->set('_locale', 'fr');
but the problem is that when the user first visit my website he has nothing stored in his session.
-
CappY over 9 yearsHello, what's difference between $this->get('request')->getLocale() and $this->get('translator')->getLocale() ?
-
Azam Alvi over 9 years@CappY basically both will return the current locale but
get('translator')
can be used for more functionality like you can get translation for desired language by providing the locale. -
petekaner almost 9 yearswhat about getting it in a form type? if i try to inject the request to a form type I got an error
-
Azam Alvi almost 9 years@petekaner you can send it from controller when you prepare your form by passing an array in form (2nd parameter)
-
Snow almost 8 years$this->get('request') is depreciated, I suggest to use "get('translator')" now or to add Request $request as hint in your function
-
mapmalith over 7 yearsHi, when we use the above code from where it is getting the locale value?
-
Azam Alvi over 7 years@PM-Riabel you will use this code when u need it. I did not get your actual point. What u want to say. Please give more info.
-
mapmalith over 7 yearsthanks for your comment. what I asked was what is the resource of
getLocale()
-
AlexioVay over 4 yearsSimply use
$request->getLocale();
in Symfony 4.