Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complicated quote escaping in django template translations

I'm trying to fix a bug that comes when viewing a page in French. The problem is that the French translation strings have apostrophes in them and cause the HTML strings to be closed improperly.

The offending code:

<select data-bind="
    optstr: [{value: 'none', label: '{% trans "String one" %}'},
             {value: 'open', label: '{% trans "String two" %}'}],
    value: actionType
"></select>

Is there a better way to structure this or to escape what comes back from the trans calls?

like image 309
Tyler Avatar asked Dec 30 '25 14:12

Tyler


1 Answers

I resolved this by doing the translations separately and using the escapejs filter.

{% trans "String one" as string_one %}
{% trans "String two" as string_two %}
<select data-bind="
    optstr: [{value: 'none', label: '{{ string_one|escapejs }}'},
             {value: 'open', label: '{{ string_two|escapejs }}'}],
    value: actionType
"></select>
like image 122
Tyler Avatar answered Jan 02 '26 05:01

Tyler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!