Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(){
    }
});
like image 489
Benjamin Lucas Avatar asked Apr 29 '15 07:04

Benjamin Lucas


1 Answers

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');
like image 142
pcm Avatar answered Oct 03 '22 09:10

pcm