How to translate inline javascript?

196 views Asked by At

So I'm working on a django project and we're having English translated to Czech (not important). I can do fine with regular tags being translated with {% trans 'sample text' %}

However, I have a button inside a link and on click, this link returns a confirmation. The full html looks like this

<a href="{% url 'foo' %}" onclick="return confirm(
       'Sample text')">
   <button>
       {% trans 'Regular trans tag' %}
  </button>
</a>

Now I would like to somehow translate the 'Sample text' text inside the confirm, however when I try it with {% trans %} it gives me error that 'foo/bar expected (js)'

1

There are 1 answers

0
Uri On

Translate "Sample text" inside the JS:

<a href="{% url 'foo' %}" onclick="return confirm('{% trans "Sample text" %}')">
   <button>
       {% trans 'Regular trans tag' %}
  </button>
</a>

Or there are other options, if you need all the languages to be available on the client at runtime. Do you need this?