Search code examples
javascriptsymfonytwigurl-routing

How to pass locale using FOSJsRoutingBundle


I installed FOSJsRouting but my route need _locale parameter. How can I do that in JS ? Is there a way to get the locale in js ?

In twig, I can do app.request.attributes.get('_locale') but in JS, I don't find any documentation.

AJAX

$.ajax({
    type : 'get',
    url : Routing.generate('get_credits', {'_locale': app.request.attributes.get('_locale'), 'amount' : amount, 'monthNumber' : month }),
    beforeSend : function(){
    console.log('loading');
    },
    success: function(data){
    },
    error : function(){
    }
});

Solution

  • What I usually do is set the locale as a html-tag attribute

    <html lang="{{ app.request.locale }}"> 
    

    which results in

    <html lang="EN">
    

    This is also useful for SEO, and you can always get the locale in JS by calling

    $('html').attr('lang');