0

We have trouble with the format of our text display on our website. In the meta head we have defined utf8 but somehow our text - coming from external source - is looking like this:

O próximo station d'essence

So we think that the external source is not converting it correctly.

How would be the correct converting into utf8 in javascript ? We've tried now a lot functions to do this but it seems not to be possible to convert O próximo station d'essence to O próximo station d'essence with the default methods we've found (escape, unescape, decodeURIComponent, encodeURIComponent, ...)

TJR
  • 6,307
  • 10
  • 38
  • 64
  • Instead of appending the values as text, why not try appending it to your element as html? – hack3rfx Dec 09 '16 at 14:24
  • Dont know if I understanding you - what do you mean ? – TJR Dec 09 '16 at 14:25
  • Well, if ' is displaying as text, that must mean you're applying this data (string) to whatever location as plain text. $(el).text(val). Have you tried applying it as html? $(el).html(val); ? – hack3rfx Dec 09 '16 at 14:26
  • `"O próximo station d'essence"` works for me, try adding the double quotes to the string. – Kevin Kloet Dec 09 '16 at 14:26
  • 2
    Those are HTML entities. You want to *decode HTML entities*. None of the functions you tried do that. – deceze Dec 09 '16 at 14:26
  • 2
    Also, you don't want to decode the HTML entities. What you want to do is fix your method of inserting these kinds of strings into the page. These strings are perfectly fine. They way you handle them probably is not. – Tomalak Dec 09 '16 at 14:28
  • Thanks for the hint with the decode HTML entities. I've found this article: http://stackoverflow.com/questions/5796718/html-entity-decode This helps for now - but you're correct the problem is that we are doing smth else wrong so we have to check other problems first. Thanks! – TJR Dec 09 '16 at 14:37
  • If you ask another question that shows where the strings come from and how they are being processed etc, then it's probably easy to sort out. I would consider manual HTML entity decoding a code smell. Either you are storing HTML-encoded data in the database instead of the actual data, or you are processing data improperly—like parsing HTML with regex—or you are handling them incorrectly on the client side. There are many possibilities, but it would be worthwhile to solve it in the right way instead of fixing the symptoms - and manual entity decoding is just that. – Tomalak Dec 09 '16 at 16:51
  • 1
    The "problem" is i18next. I thought the problem is the source, but from the source we get the string correctly. But when add strings like that as variables (for interpolation) i18next is escaping it. With a small fix i18next will not escape these strings so this is our fix for now. ( {{myVar}} => {{- myVar}}) source: http://i18next.com/translate/interpolation/ – TJR Dec 09 '16 at 16:55

0 Answers0