sergzach
sergzach

Reputation: 6754

Django Localization: How To Make 'po' file without HTML?

I would like my po translation file without any HTML.

So, in .po file we have:

msgid "PRODUCTS_WERE_ORDERED(%(b_sum)s)"
msgstr "Products were ordered for %(b_sum)s"

I wouldn't like the next (this is a solution but it's bad):

msgstr "Products were ordered for <b>%(b_sum)s</b>"

Finally I would like to make b_sum bold. The next code (in template) does not work:

{% with b_beg="<b>" b_end="</b>" %}
  <p>
    {% blocktrans with b_sum=sum %}PRODUCTS_WERE_ORDERED({{b_beg}}{{b_sum}}{{b_end}}){% endblocktrans %}
  </p>
{% endwith %}

After translation we have: PRODUCTS_WERE_ORDERED(350)

If we use

PRODUCTS_WERE_ORDERED({{b_sum}}) 

then we have: Products were ordered for 350 (without boldness).

Does anybody know how to make the sum bold not using HTML in po translation file?

Upvotes: 2

Views: 432

Answers (1)

sergzach
sergzach

Reputation: 6754

I found the solution by writing a custom template filter that formats a string with sum as parameter:

@register.filter( name='safewrap' )
def safewrap( val, arg ):
    return val.format( arg )
safewrap.is_safe = True

In this case we have in our template file:

{% blocktrans with sum2="<b>{0}</b>"|safewrap:sum %}PRODUCTS_WERE_ORDERED({{sum2}}){% endblocktrans %}

So our 'po' file is without HTML:

msgid "PRODUCTS_WERE_ORDERED(%(sum2)s)"
msgstr "This costs %(sum2)s"

Upvotes: 1

Related Questions