Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django blocktrans and i18n in templates

I have an i18n problem in django:

This works fine :

{% trans cat.name %}  

cat.name will be translated

But this doesn't work:

{% blocktrans with cat.name|slugify as cat_slug %}{{ cat_slug }}{% endblocktrans %}  

cat.name is not translated

If I change the filter :

{% blocktrans with cat.name|capfirst as cat_slug %}{{ cat_slug }}{% endblocktrans %}  

I can see that the filter is working, but there is no translation...

like image 614
Julien Avatar asked Nov 28 '09 19:11

Julien


People also ask

What is Blocktrans Django?

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 %}

What is {% include %} in Django?

The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.

What is Gettext_lazy in Django?

gettext_lazy is a callable within the django. utils. translation module of the Django project.


1 Answers

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.

like image 134
Tom Avatar answered Oct 05 '22 23:10

Tom