How to properly add entries for computed values to the django internationalization messages file?

前端 未结 3 1712
不思量自难忘°
不思量自难忘° 2021-01-04 08:11

Django documentation states:

The caveat with using variables or computed values, as in the previous two examples, is that Django\'s translation-stri

3条回答
  •  时光说笑
    2021-01-04 08:42

    There is one nice way of doing this! (I know, because I happened to work on the same code).

    First of all - this value is computed somewhere. So, in your action, you may have:

    context['var'] = 'good' if condition(request) else 'bad'
    

    and later in the template:

    {% if var == 'good' %}
        {% trans "Congratulations, var equals: "}
    {% else %}
        {% trans "Oops, var equals: "}
    {% endif %}
    {% trans var %}
    

    You may have different values, which can become impractical... Unless you use this trick:

    _ = lambda x: x 
    context['var'] = _('good') if condition(request) else _('bad')
    

    You need to make _ something local if you don't want to clash with ugettext_lazy, etc.

    This way, you're not:

    • translating prematurely
    • using some lame "dummy" redundant function to "list" your translated strings
    • messing up, and having to give up manage.py makemessages

提交回复
热议问题