django blocktrans и i18n в шаблонах

Можно определить небольшую функцию помощника для предоставления преимуществ with без неоднозначности:

var with_ = function (obj, func) { func (obj); };

with_ (object_name_here, function (_)
{
    _.a = "foo";
    _.b = "bar";
});
10
задан Harley Holcombe 3 November 2011 в 05:09
поделиться

3 ответа

Это работает:

{% filter slugify %}{% trans cat.name %}{% endfilter %}
9
ответ дан 3 December 2019 в 14:53
поделиться
{% blocktrans with cat.name as cat_slug %}{{ cat_slug|capfirst }}{% endblocktrans %}

?

РЕДАКТИРОВАТЬ: вы были правы, в документе говорится, что фильтр должен быть помещен в блок-транс

1
ответ дан 3 December 2019 в 14:53
поделиться

I'm only just getting started with Django internationalization, but I think you're misunderstanding how the {% blocktrans %} tag handles placeholders.

The point of blocktrans is to allow the text around the placeholders to be translated. It won't translate anything inside {{...}}.

If you look at the generated .po file, you'll see that the following template code:

{% blocktrans %}This is my variable: {{variable}}{% endblocktrans %}

Will get converted into something like the following:

msgid:"This is my variable: %s"

I don't think you can translate a variable within a blocktrans tag. You can probably do constant strings with {% blocktrans with _("string") as x %}{{x}}{% endblocktrans %} but I can't think why you'd want to.

You'll have to do what you want in your view or model code I think.

18
ответ дан 3 December 2019 в 14:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: