I have a template string that contains a link, which I want to translate afterwards throught the Drupal UI:

var html = `<p>${Drupal.t('<a href="/node/16">Check this out</a>')}</p>`

Two questions about that:

  1. What is the correct way to build links in JS that could be translatable, (both the anchor text and the href)?

  2. What should I do with these double quotes? Should I escape them? I've noticed in the UI that if I'd escape them with backslashs, each backslash '\' is converted to a triple baskslash '\\\', which makes no sense for me.

Any help will be very appreciated!

1 Answer 1


I would do this:

// Create a <p> tag, append an <a> tag, and make the
// link text translatable:
let html = $("<p/>").append(
  $("</a>", {href:"/node/16"}).text(Drupal.t("Check this out"))

After that you should clear the cache once, and go to the translate page (/admin/config/regional/translate)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.