2

I am calling google translate api and getting back strings that are not fully decoded. In particular, I am seeing ' where single quotes should be.

For example:

{
  "q": "det är fullt",
  "target": "en"
}

Returns

{
   "data": {
      "translations": [
       {
        "translatedText": "It&\#39;s full",
        "detectedSourceLanguage": "sv"
      }
    ]
  }
}

I would have expected JSON.parse to take care of this, but it does not. Is there some other native function I need to be calling? My current fix is to fix this using a regex .replace(/'/g, "'");, but is there a better way to decode this type of thing using javascript?

Negatar
  • 637
  • 8
  • 14
  • 1
    *"I would have expected JSON.parse to take care of this"* JSON has nothing (and should have nothing) to do with HTML. – Felix Kling Jun 14 '17 at 15:21

1 Answers1

2

Aha! The issue is caused because the response is HTML encoded.

If I were to put the translation onto the page directly, the quote renders just fine. However, I am putting the result in a textarea to give users a chance to edit that translation. As a result, the browser is not automatically reading the string as HTML since it is not rendered directly as HTML.

The solution I am now using is to decode the string using DOMParser as described on this stackoverflow thread:

var encodedStr = 'hello & world';

var parser = new DOMParser;
var dom = parser.parseFromString(
    '<!doctype html><body>' + encodedStr,
    'text/html');
var decodedString = dom.body.textContent;

console.log(decodedString);
Negatar
  • 637
  • 8
  • 14