Note that there are some explanatory texts on larger screens.

plurals
  1. POI18N with Express/Jade: Strings with embedded tags and interpolation
    text
    copied!<p>tl;dr: I am looking for an elegant method of doing internationalization in Jade templates. Essentially the problem boils down to that I have to do interpolation on strings in variables, instead of verbatim code.</p> <p>Problem details: </p> <p>In a monolingual Jade template I can just make an element with embedded tags and variables like so:</p> <pre><code>p Posted by span.author= post.author | on span.date= post.author </code></pre> <p>to get something like </p> <pre><code>&lt;p&gt;Posted by &lt;span style="author"&gt;The Author&lt;/span&gt; on &lt;span style="date"&gt;2012-03-08&lt;/span&gt;&lt;/p&gt; </code></pre> <p>But when I want to internationalize this, I need a single string, since word order is not the same in every language. Also, I would like to hide html details from translators, and just give them a line like this:</p> <pre><code>Posted by #{author} on #{date} </code></pre> <p>Now when I pass the i18n-ed version of this string to the Jade template as i18n.posted_by_on, it won't do interpolation on it, so the best I can do is:</p> <pre><code>- var author = '&lt;span class="author"&gt;$&lt;/span&gt;'.replace('$',post.author); - var date = '&lt;span class="date"&gt;$&lt;/span&gt;'.replace('$',post.date); - var header = i18n.posted_by_on .replace('#{author}',author) .replace('#{date}',date); p!= header </code></pre> <p>This mostly defeats the point of the nice Jade templating, as I have to do all the interpolation manually. Is there any method doing it more nicely, compactly, readable?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload