Passing a JS var from AJAX response to Twig

Ad

I am trying to pass a Javascript var = value that I receive from an Ajax response to the twig |trans filter so I can translate the strings.

{{('makler.realestate::lang.tip_neprem.' ~ value)|trans}} doesn't work, because value returns empty.

  $.each( tipi_nepremicnin, function( key, value ) {
    $(select_input)
    .append(
    "<option value='" + value + "'>" 
    + '{{('makler.realestate::lang.tip_neprem.' ~ value)|trans}}'  
    + "</option>").removeAttr("disabled"
    );
  }
Ad

Answer

Ad

You can't do that.

You've got two solutions :

  1. Translate your content in PHP then pass it to ajax response
  2. Use Bazing js translation bundle so you can translate JS variables
Ad
source: stackoverflow.com
Ad