Score:1

How to translate a link defined in javascript with Drupal.t()

cn flag

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!

Score:2
de flag

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)

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.