-3

I have a javascript variable that fills with Laravel translation system:

'are_you_sure' => '¿Remplacer l\'arbre?',

and then in JS:

swal({
          title: "{{ trans('msg.are_you_sure') }}",
          ...

    });

And it appears to me:

¿Remplacer l'arbre?

EDIT:

Sorry for htmlspecialchars, the thing is it doesn't change removing htmlspecialchars, adding it, or adding htmlspecialchars_decode

Cœur
  • 32,421
  • 21
  • 173
  • 232
Juliatzin
  • 14,437
  • 26
  • 115
  • 241

5 Answers5

1

htmlspecialchars takes a plain text string and turns it into HTML.

i.e. exactly the opposite of what you want it to do

Look at htmlspecialchars_decode instead.

Beware " and new line characters which will break the JS syntax.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
1

htmlspecialchars() is what is causing your ' to turn into the html translation(&#039). Just removing it should do the trick.

Source: http://php.net/manual/en/function.htmlspecialchars.php

JCDR
  • 11
  • 1
1

I finally found it. I had to replace {{ }} by {!! !!}

Juliatzin
  • 14,437
  • 26
  • 115
  • 241
0

I had the same problem, this worked for me:

use {{ 'key' |trans |e('js') }} 

Source: https://twig.symfony.com/doc/2.x/filters/escape.html

Dharman
  • 21,838
  • 18
  • 57
  • 107
thierry
  • 1
  • 1
-1

I think you can use unescape() Function to convert the code into a string, i left you a web here https://www.w3schools.com/jsref/jsref_unescape.asp

  • `unescape` is (a) deprecated due to broken unicode support and should not be used and (b) unescapes the wrong kind of escaping for this problem – Quentin Sep 01 '17 at 14:08
  • W3Schools are [much better than they once were](http://www.w3fools.com/) which means they are no worse that *awful*. You should avoid recommending them. – Quentin Sep 01 '17 at 14:09
  • Thanks @Quentin for the advice. – Ricardo diaz Sep 01 '17 at 14:18
  • @quentin But if you read in the same line where it says The unescape() function was deprecated in JavaScript version 1.5. You have the answer to. Use decodeURI() or decodeURIComponent() instead. – Ricardo diaz Sep 01 '17 at 14:53