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.

Share:
32,760

Related videos on Youtube

Seriescw Esxille
Author by

Seriescw Esxille

Updated on December 11, 2020

Comments

  • Seriescw Esxille
    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
    CappY over 9 years
    Hello, what's difference between $this->get('request')->getLocale() and $this->get('translator')->getLocale() ?
  • Azam Alvi
    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
    petekaner almost 9 years
    what about getting it in a form type? if i try to inject the request to a form type I got an error
  • Azam Alvi
    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
    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
    mapmalith over 7 years
    Hi, when we use the above code from where it is getting the locale value?
  • Azam Alvi
    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
    mapmalith over 7 years
    thanks for your comment. what I asked was what is the resource of getLocale()
  • AlexioVay
    AlexioVay over 4 years
    Simply use $request->getLocale(); in Symfony 4.

Related