Note that there are some explanatory texts on larger screens.

plurals
  1. PO"eval is evil", but do I have an option?
    primarykey
    data
    text
    <p>I've came up with some custom localization solution for a project I'm working on. The idea is that my HTML can contain this:</p> <pre><code>&lt;h2 data-l10n="HELLO" data-l10n-params="['Visitor', new Date()]"&gt;&lt;/h2&gt; </code></pre> <p>When the page is initiated a javascript function like this runs:</p> <pre><code>localizeAll: function(sel) { var selector = sel || document, $o = $(selector); $o.find('[data-l10n]').each( function() { var $t = $(this), val = $t.attr('data-l10n'), params = $t.attr('data-l10n-params'), po = null; if (typeof params !== 'undefined') { po = eval(params); log(params, po); } var res = doLocalize(val, po); if (res[0] !== '&lt;') { $t.text(res); } else { $t.text(val); } }); } </code></pre> <p>So basically we search for any elements that have a <code>data-l10n</code>-attribute and call <code>doLocalize()</code> for each of those objects. Additionally, the element can have a <code>data-l10n-params</code>-attribute, which is just a string literal that can be parsed to an array. This string is evaluated (<code>params</code> string becomes <code>po</code> array) and <code>po</code> is supplied to <code>doLocalize()</code> as the optional second parameter. </p> <p>Hence, the output in Firebug (from <code>log(params, po);</code> statement) is:</p> <pre><code>['Vistor', new Date()] ["Vistor", Date {Thu Nov 17 2011 10:10:31 GMT+0100 (CET)}] </code></pre> <p>So yes, I'm using eval. And yes, I know that "eval is evil". But occasionally, I need to pass a parameter to <code>doLocalize()</code>.</p> <p>How could this be done without eval?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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